Home > Enterprise >  Sending emails to multiple addresses using config PHP file and PHP Mailer
Sending emails to multiple addresses using config PHP file and PHP Mailer

Time:10-26

I setup a config.php file to make it easy for my clients to add emails, subject and other sending information for an online form on their website. It looks like this:

<?php
$config = [
    "host" => "xxx",
    "username" => "xxx",
    "password" => "xxx",
    "secure" => "ssl", // ssl or tls
    "port" => 465,
    "sendTo" => "[email protected]",
    "sendToBCC" => "[email protected]",
    "from" => "[email protected]",
    "fromName" => "Contact Form"
];

The challenge I am having now is sending to multiple emails. I tried "sendTo" => "[email protected], [email protected], [email protected]", but it throws up invalid email error.

In the sending file, we have the code below:

//Recipients
$mail->setFrom($config['from'], $config['fromName']);
$mail->addAddress($config['sendTo']);
$mail->addCC($config['sendToCC']);          
$mail->addBCC($config['sendToBCC']);
$mail->addAddress($_POST['email']);

So I am guessing the line $mail->addAddress($config['sendTo']); is having trouble picking multiple emails. How do we edit this code to allow for multiple recipients? Will very much like to ensure that our clients can easily add emails in the config.php file instead of the sending file.

CodePudding user response:

Based on the examples from PHPMailer courtesy of the link shared by @Chris Haas. I made these changes:

<?php
$config = [
    "host" => "xxx",
    "username" => "xxx",
    "password" => "xxx",
    "secure" => "ssl", // ssl or tls
    "port" => 465,
    "sendTo" => "[email protected]",
    "sendTo2" => "[email protected]",
    "sendTo3" => "[email protected]",
    "sendToBCC" => "[email protected]",
    "from" => "[email protected]",
    "fromName" => "Contact Form"
];

And in the sending file I did this:

//Recipients
$mail->setFrom($config['from'], $config['fromName']);
$mail->addAddress($config['sendTo']);
$mail->addAddress($config['sendTo2']);
$mail->addAddress($config['sendTo3']);
$mail->addCC($config['sendToCC']);          
$mail->addBCC($config['sendToBCC']);
$mail->addAddress($_POST['email']);

That resolved the issue.

CodePudding user response:

You can support both cases:

$addrs = preg_split('#[\\s;,] #', $config['sendTo'], -1, PREG_SPLIT_NO_EMPTY);
foreach ($addrs as $addr) {
    $mail->addAddress($addr);
}

This will split a "[email protected]; [email protected], [email protected] [email protected]" into four addresses, and add them all. A single address will waste a few cycles to be translated into a one-element array, but will then work just as well.

You may want to also filter $addrs through filter_var, use array_unique, and do nothing if count($addrs) is zero, meaning no valid addresses are available:

function filterMail($emails) {
    $emails = array_map('trim', $emails);
    $emails = array_filter($emails);
    $emails = array_unique($emails);
    $emails = array_map(
        function ($email) {
            return filter_var($email, FILTER_VALIDATE_EMAIL);
        },
        $emails
    );
    $emails = array_filter($emails);
    $emails = array_values($emails);
    return $emails;
}
  • Related