我目前正在开发一个php mysql基础系统,它将从数据库向多个收件人发送电子邮件。我已经在论坛搜索,很多答案是使用循环到电子邮件收件人。然后我尝试在电子邮件收件人中使用循环,但只能发送1封电子邮件(虽然在数据库中有5个或更多的电子邮件收件人)。你能告诉我,我的代码在哪里错了吗? 以下是我的代码:
function send_message( $from, $to, $subject, $message_content )
{
require_once "function.php";
require_once( 'phpmailer/PHPMailerAutoload.php' );
//Initiate the mailer class
$mail = new PHPMailer();
//Check to see if SMTP creds have been defined
if( defined( 'SMTP_USER' ) && defined( 'SMTP_PASS' ) && defined( 'SMTP_LOCATION' ) && defined( 'SMTP_PORT' ) )
{
$mail->IsSMTP();
$mail->Host = SMTP_LOCATION;
$mail->SMTPAuth = true;
$mail->Port = SMTP_PORT;
$mail->Username = SMTP_USER;
$mail->Password = SMTP_PASS;
if( defined( 'DEBUG' ) && DEBUG )
{
$mail->SMTPDebug = 1;
}
}
//Set the sender and receiver email addresses
$alamatmail=get_mail();
foreach ( $alamatmail as $datamail):
$from="xxx@gmail.com";
$to=$datamail['email'];
//Include the phpmailer files
$mail->SetFrom( $from, "" );
//We 'can' send to an array, in which case you'll want to explode at comma or line break
if( is_array( $to ) )
{
foreach( $to as $i )
{
$mail->addAddress( $i );
}
}
else
{
$mail->AddAddress( $to, "" );
}
//Set the message subject
$mail->Subject = $subject;
//Add the message header
$message = file_get_contents( 'email-templates/email-header.php' );
//Add the message body
$message .= file_get_contents( 'email-templates/email-body.php' );
//Add the message footer content
$message .= file_get_contents( 'email-templates/email-footer.php' );
//Replace the codetags with the message contents
$replacements = array(
'({message_subject})' => $subject,
'({message_body})' => nl2br( stripslashes( $message_content ) ),
);
$message = preg_replace( array_keys( $replacements ), array_values( $replacements ), $message );
//Make the generic plaintext separately due to lots of css and tables
$plaintext = $message_content;
$plaintext = strip_tags( stripslashes( $plaintext ), '<p><br><h2><h3><h1><h4>' );
$plaintext = str_replace( array( '<p>', '<br />', '<br>', '<h1>', '<h2>', '<h3>', '<h4>' ), PHP_EOL, $plaintext );
$plaintext = str_replace( array( '</p>', '</h1>', '</h2>', '</h3>', '</h4>' ), '', $plaintext );
$plaintext = html_entity_decode( stripslashes( $plaintext ) );
//Send the message as HTML
$mail->MsgHTML( stripslashes( $message ) );
//Set the plain text version just in case
$mail->AltBody = $plaintext;
$failed_error="email gagal dikirim";
//Display success or error messages
if( !$mail->Send() )
{
return 'Message send failure: ' . $mail->ErrorInfo;
return $failed_error;
}
else
{
//You'll usually want to just return true, but for the purposes of this
//Example I'm returning the message contents
// return $message;
return print_r($alamatmail);
}
endforeach;
}
答案 0 :(得分:3)
在成功案例中,您的函数在第一次迭代后由
结束return print_r($alamatmail);
此行退出该功能。下一次迭代不会被调用。
在
之后移动此行endforeach;
并且您的代码应该有效(只有它不会打印整个邮件。您需要单独处理)
答案 1 :(得分:0)
由于您向所有收件人发送相同的邮件,因此无需一遍又一遍地迭代邮件。您需要做的就是设置发件人,然后将所有收件人添加到addAddress数组,然后结束循环。
之后,您只需构建邮件正文并执行发送功能。
您可以使用:
<?
function send_message( $from, $to, $subject, $message_content )
{
require_once "function.php";
require_once( 'phpmailer/PHPMailerAutoload.php' );
//Initiate the mailer class
$mail = new PHPMailer();
//Check to see if SMTP creds have been defined
if( defined( 'SMTP_USER' ) && defined( 'SMTP_PASS' ) && defined( 'SMTP_LOCATION' ) && defined( 'SMTP_PORT' ) )
{
$mail->IsSMTP();
$mail->Host = SMTP_LOCATION;
$mail->SMTPAuth = true;
$mail->Port = SMTP_PORT;
$mail->Username = SMTP_USER;
$mail->Password = SMTP_PASS;
if( defined( 'DEBUG' ) && DEBUG )
{
$mail->SMTPDebug = 1;
}
}
$mail->SetFrom( 'xxx@gmail.com', "" );
//Set the sender and receiver email addresses
$alamatmail = get_mail();
foreach ($alamatmail as $datamail) {
$to = $datamail['email'];
//We 'can' send to an array, in which case you'll want to explode at comma or line break
if(is_array($to)) {
foreach( $to as $i ) {
$mail->AddAddress($i, "" );
}
}
else {
$mail->AddAddress($to, "" );
}
}
//Set the message subject
$mail->Subject = $subject;
//Add the message header
$message = file_get_contents( 'email-templates/email-header.php' );
//Add the message body
$message .= file_get_contents( 'email-templates/email-body.php' );
//Add the message footer content
$message .= file_get_contents( 'email-templates/email-footer.php' );
//Replace the codetags with the message contents
$replacements = array(
'({message_subject})' => $subject,
'({message_body})' => nl2br( stripslashes( $message_content ) ),
);
$message = preg_replace( array_keys( $replacements ), array_values( $replacements ), $message );
//Make the generic plaintext separately due to lots of css and tables
$plaintext = $message_content;
$plaintext = strip_tags( stripslashes( $plaintext ), '<p><br><h2><h3><h1><h4>' );
$plaintext = str_replace( array( '<p>', '<br />', '<br>', '<h1>', '<h2>', '<h3>', '<h4>' ), PHP_EOL, $plaintext );
$plaintext = str_replace( array( '</p>', '</h1>', '</h2>', '</h3>', '</h4>' ), '', $plaintext );
$plaintext = html_entity_decode( stripslashes( $plaintext ) );
//Send the message as HTML
$mail->MsgHTML(stripslashes($message));
//Set the plain text version just in case
$mail->AltBody = $plaintext;
$failed_error="email gagal dikirim";
//Display success or error messages
if(!$mail->Send()) {
return 'Message send failure: ' . $mail->ErrorInfo . $failed_error;
} else {
//You'll usually want to just return true, but for the purposes of this
//Example I'm returning the message contents
// return $message;
return print_r($alamatmail);
}
}
由于第一个包含每封电子邮件中to字段中的所有收件人,您可以使用此功能(我已忽略错误部分,因此您可以手动添加):
<?
function send_message( $from, $to, $subject, $message_content )
{
require_once "function.php";
require_once( 'phpmailer/PHPMailerAutoload.php' );
//Initiate the mailer class
$mail = new PHPMailer();
//Check to see if SMTP creds have been defined
if( defined( 'SMTP_USER' ) && defined( 'SMTP_PASS' ) && defined( 'SMTP_LOCATION' ) && defined( 'SMTP_PORT' ) )
{
$mail->IsSMTP();
$mail->Host = SMTP_LOCATION;
$mail->SMTPAuth = true;
$mail->Port = SMTP_PORT;
$mail->Username = SMTP_USER;
$mail->Password = SMTP_PASS;
if( defined( 'DEBUG' ) && DEBUG )
{
$mail->SMTPDebug = 1;
}
}
$mail->SetFrom( 'xxx@gmail.com', "" );
//Set the sender and receiver email addresses
$alamatmail = get_mail();
//Set the message subject
$mail->Subject = $subject;
//Add the message header
$message = file_get_contents( 'email-templates/email-header.php' );
//Add the message body
$message .= file_get_contents( 'email-templates/email-body.php' );
//Add the message footer content
$message .= file_get_contents( 'email-templates/email-footer.php' );
//Replace the codetags with the message contents
$replacements = array(
'({message_subject})' => $subject,
'({message_body})' => nl2br( stripslashes( $message_content ) ),
);
$message = preg_replace( array_keys( $replacements ), array_values( $replacements ), $message );
//Make the generic plaintext separately due to lots of css and tables
$plaintext = $message_content;
$plaintext = strip_tags( stripslashes( $plaintext ), '<p><br><h2><h3><h1><h4>' );
$plaintext = str_replace( array( '<p>', '<br />', '<br>', '<h1>', '<h2>', '<h3>', '<h4>' ), PHP_EOL, $plaintext );
$plaintext = str_replace( array( '</p>', '</h1>', '</h2>', '</h3>', '</h4>' ), '', $plaintext );
$plaintext = html_entity_decode( stripslashes( $plaintext ) );
//Send the message as HTML
$mail->MsgHTML(stripslashes($message));
//Set the plain text version just in case
$mail->AltBody = $plaintext;
$failed_error="email gagal dikirim";
foreach ($alamatmail as $datamail) {
$to = $datamail['email'];
//We 'can' send to an array, in which case you'll want to explode at comma or line break
if(is_array($to)) {
foreach( $to as $i ) {
$mail2 = clone $mail;
$mail2->AddAddress($i, "");
$mail2->send();
}
}
else {
$mail2 = clone $mail;
$mail2->AddAddress($to, "");
$mail2->send();
}
}
}
&#13;