电子邮件正文多次出现

时间:2012-11-16 11:04:09

标签: php email

我正在尝试使用PHP mail()在循环中发送一小批电子邮件。发送电子邮件的脚本运行正常。然而,有一个轻微的故障。虽然收件人都只收到一封电子邮件,但列表中的第一个人收到邮件正文($ MESSAGE_BODY)一次,第二个人收到身体两次,第三个人收到邮件3次(并且就此而去)。我不能为我的生活找出原因,为什么要这样做。

发送电子邮件的表单是:

<p>Message Text:
<br />
<textarea name="thebody" id="thebody" cols="65" rows="12"><?php echo $row_email['emailtext'];?></textarea>
<script type="text/javascript">CKEDITOR.replace( 'thebody' );</script>
</p>
<table >
<tr>
<th>Site</th>
<th>Email Address</th>
<th colspan="2">Email Now?</th>
</tr>
<?php  
$b = 0;
$q = 1;
while ($row_selfdo = mysql_fetch_assoc($selfdo)) {  ?>
<tr>
<td><?php echo $row_seldo[‘sitename’];?></td>
<td><input type="text" name="emailto[]" style="font-size:9px;" size="20" value="<?php echo $row_selfdo['eaddress']; ?>"/></td>

<td valign="middle">Yes:<input type="radio" name="emailnow[<?php echo $b;?>]" value="Yes" <?php if (isset($mailed) && ($mailed=="Not Yet")) { echo ""; } else echo "disabled='disabled'"; ?> /></td>

<td>No:<input name="emailnow[<?php echo $b;?>]" type="radio" value="No" checked="checked"  <?php if (isset($mailed) && ($mailed=="Not Yet")) { echo ""; } else echo "disabled='disabled'"; ?>? /></td>

</tr>
<?php $b++; $q++; }  ?>
</table>

这是发送邮件的脚本

$numb = count($_POST['emailto']);
$num = $numb -1;
$subject=$_POST['subject'];
$thisrecipient = $_POST['emailto'];
$sendtothemnow = $_POST['emailnow'];

for ($a=0;$a<=$num;$a++) {

$emailthemnow = $sendtothemnow[$a];


if ((isset($emailthemnow))&&(($emailthemnow)=="Yes")) {

$recipient = $thisrecipient[$a];
$ToEmail = $recipient; 
$EmailSubject = $subject; 
$mailheader = 'From: me@mydomain.com'."\r\n"; 
$mailheader .= 'Reply-To: me@mydomain.com'."\r\n";
$mailheader .= 'MIME-Version: 1.0'."\r\n";
$mailheader .= 'Content-type: text/html; charset=iso-8859-1'."\r\n"; 
$MESSAGE_BODY .= '<p>'.$_POST['thebody'].'</p>';
$MESSAGE_BODY .= '<p>Kind Regards</p>';
$MESSAGE_BODY .= '<p>The Environment Team</p>';
$MESSAGE_BODY .= 'email footer bits here ';
$MESSAGE_BODY .='<p style="color:#0C0;">Please consider the environment - do you really need to print this email?';

$MESSAGE_BODY=wordwrap($MESSAGE_BODY,70);

$mailsent=  mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Not Sent");

    if($mailsent){
    //update a table to record date email was sent
    }
}//end email send loop

有什么建议吗?

非常感谢提前

2 个答案:

答案 0 :(得分:3)

在您使用邮件正文的第一行,设置它而不是附加邮件:

$MESSAGE_BODY = '<p>'.$_POST['thebody'].'</p>';

(点已被删除)

答案 1 :(得分:0)

仅更改有冲突的变量名称。

if ((isset($emailthemnow))&&(($emailthemnow)=="Yes")) {
$recipient = $thisrecipient[$a];
$ToEmail = $recipient; 
$EmailSubject = $subject; 
$mailheader = 'From: me@mydomain.com'."\r\n"; 
$mailheader .= 'Reply-To: me@mydomain.com'."\r\n";
$mailheader .= 'MIME-Version: 1.0'."\r\n";
$mailheader .= 'Content-type: text/html; charset=iso-8859-1'."\r\n"; 
$MESSAGE_BODY .= '<p>'.$_POST['thebody'].'</p>';
$MESSAGE_BODY .= '<p>Kind Regards</p>';
$MESSAGE_BODY .= '<p>The Environment Team</p>';
$MESSAGE_BODY .= 'email footer bits here ';
$MESSAGE_BODY .='<p style="color:#0C0;">Please consider the environment - do you really need to print this email?';
$MESSAGE_BODY_FINAL=wordwrap($MESSAGE_BODY,70);
$mailsent=  mail($ToEmail, $EmailSubject, $MESSAGE_BODY_FINAL, $mailheader) or die ("Not Sent");
if($mailsent){
//update a table to record date email was sent
}
}