PHPMailer发送多个以逗号符号

时间:2017-10-02 06:06:29

标签: php mysql email phpmailer

我在使用PHPMailer发送多个以逗号符号分隔的电子邮件时遇到问题。 。我正在使用PHP 7.

我的mysql数据库中的这些数据包含以逗号(,)分隔的单个用户帐户中的多个电子邮件地址。

enter image description here

我已经按照这个问题here的答案,但它不起作用。电子邮件只能发送到john@mail.com。 billy@mail.com没有收到任何邮件。

我尝试回显var_dump(maildbs);

输出显示没有昏迷。

john@mail.combilly@mail.com

这是我的PHP代码。 。

sendmail.php

<?php
    $connect      = mysqli_connect("", "", "", "");
    global $connect;  
    if(isset($_POST['Submit'])){
        $staffname = $_POST['staffname'];
        $sql = "SELECT * FROM table WHERE staff_name ='$staffname'";
        $get = mysqli_query($connect,$sql);
        if($get && mysqli_num_rows($get) > 0 )
        {              
            while($row = mysqli_fetch_assoc($get))
            {
                $maildbs = explode(',',$row["email_address"]);
                foreach($maildbs as $maildb){
                    date_default_timezone_set('Etc/UTC');
                    require_once '../PHPMailerAutoload.php';
                    $mail = new PHPMailer;
                    $mail->isSMTP();
                    $mail->SMTPDebug = 2;
                    $mail->Debugoutput = 'html';
                    $mail->Host = "host";
                    $mail->Port = 25;
                    $mail->SMTPAuth = false;
                    $mail->setFrom('sender@mail.com', 'Sender');
                    $mail->addAddress('mail@mail.com','receipent');
                    $mail->addCC($maildb);
                    $mail->Subject = 'PHPMailer SMTP without auth test';
                    $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
                    $mail->AltBody = 'This is a plain-text message body';
                    $mail->Body = 'body content';
                    $mail->addAttachment('images/phpmailer_mini.png');
                    if (!$mail->send()) {
                        echo "Mailer Error: " . $mail->ErrorInfo;
                    } else {
                        echo "Message sent!";
                    }                   
                }   
            }   
        mysqli_free_result($get);
        }
    }
?>
<!DOCTYPE html>
<html><title>Test Email</title></head>
<body>
    <table>
        <form action="sendmail.php" method="POST">
            <tr>
                <td>Staff Name :</td>
                <td><input type="text" name="staffname" value="" ></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="Submit" value="send email"></td>
            </tr>
        </form>
    </table>
</body>
</html>

感谢有人可以提供帮助。

1 个答案:

答案 0 :(得分:0)

使用爆炸php功能

             $email = explode(',', $toEmail);
                for ($i = 0; $i < count($email); $i++) {
                    $mail->addAddress($email[$i], 'recipient '); 
                }
                if ($ccEmail != null) {
                    $emailCC = explode(',', $ccEmail);
                    for ($i = 0; $i < count($emailCC); $i++) {
                        $mail->addCC($emailCC[$i]);
                    }
                }
                if ($ccBCC != null) {
                    $emailBCC = explode(',', $ccBCC);
                    for ($i = 0; $i < count($emailBCC); $i++) {
                        $mail->addBCC($emailBCC[$i]);
                    }
                }
相关问题