dsymx68408 2013-09-02 09:14
浏览 47
已采纳

使用PHP Swiftmailer从txt文件向多个收件人发送电子邮件

I have been working on a script to send emails using Swiftmailer to a list of recipients stored in txt file. It is supposed to take emails from ./receivers folder all of them named TH001.txt, TH002.txt,... . When I run the code it goes to infinite loop with error:

"
PHP Notice:  Undefined index: 0
 in /var/www/sipnati/sendmail.php on line 37
PHP Notice:  Undefined index: 0
 in /var/www/sipnati/sendmail.php on line 37
"

It looks like I have messed up something with parsing the names of the files in ./receivers folder. I am running Ubuntu Server 12.04 with PHP5.3 . I am completely stuck, any help pointing the mistake will be greatly appreciated.

    <?php

require_once 'swift_required.php';

define("SMTP_SERVER", "xxx");
define("FROM_NAME", "xxx");
define("FROM_EMAIL", "xxx");
define("USERNAME", "xxx");
define("PASSWORD", "xxx");



define("LOG_PATH", "./");

class sendMail {

    public function sendMail() {

        $transport = Swift_SmtpTransport::newInstance(SMTP_SERVER, 25)
            ->setUsername(USERNAME)
            ->setPassword(PASSWORD)
            ;

        $mailer = Swift_Mailer::newInstance($transport);


 /*     $counter = file_get_contents("./variables.txt");
        $emails_to_send = $counter + 200;
        $file_counter = file_get_contents("./variables1.txt"); */

        $scan=array_values(array_diff(scandir('./receivers/'), array('..', '.','.htaccess')));
        $mails=array();
        $c=count($scan);
        if(isset($scan[0])){
            for($i=0;$i<$c;$i++){
                if(substr($scan[$i], strlen($scan[$i] - 3), 3) <> "txt"){
                    $tmp=file('./receivers/'.$scan[$i],FILE_IGNORE_NEW_LINES);
                    $mails=array_merge($mails,$tmp);
                }
            }
        }
 /*     file_put_contents("./variables1.txt", $count-1); */
        $regexp = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';

        $count = 0;
        if($c>0){
            foreach ($mails as $key => $value) {
                while ($count < count($mails)) { /* replaced $counter with count($mails) */

                    if (preg_match($regexp, $value) != 0) {

                        $message = Swift_Message::newInstance('Wonderful Subject')
                            ->setFrom(array(FROM_EMAIL => FROM_NAME))
                            ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
                            ->setSubject(file_get_contents('./thailand_subject.txt'))
                            ->setBody(file_get_contents('./thailand_body.txt'))
                            ;

                        if ($mailer->send($message)) {
                            $count++;
                        } else {
                            $not_sent[] = $value;
                        }
                    } else {
                        $not_sent[] = $value;
                    }
                    $count++;
                }
                /*file_put_contents("./variables.txt", $file_counter++);*/
                break;
            }
        }
        if (!isset($not_sent))
            $not_sent = null;
        $this->logToFile($count, count($mails), $not_sent);
    }

    private function logToFile($count, $on, $undelivered = null) {
        $toLog = "Sent " . $count . "/" . $on . " mails. Not sent: 
" . print_r($undelivered, true);
        file_put_contents(LOG_PATH . "MAIL_LOG_" . microtime(), $toLog);
    }

}

$p = new sendMail();
?>
  • 写回答

1条回答 默认 最新

  • donglinyi4313 2013-09-02 09:58
    关注

    New code:

    <?php
    
    require_once 'swift_required.php';
    
    define("SMTP_SERVER", "xxx");
    define("FROM_NAME", "xxx");
    define("FROM_EMAIL", "xxx");
    define("USERNAME", "xxx");
    define("PASSWORD", "xxx");
    
    define("LOG_PATH", "./");
    
    class sendMail {
    
        public function sendMail() {
    
            $transport = Swift_SmtpTransport::newInstance(SMTP_SERVER, 25)
                ->setUsername(USERNAME)
                ->setPassword(PASSWORD)
                ;
    
            $mailer = Swift_Mailer::newInstance($transport);
    
            $scan=array_values(array_diff(scandir('./receivers/'), array('..', '.','.htaccess')));
            $mails=array();
            $c=count($scan);
            if(isset($scan[0])){
                for($i=0;$i<$c;$i++){
                    if(substr($scan[$i], strlen($scan[$i] - 3), 3) <> "txt"){
                        $tmp=file('./receivers/'.$scan[$i],FILE_IGNORE_NEW_LINES);
                        $mails=array_merge($mails,$tmp);
                    }
                }
            }
    
            $regexp = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
    
            $c=count($mails);
            if($c>0){
                for($i=0;$i<$c;$i++){
                    if (preg_match($regexp, $mails[$i]) != 0) {
                        $message = Swift_Message::newInstance('Wonderful Subject')
                            ->setFrom(array(FROM_EMAIL => FROM_NAME))
                            ->setTo($mails[$i])
                            ->setSubject(file_get_contents('./thailand_subject.txt'))
                            ->setBody(file_get_contents('./thailand_body.txt'))
                        ;
                        if (!$mailer->send($message))
                            $not_sent[] = $mails[$i];
                    }
                    else
                        $not_sent[] = $mails[$i];
                }
            }
            if (!isset($not_sent))
                $not_sent = array();
    
            $this->logToFile($c, $not_sent);
        }
    
        private function logToFile($on, $undelivered) {
            $sent=$on-count($undelivered);
            $toLog = "Sent " . $sent . "/" . $on . " mails. Not sent: 
    " . print_r($undelivered, true);
            file_put_contents(LOG_PATH . "MAIL_LOG_" . microtime(), $toLog);
        }
    
    }
    
    $p = new sendMail();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误