php - 使用 phpmailer 从数据库发送电子邮件

标签 php mysqli phpmailer

我目前正在开发一个 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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a4242427a5d575b535614595557" rel="noreferrer noopener nofollow">[email protected]</a>";
        $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;

}

最佳答案

在成功的情况下,您的函数在第一次迭代后结束

return print_r($alamatmail);

该行退出该函数。下一次迭代将不会被调用。

将此行移至之后

endforeach;

并且您的代码应该可以工作(只是它不会打印整个邮件。您需要单独处理)

关于php - 使用 phpmailer 从数据库发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34285480/

相关文章:

PHP/MySQL - 需要帮助获取用户适用的事件列表

phpmailer - 在 "To"标题中使用 php mailer 时在邮件中显示两次相同的地址

php - 硬错误 : SMTP -> FROM SERVER:421 Unexpected failure, 请稍后再试

php - MYSQL中存储的汉字在CI中不显示

php - 通过 .htaccess 从 URL 剪切文件夹路径

php - 传递变量的更好方法?

php - 错误 mysqli_insert_id

php - 如何为与其他字段名称相同的字段创建别名?

PHP数据库插入问题

smtp - phpmailer 和 gmail SMTP 错误 : Failed to connect to server: Network is unreachable (101) SMTP connect() failed