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

问题描述:

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

<?php 

$email_to = "[email protected]"; // your email address 
$email_subject = "Contact Form Message"; // email subject line 
$thankyou = "thankyou.htm"; // thank you page 

?> 

请给我一个例子。谢谢。

+28

这是不幸的是,没有人读手册了。 – 2010-12-22 04:45:57

+7

*是手册 – 2017-03-31 01:26:41

$email_to = "[email protected], [email protected], [email protected]" 

需要是电子邮件adrresses的一个逗号分隔的列表。

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

为什么你在逗号后面放一个空格?而不是.com,地址例如? – Jon 2016-02-25 11:42:49

+1

@Jon - 可读性我在猜测 – w69rdy 2017-01-16 17:29:04

只用逗号分开它们,如$email_to = "[email protected], [email protected], John Doe <[email protected]>"

事情是这样的:

mail("[email protected] , [email protected] , [email protected]", "Test e-mail", "Hi, this is a test message!"); 

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

在邮件功能,你可以尽可能多的reciepient,只要你想在$ emailto paramater用英文逗号分隔。

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

你可以试试这个代码,假设你有你的电子邮件在一个数据库中

/*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"); 
+0

为什么你不直接把感受好的邮件放到数组中,然后在你调用`mail`之前`implode`? – prodigitalson 2010-12-22 04:42:06

富勒可读性起见在代码中使用数组和内爆到一个逗号分隔字符串: -

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

以下代码将完成任务....

<?php 

$contacts = array(
"[email protected]", 
"[email protected]", 
//....as many email address as you need 
); 

foreach($contacts as $contact) { 

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

} 

?> 

我认为下面的代码将起作用。

$tos = array('[email protected]', '[email protected]'); 
foreach ($tos as $to){ 
    $ok = mail ($to, $subject, $body, $from); 
} 
if ($ok) { 
    echo "Message Send"; 
} else { 
    echo "Error"; 
} 

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

将所有电子邮件地址发送给所有收件人是非常不好的做法;你应该使用密件抄送(密件抄送)。

$from = "[email protected]"; 
    $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 

$recipients = "[email protected],[email protected],[email protected],[email protected]"; 
    $email_array = explode(",",$recipients); 
    foreach($email_array as $email) 
    { 
     echo $to  = $email; 
     $subject = 'the subject'; 
     $message = 'hello'; 
     $headers = 'From: [email protected]' . "\r\n" . 
     'Reply-To: [email protected]' . "\r\n" . 
     'X-Mailer: PHP/' . phpversion(); 
     mail($to, $subject, $message, $headers); 

    }