2012-02-21 14 views
0

我有以下代碼,它發送電子郵件,但它將一個電子郵件嵌入到另一個電子郵件中,而不是單獨發送,輸出也在下面。PHP在發送電子郵件時做的事

有什麼想法?

H.

電子郵件輸出

SMTP Test Email Message 

Dear Gary, 

Dear Jaime, 

Test Message 

Here is a quick email test to see what it comes through like. Link: = 

Test 

Does it do formatting correctly and what is the situation with dual = names appearing etc? 

Kind regards, 

Gary. 
Link: http:// 

Test 

Does it do formatting correctly and what is the situation with dual names appearing etc? 

Kind regards, 

Gary. 

代碼片斷

do { 
    $dear = "<font face='Arial' size='2'>Dear " . $row["forename"] . ",<br><br></font>"; 

    $email_body = $dear . "<font face='Arial' size='2'>" . stripslashes($body) . "</font><br><br><font face='Arial' size='2'>" . $linky . " <a href='" . $row["link"] . "'> " . $row["link"] . "</a> </font> <br><br><font face='Arial' size='2'>" . stripslashes($body2) . "</font>"; 

    $subject = $sub; 

$bcc = "[email protected]"; 
$recipients = $row["email"].",".$bcc; 
// Constructing the email 
$sender = $bcc;            
$recipient = $recipients;        
$subject = $subject;             

$html = $email_body;  
$crlf = "\n"; 
    $headers = array(
     'To'  => $row["Mail"], 
     'From'   => $sender, 
     'Return-Path' => $sender, 
     'Subject'  => $subject 
        ); 

// Creating the Mime message 
$mime = new Mail_mime($crlf); 

// Setting the body of the email 
//$mime->setTXTBody($text); 
    $mime->setHTMLBody($html); 


// Set body and headers ready for base mail class 
$body = $mime->get(); 
$headers = $mime->headers($headers); 

// SMTP authentication params 
$smtp_params["host"]  = "scr.emserv.com"; 
$smtp_params["port"]  = "25"; 
$smtp_params["auth"]  = true; 
$smtp_params["username"] = "[email protected]"; 
$smtp_params["password"] = "blob"; 

// Sending the email using smtp 
$mail =& Mail::factory("smtp", $smtp_params); 
sleep(2); 
$result = $mail->send($recipient, $headers, $body); 

for($i=0;$i<$elements1;$i++){ 
$bar1->increase(); //calls the bar with every processed element 
$bar1->setMessage('Sending emails (2 second delay between each): '.(($i+1)*$sent).'%'); 

} 

if($row["sent"]=='YES'){ 
$reminder = $row["reminder"] + 1; 
$update = mysql_query("UPDATE $table SET reminder = $reminder WHERE respondent_id = " . $row["respondent_id"] . " ") or die("Could not update record<br>".mysql_error()); 
}else 

$update = mysql_query("UPDATE $table SET sent = 'YES' WHERE respondent_id = " . $row["respondent_id"] . " ") or die("Could not update record<br>".mysql_error()); 

} while ($row = mysql_fetch_assoc($results)); 
    $bar1->setMessage('Emails have now been sent: 100%'); 
    } 
+0

通過拋出你傳遞給'$ mail-> send()'調用的東西進行調試 - 如果加倍的電子郵件在那裏,那麼這是你的代碼引起的問題。最後,如果郵件沒有發送,你仍然更新數據庫,說它已被髮送!? – 2012-02-21 15:12:18

回答

1

你看到這個問題的原因是,你不清除$體變量。每次通過循環它基本上是追加到$ EMAIL_BODY變量在這一行:

$email_body = $dear . "<font face='Arial' size='2'>" . stripslashes($body) ...

你越往下$體設置爲整個消息:

$body = $mime->get();

get() returns the entire message as a string。它也沒有任何意義,因爲在這個do while循環中沒有初始化,直到我列出的第一行之後,你纔會使用$ body。