0

I have problem sending multiple email cc separated by coma symbol using PHPMailer . . i'm using PHP 7.

I have these data in my mysql database contains multiple email address in single user account separated by coma (,) .

enter image description here

I already follow the answer from this question here, but its not working. email can only be sent to john@mail.com . billy@mail.com did not receive any mail.

I tried to echo var_dump(maildbs);

output shown without coma . .

john@mail.combilly@mail.com

Here's my PHP code . .

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>

Appreciate if someone can help.

AlotJai
  • 147
  • 4
  • 18
  • This is terribly inefficient. Look at [the mailing list example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps) for how to do it better. Add one CC or BCC address at a time using `addCc()` and `addBcc()`. Also notice what `clearAddresses()` does. – Synchro Oct 02 '17 at 06:47

1 Answers1

0

use explode php function

             $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]);
                    }
                }