0

I'm using the latest version of PHPMailer to send an email to 3 emails on the same domain.

All the emails, The one I send emails from and the 3 other emails I send messages to are on the same domain, It's a business Gmail account.

Here is the code:

//Including PHPMailer files
require_once('phpmailer/src/phpmailer.php');
require_once('phpmailer/src/SMTP.php');
require_once('phpmailer/src/Exception.php');

$msg = '';

//List of emails
$recipients = array('info@mydomain.com', 'help@mydomain.com', 'schedule@mydomain.com');

//Initializing PHPMailer
$mail = new PHPMailer\PHPMailer\PHPMailer();                              // Passing `true` enables exceptions

try {

    //Sender data
    $mail->setFrom('admin@mydomain.com', 'Admin');
    $mail->addReplyTo('info@mydomain.com', 'Admin');

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Hello World!';
    $mail->Body    = 'Hello World!';
    $mail->AltBody = 'Hello World!';

    //Loop through the emails list
    foreach ($recipients as $recipient) {

        $mail->addAddress($recipient);

        if (!$mail->send()) {
            echo "Mailer Error (" . str_replace("@", "&#64;", $recipient) . ') ' . $mail->ErrorInfo . '<br />';
            break; //Abandon sending
        } else {
            echo "Message sent to :"  . ' (' . str_replace("@", "&#64;", $recipient) . ')<br />';
        }

        // Clear all addresses and attachments for next loop
        $mail->clearAddresses(); 
        $mail->clearAttachments();

    }

} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

When I run the script on the same domain I get the following output:

Message sent to : (info@mydomain.com)
Message sent to : (help@mydomain.com)
Message sent to : (schedule@mydomain.com)

But when I check the inbox of these emails, I don't find the email.

I tried to get the mail.log file by:

ini_set('mail.log', __DIR__ . 'mail.log');

But it didn't work and I can't use $mail->SMTPDebug = 2;, As I'm not using SMTP.

  • Are you sure it's not in the junk folder or something? Also some providers (outlook most commonly) have an automatically junk deletion keep that in mind. – pr1nc3 Jan 11 '19 at 10:22
  • As you're not using SMTP, errors will appear in your mail server's logs, which are nothing to do with PHP ini settings - it should be somewhere like `/var/log/mail.log`. Generally you should be using SMTP, even if it's to localhost, as it's faster, safer, and easier to debug. – Synchro Jan 11 '19 at 10:31
  • @pr1nc3, Checked the spam folder, Nothing there –  Jan 11 '19 at 10:39
  • @Synchro, How to use SMTP with a business Gmail account?, All the examples I saw were using a personal account –  Jan 11 '19 at 10:41
  • I'm sure you can do that, (though I don't know the specifics - I'd search for them), but you can't be using gmail at the moment because you're sending through your local mail server. – Synchro Jan 11 '19 at 10:43
  • @Synchro, I didn't get what you mean by 'you can't be using gmail at the moment because you're sending through your local mail server.', I used my personal Gmail on a domain before and it worked, But I had to change some settings to allow PHPMailer sending emails from my personal account –  Jan 11 '19 at 10:52
  • The code you posted is sending using PHP's `mail()` function (what PHPMailer uses by default), which uses your local mail server, which is clearly not a gmail server, and gmail enforces using their servers for all sending (via SPF), therefore you're not sending using a gmail account in the script you posted. Even if you do succeed in actually submitting messages, they will get bounced or rejected because they're forgeries. – Synchro Jan 11 '19 at 10:57
  • @Synchro, With `SMTP` details It could look like this ` $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->SMTPKeepAlive = true; $mail->Username = 'info@mydomain.com'; $mail->Password = '***'; $mail->SMTPSecure = 'tls'; $mail->Port = 587;` –  Jan 11 '19 at 11:04
  • Yes, that's right, but not the code you asked about. I don't know if google for business requires any different settings. – Synchro Jan 11 '19 at 11:12

0 Answers0