PHP将邮件发送到多个电子邮件地址

时间:2010-12-22 04:08:50

标签: php forms

我应该在这个PHP脚本中修改哪些代码,将一封电子邮件发送到20多个电子邮件地址

<?php

$email_to = "youremailaddress@yourdomain.com"; // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page

?>

请举个例子。谢谢。

14 个答案:

答案 0 :(得分:53)

代码中的前提可读性使用数组并将其内嵌到以逗号分隔的字符串中: -

$recipients = array(
  "youremailaddress@yourdomain.com",
  // more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page

答案 1 :(得分:28)

你的

$email_to = "address@one.com, address@two.com, address@three.com"

需要是以逗号分隔的电子邮件地址列表。

mail($email_to, $email_subject, $thankyou);

答案 2 :(得分:26)

只需用逗号分隔它们,例如$email_to = "youremailaddress@yourdomain.com, emailtwo@yourdomain.com, John Doe <emailthree@example.com>"

答案 3 :(得分:12)

以下代码将执行任务....

<?php

$contacts = array(
"youremailaddress@yourdomain.com",
"youremailaddress@yourdomain.com",
//....as many email address as you need
);

foreach($contacts as $contact) {

$to      =  $contact;
$subject = 'the subject';
$message = 'hello';
mail($to, $subject, $message, $headers);

}

?>

答案 4 :(得分:7)

    $recipients = "test1@test.com,test2@test.com,test3@test.com,test1@test.com";
    $email_array = explode(",",$recipients);
    foreach($email_array as $email)
    {
        echo $to      =  $email;
        $subject = 'the subject';
        $message = 'hello';
       $headers = 'From: webmaster@example.com' . "\r\n" .
       'Reply-To: webmaster@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
        mail($to, $subject, $message, $headers);

    }

答案 5 :(得分:6)

这样的事情:

mail("john@doe.com , marry@mail.com , frank@domain.com", "Test e-mail", "Hi, this is a test message!");

http://myphpform.com/php-form-multiple-recipients.php

答案 6 :(得分:2)

最好的方法是将所有电子邮件保存在数据库中。

您可以尝试使用此代码,假设您的电子邮件已在数据库中

/*Your connection to your database comes here*/
$query="select email from yourtable";
$result =mysql_query($query);

/ 以上代码取决于您保存电子邮件地址的位置,因此请务必将其替换为参数 /

然后你可以从结果中创建逗号分隔的字符串

while($row=$result->fetch_array()){
        if($rows=='')    //this prevents from inserting comma on before the first element
        $rows.=$row['email'];
        else
        $rows.=','.$row['email'];
    }

现在你可以使用

$to = explode(',',$rows); // to change to array

$string =implode(',',$cc); //to get back the string separated by comma

使用上面的代码,您可以发送这样的电子邮件

mail($string, "Test", "Hi, Happy X-Mas and New Year");

答案 7 :(得分:1)

在邮件功能中,您可以在$ emailto参数中以逗号分隔所需数量的收件人。

答案 8 :(得分:1)

将所有电子邮件地址发送给所有收件人是非常糟糕的做法;你应该使用Bcc(盲目复印件)。

    $from = "myname@mymail.com";
    $recipList = "mailaddress1,mailaddress2,etc";
    $headers = "MIME-Version: 1.0\nContent-type: text/html; charset=utf-8\nFrom: {$from}\nBcc: {$recipList}\nDate: ".date(DATE_RFC2822);
    mail(null,$subject,$message,$headers); //send the eail

答案 9 :(得分:1)

尝试一下。它对我有用。

$to = $email1 .','. $email2 .','. $email3;

答案 10 :(得分:0)

我认为以下代码可行。

$tos = array('address1@yourdomain.com', 'address2@yourdomain.com');
foreach ($tos as $to){
    $ok = mail ($to, $subject, $body, $from);
}
if ($ok) {
    echo "Message Send";
} else { 
    echo "Error";
}

答案 11 :(得分:0)

这对我有用,

$ recipient_email ='sales @ abc.com,support @ xyz.com';

$ success = mail($ recipient_email,$ subject,$ body,$ headers);

答案 12 :(得分:0)

您可以编写多个电子邮件地址,并将其作为第一个参数传递给您。 示例:-

mail("FirstEmailId@ABC.com, SecondEmilId@XYZ.com","Subject","Message","From: EmailId@PQR.com");

答案 13 :(得分:-2)

以编程方式将提交的表单发送到多个电子邮件地址是可能的,但最好的做法是创建邮件列表。在代码上将放置列表地址,并且可以在不更改代码的情况下完成对收件人列表的电子邮件地址的任何更改或更新。