PHP邮件批量邮件一个接一个

时间:2015-10-29 05:05:31

标签: php mailer

我正在阅读来自文本文件的电子邮件,并且必须一次发送一封电子邮件。除接收器外,一切(发送者,主体,身体)都是恒定的。我有我的PHP邮件工作,我正在使用的其他代码是:

<?php
if ($_FILES) {
    if ($_FILES['file']['name'] != "") {
        if (isset($_FILES) && $_FILES['file']['type'] != 'text/plain') {
            echo "<span>File could not be accepted ! Please upload any '*.txt' file.   </span>";
            exit();
        } 

        echo "<center><span id='Content'>Contents of ".$_FILES['file']['name']." File</span></center>"; 
        $fileName = $_FILES['file']['tmp_name'];
        $file = fopen($fileName,"r") or exit("Unable to open file!");
        $string = file_get_contents("$fileName"); // Load text file contents
        $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';
        preg_match_all($pattern, $string, $matches);
        $output = var_export($matches[0]);

        foreach ($output as $item) {
            echo "$item\n";
            $output = $item;
        }

        print_r($output);
    } else {
        if (isset($_FILES) && $_FILES['file']['type'] == '') {
            echo "<span>Please Choose a file by click on 'Browse' or 'Choose File' button.</span>";
        }
    }
}
?>

1 个答案:

答案 0 :(得分:0)

您可以将多个收件人设置为单个邮件,并将SingleTo设置为true。

$recipient_addresses = array();
....
$message = new \PHPMailer;
$message->SingleTo = true; 
....
foreach($recipient_addresses as $email) $message->addAddress($email);     
....

请注意,SingleTo仅支持“mail”和“sendmail”传输,而不支持“SMTP”。

如果您使用SMTP传输,只需逐个循环发送每个地址。只需调用$ message-&gt; addAddress一次,只为PHPMailer类的每个实例传递一个地址,但是逐个创建多个实例:

1. $message = new \PHPMailer;
2. $message->addAddress($email); 
3. fill other data, but don't set the SingleTo property value, keep it default (false).
4. $message->send();
5. unset($message);

另请注意,计划在PHPMailer 6.0版本中弃用SingleTo,并在7.0版本中删除。