如何使用php和shell命令发送大量电子邮件

时间:2013-06-21 09:49:18

标签: php codeigniter shell-exec

我在循环发送电子邮件时遇到问题。详情如下:

我有一个名为email.csv的csv文件。我正在上传此文件并从此csv文件中读取电子邮件。我正在使用codeignitor框架。用于读取csv文件和发送电子邮件的代码,代码如下。

 $file = $_FILES['email_from_file'][tmp_name];
                        $handle = fopen($file,"r");

                        if (($handle = fopen($file,"r")) !== FALSE) {
                            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                                $num = count($data);
                                for ($c=0; $c < $num; $c++) {
                                    $this->email->clear();
                                    $this->email->set_mailtype("html");
                                    $this->email->to(trim($data[$c]));
                                    $this->email->from('admin@workerbee.com');
                                    $this->email->subject($data['news_letter_info'][0]['subject']);
                                    $this->email->message($data['news_letter_info'][0]['template_body']);
                                    $this->email->send();
                                }
                            }
                            fclose($handle);
                        }

当我发送电子邮件时,需要花费太多时间来执行和发送电子邮件。而我的浏览器也变得悬而未决。那么有没有办法使用shell命令发送这些电子邮件,这将在后台执行php脚本。

提前感谢。

2 个答案:

答案 0 :(得分:1)

使用此功能。

exec('php /<SERVER PATH>/send_bulkmail.php');

在你的send_bulkmail.php中使用代码来使用fread()或file_get_content()来读取文件,并像上面那样在lood中发送邮件。

答案 1 :(得分:0)

我将Cron作业用于此类任务。

检查crontab手册。 http://www.linuxmanpages.com/man5/crontab.5.php

EX。

*/5 * * * * php /u1/dev/cuser5/scripts/cli.php --action project/.../messages/send-email.php

这意味着。每天每5分钟运行一次send-email文件。

注意:如果您需要发送许多电子邮件,那么您应该以较小的组发送电子邮件。

相关问题