如何向php中的多个用户发送邮件?

时间:2015-04-21 17:02:00

标签: php email

我目前在向php中的多个用户发送邮件时遇到问题,我能够一次发送给一个用户,但邮件功能只将邮件发送给第一个用户而不发送邮件到第二个用户在$to变量中用逗号分隔的一个。

这是我的代码:

<?php
$reg = @$_POST['reg'];
if($reg) {
        $to = "mail1@mail.com,mail2@mail.com";                           
        $from = "hootpile@hootpile.com";
        $subject = 'Hi VITian !';
        $message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Hi VITian.</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><center>
<table id="ecxbodyTable" width="100%" height="100%" cellspacing="0" cellpadding="0" border="0" align="center" style="border-collapse:collapse;padding:0;background-color:#F2F2F2;height:100% !important;width:100% !important;">
<tbody>
<tr>
<td id="ecxbodyCell" valign="top" align="center" style="padding:20px;border-top:0;height:100% !important;width:100% !important;">
<table id="ecxtemplateContainer" width="600" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;border:0;">
<tbody>
<tr>
<td valign="top" align="center" style="">
<table id="ecxtemplatePreheader" width="600" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;background-color:#FFFFFF;border-top:0;border-bottom:0;">
<tbody>
<tr>
<td class="ecxpreheaderContainer" valign="top" style="padding-top:9px;"></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td valign="top" align="center" style="">
<table id="ecxtemplateHeader" width="600" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;background-color:#FFFFFF;border-top:0;border-bottom:0;">
<tbody>
<tr>
<td class="ecxheaderContainer" valign="top" style="">
<table class="ecxmcnImageBlock" width="100%" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;">
<tbody class="ecxmcnImageBlockOuter">
<tr>
<td class="ecxmcnImageBlockInner" valign="top" style="padding:9px;">
<table class="ecxmcnImageContentContainer" width="100%" cellspacing="0" cellpadding="0" border="0" align="left" style="border-collapse:collapse;">
<tbody>
<tr>
<td class="ecxmcnImageContent" valign="top" style="padding-right:9px;padding-left:9px;padding-top:0;padding-bottom:0;">
<a class="" style="word-wrap:break-word;" target="_blank" title="" href="http://www.hootpile.com">
<img class="ecxmcnImage" width="90" align="left" style="max-width:125px;padding-bottom:0;display:inline !important;vertical-align:bottom;border:0;text-decoration:none;-ms-interpolation-mode:bicubic;" src="http://www.hootpile.com/userdata/profile_pics/BqUht7I39izV6Fn/resized_2015-04-10_1620.png" alt="Hootpile">
</a>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td valign="top" align="center" style="">
<table id="ecxtemplateBody" width="600" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;background-color:#FFFFFF;border-top:0;border-bottom:0;">
<tbody>
<tr>
<td class="ecxbodyContainer" valign="top" style="">
<table class="ecxmcnTextBlock" width="100%" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;">
<tbody class="ecxmcnTextBlockOuter">
<tr>
<td class="ecxmcnTextBlockInner" valign="top" style="">
<table class="ecxmcnTextContentContainer" width="600" cellspacing="0" cellpadding="0" border="0" align="left" style="border-collapse:collapse;">
<tbody>
<tr>
<td class="ecxmcnTextContent" valign="top" style="padding-top:9px;padding-right:18px;padding-bottom:9px;padding-left:18px;color:#606060;font-family:Helvetica;font-size:15px;line-height:150%;text-align:left;">
<h1 class="ecxnull" style="padding:0;display:block;font-family:Helvetica;font-size:40px;font-style:normal;font-weight:bold;line-height:125%;letter-spacing:-1px;text-align:left;color:#606060 !important;">
<strong style="color:#000000;display:block;font-family:helvetica,arial,sans-serif;font-size:24px;font-weight:bold;line-height:30px;text-align:left;text-transform:none;">
<span style="font-size:22px;">
<span style="color:#333333;font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;line-height:1.6em;">Join Hootpile Now.</span>
</span>
</strong>
</h1>
<p style="padding:0;color:#606060;font-family:Helvetica;font-size:15px;line-height:150%;text-align:left;">
<span style="background-color:#FFFFFF;color:#000000;font-family:courier,courier new,monospace;font-size:15px;line-height:18px;text-align:left;">Hootpile is social utility that is transforming how students at VIT University connect with each other.</span>
<span style="color:#000000;font-family:courier,courier new,monospace;font-size:15px;line-height:18px;"> We believe that students should be provided with a common platform to discuss everything, build anything. Hootpile is free from advertising, manipulation and  exploitation. </span>
<br>
<br>
<span style="color:#000000;font-family:courier,courier new,monospace;font-size:15px;line-height:18px;">We can all be in one place but it starts from you, Join Now!</span>
<br>
<br>
<span style="background-color:#FFFFFF;color:#000000;font-family:courier,courier new,monospace;font-size:15px;line-height:18px;text-align:left;">Simple, beautiful, and ad-free.</span>
<br>
</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<table class="ecxmcnButtonBlock" width="100%" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;">
<tbody class="ecxmcnButtonBlockOuter">
<tr>
<td class="ecxmcnButtonBlockInner" valign="top" align="left" style="padding-top:0;padding-right:18px;padding-bottom:18px;padding-left:18px;">
<table class="ecxmcnButtonContentContainer" cellspacing="0" cellpadding="0" border="0" style="border-collapse:separate !important;border:2px solid #418bbc;border-radius:0px;background-color:#418bbc;">
<tbody>
<tr>
<td class="ecxmcnButtonContent" valign="middle" align="center" style="font-size:16px;padding:15px;">
<a class="ecxmcnButton " style="font-weight:bold;letter-spacing:normal;line-height:100%;text-align:center;text-decoration:none;color:#FFFFFF;word-wrap:break-word;" target="_blank" href="http://www.hootpile.com" title="Go to Hootpile">Go to Hootpile.</a>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td valign="top" align="center" style="">
<table id="ecxtemplateFooter" width="600" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;background-color:#FFFFFF;border-top:0;border-bottom:0;">
<tbody>
<tr>
<td class="ecxfooterContainer" valign="top" style="padding-bottom:9px;">
<table class="ecxmcnTextBlock" width="100%" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;">
<tbody class="ecxmcnTextBlockOuter">
<tr>
<td class="ecxmcnTextBlockInner" valign="top" style="">
<table class="ecxmcnTextContentContainer" width="600" cellspacing="0" cellpadding="0" border="0" align="left" style="border-collapse:collapse;">
<tbody>
<tr>
<td class="ecxmcnTextContent" valign="top" style="padding-top:9px;padding-right:18px;padding-bottom:9px;padding-left:18px;color:#606060;font-family:Helvetica;font-size:11px;line-height:125%;text-align:left;">
<br>
<br>
<br>
<span style="font-size:14px;">
<span style="font-family:courier new,courier,lucida sans typewriter,lucida typewriter,monospace;">
<span>
Visit the 
<a style="word-wrap:break-word;color:#606060;font-weight:normal;text-decoration:underline;" target="_blank" href="http://www.hootpile.com/manifesto">Hootpile Manifesto page.</a>
</span>
</span>
</span>
<br>
<br>
<br>
<br>
<span style="font-size:14px;">
<span style="font-family:courier new,courier,lucida sans typewriter,lucida typewriter,monospace;">
<strong>
<a style="word-wrap:break-word;color:#606060;font-weight:normal;text-decoration:underline;" target="_blank" href="http://www.hootpile.com">
<span style="color:#000000;">Hootpile.com</span>
</a>
</strong>
</span>
</span>
<br>
<br>
<br>
<br>
<span style="font-size:11px;">
<span style="font-family:courier new,courier,lucida sans typewriter,lucida typewriter,monospace;">
<span>
<span style="color:#D3D3D3;">If you dont want to receive </span>
<a target="_blank" style="word-wrap:break-word;color:#606060;font-weight:normal;text-decoration:underline;" href="http://ello.us3.list-manage2.com/unsubscribe?u=9ad48e023e0e67d4a8cda3fc9&id=eac7ac1a28&e=c73797a5f4&c=9d1407f6b5">
<span style="color:#D3D3D3;">emails</span>
</a>
<span style="color:#D3D3D3;">
from Hootpile
<br>
anymore, please
</span>
<a target="_blank" style="word-wrap:break-word;color:#606060;font-weight:normal;text-decoration:underline;" href="http://www.hootpile.com">
<span style="color:#D3D3D3;">unsubscribe here.</span>
</a>
<br>
<br>
<span style="color:#D3D3D3;">Hootpile, VIT University</span>
</span>
</span>
</span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</center>
</body></html>';
        $headers = "From: $from\n";
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\n";
        mail($to, $subject, $message, $headers);
}
?>
<form id="new_user" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" accept-charset="UTF-8" autocomplete="off">
<input type="submit" name="reg" value="send">
</form>

1 个答案:

答案 0 :(得分:1)

使用多次mail()函数:

$to = "hootpile@gmail.com, hootpile2@gmail.com";
foreach(explode(',', $to) as $mail) {
  mail(trim($mail), $subject, $message, $headers);
}

这将为每个用户发送一封电子邮件。