发送电子邮件到多个(每行电子邮件地址)和打印

时间:2015-04-15 01:28:45

标签: php html forms email

我不擅长PHP但仍尝试创建测试脚本以便我可以学习它。我指的是w3schools,我不知道我有多好或多坏。 我需要对刚刚创建的脚本进行一些更改。

<?php

$to = $_POST['EmailList'];
$subject = $_POST['EmailSubject'];
$message = $_POST['EmailBody'];

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <admin@admin.com>' . "\r\n";

mail($to,$subject,$message,$headers);

print_r($headers)
?>

<html>
<center>
<form method="post">
<br><strong>PHP Email Sender</strong><br><br><br>

Email List<br>
<textarea name="EmailList" placeholder="email@email.com (New Email Each Line)"  rows="20" cols="50"></textarea><br><br>

Subject<br>
<input type="text" name="EmailSubject" placeholder="Your Subject Goes Here"><br><br>

Body<br>
<textarea name="EmailBody" placeholder="Write your content (HTML Accepted)"  rows="20" cols="50"></textarea><br><br>

<input type="submit" value="Submit!">
</form><br><br>

</center>
</html>
  1. 我想要一些帮助,以便我可以发送电子邮件到不同的电子邮件,但每封电子邮件将在不同的行,我不会使用逗号(,)。我希望脚本能够自己生成逗号(,)并从新行中传送每封电子邮件。
  2. 例如,我输入了10封电子邮件,我需要打印每封电子邮件,并发送除此之外发送的消息。
  3. 如果可能,请告诉我。我只是需要一些帮助。

1 个答案:

答案 0 :(得分:0)

经测试:

<?php
$subject = $_POST['EmailSubject'];
$message = $_POST['EmailBody'];

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <admin@admin.com>' . "\r\n";
$emailList = explode("\n",$_POST['EmailList']);
if(count($emailList) > 0){
  foreach($emailList as $to){
    $to = trim($to);
    $sent = mail($to,$subject,$message,$headers);
    if ($sent){
      echo "<p>Sent: $to</p>";
    }
    else{
       echo "<p>Not Sent: $to</p>";
    }
  }
}
else{
  echo "<p>No email addresses</p>";
}
print_r($headers);

?>