简体   繁体   中英

Sending emails to multiple addresses using config PHP file and PHP Mailer

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" => "abc@email.com",
    "sendToBCC" => "xyz@email.com",
    "from" => "no-reply@email.com",
    "fromName" => "Contact Form"
];

The challenge I am having now is sending to multiple emails. I tried "sendTo" => "abc@email.com, efg@email.com, hik@email.com", 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.

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" => "abc@email.com",
    "sendTo2" => "efg@email.com",
    "sendTo3" => "hik@email.com",
    "sendToBCC" => "xyz@email.com",
    "from" => "no-reply@email.com",
    "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.

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 "joe@mail1.com; jack@mail2.com, jill@mail3.com jane@mail4.com" 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM