douze2890241475 2015-10-29 05:05
浏览 71

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

I am reading emails from a text file and have to send one email at a time. Everything(sender, subject, body) is constant except for receiver. I have my PHP mailer working and the additional code I am using is:

<?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
";
            $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条回答 默认 最新

  • dqbjvg2518 2017-05-10 22:28
    关注

    You can set multiple recipients to a single message and set the SingleTo to true.

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

    Please not that the the SingleTo is only supported in "mail" and "sendmail" transports, not in "SMTP".

    If you use the SMTP transport, just loop the send for each address one-by-one. Just call $message->addAddress one time to pass just one address for each instance of the PHPMailer class, but create multiple instance, one-by-one:

    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);
    

    Please also be aware of the fact that SingleTo is planned to be deprecated in the release of PHPMailer 6.0, and removed in 7.0.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看