简体   繁体   English

使用php发送(相对)大量的html电子邮件

[英]Sending (relatively) large volumes of html e-mail with php

What is the best way/class to send a lot of html e-mail? 发送大量html电子邮件的最佳方法/类是什么?

(Every mail needs to contain information (from a mysql database) that is linked to the address to which it's sent) (每封邮件都必须包含(从mysql数据库中获取)链接到其发送地址的信息)

First I store the information I need in a transient table (not temporary , just a normal table that I drop after I'm done). 首先,我将所需的信息存储在临时表中(不是临时表,而是完成后删除的普通表)。

Then I generate the mails, deleting the processed records as I go, and put the generated mails into an "outbox" table. 然后,我生成邮件,并随即删除已处理的记录,并将生成的邮件放入“发件箱”表中。

Lastly, a script fires off every N minutes taking M records out the "outbox" table: it sends a mail, and then deletes that mail from the outbox table. 最后,脚本每N分钟触发一次,以将M条记录从“发件箱”表中删除:它发送一封邮件,然后从发件箱表中删除该邮件。

Why the transient table? 为什么使用瞬态表? Because the server on which this all happens has very strict time constraints. 因为发生这一切的服务器具有非常严格的时间限制。 Using the above method allows me to partially generate the mails; 使用上述方法可以使我部分生成邮件; you can safely re-run the generation phase without worrying about duplicate mails being generated. 您可以安全地重新运行生成阶段,而不必担心会生成重复的邮件。 So in spite of the server killing the job, the script makes progress. 因此,尽管服务器取消了该工作,但脚本仍在进步。

The sending script sends (60 / N)*M mails an hour, again to work within the constraints of the server. 发送脚本每小时发送(60 / N)* M封邮件,再次在服务器的限制内工作。

I would suggest using the Pear packages Mail ( http://pear.php.net/package/Mail ) and Mail_Mime ( http://pear.php.net/package/Mail_Mime ). 我建议使用Pear软件包Mail( http://pear.php.net/package/Mail )和Mail_Mime( http://pear.php.net/package/Mail_Mime )。

If you need a queuing system, you could try gearman ( http://gearman.org/ ) 如果需要排队系统,可以尝试gearman( http://gearman.org/

I use Zend_Mail . 我使用Zend_Mail The following example (from the docs ) shows how to send multiple mails over a single SMTP connection: 以下示例(来自docs )显示了如何通过单个SMTP连接发送多个邮件:

// Create transport
$config = array('name' => 'sender.example.com');
$transport = new Zend_Mail_Transport_Smtp('mail.example.com', $config);

// Set From & Reply-To address and name for all emails to send.
Zend_Mail::setDefaultFrom('sender@example.com', 'John Doe');
Zend_Mail::setDefaultReplyTo('replyto@example.com','Jane Doe');

// Loop through messages
for ($i = 0; $i < 5; $i++) {
    $mail = new Zend_Mail();
    $mail->addTo('studio@example.com', 'Test');

    $mail->setSubject(
        'Demonstration - Sending Multiple Mails per SMTP Connection'
    );
    $mail->setBodyText('...Your message here...');
    $mail->send($transport);
}

// Reset defaults
Zend_Mail::clearDefaultFrom();
Zend_Mail::clearDefaultReplyTo();

You would just need to modify the loop to fetch data from your database specific to each message to send. 您只需要修改循环,即可从数据库中获取要发送的每条消息的特定数据。 Furthermore, you won't need the entire framework to make use of Zend_Mail . 此外,您将不需要整个框架来使用Zend_Mail

Take a look at: http://framework.zend.com/manual/en/zend.mail.multiple-emails.html 看一下: http : //framework.zend.com/manual/en/zend.mail.multiple-emails.html

I think the first consideration is that is likely to take some time to process - so should not be done as a synchronous web request - see 我认为第一个考虑因素是可能需要花费一些时间来处理-因此不应该作为同步Web请求进行处理-请参阅

Best way to manage long-running php script? 最好的方法来管理长期运行的PHP脚本?

There are then all sorts of issues about creating HTML emails. 这样就产生了有关创建HTML电子邮件的各种问题。 You might want to have a look at one of the off-the-shelf packages such as phpmailer for generating the email itself. 您可能想看看其中一种用于生成电子邮件本身的现成软件包,例如phpmailer。 Alternatively, if the content is very complicated, you might consider setting it up as a web page, perhaps using a templating system - but remember to restrict any external access to it. 另外,如果内容非常复杂,则可以考虑使用模板系统将其设置为网页-但切记限制对它的任何外部访问。 eg 例如

<?php
if ($_SERVER["REMOTE_ADDR"]!='127.0.0.1') {
   die ('NO ACCESS!');
}
$send_to=$_GET['email'];
$dbh=mysql_connect(...);
....

Then in your script: 然后在您的脚本中:

....
$content=file('http://localhost/generate_email.php?email='
    . urlencode($db_row['recipient']));
mail($db_row['recipient'], $subject, $content);

HTH HTH

C. C。

我喜欢Karim的想法,尽管我会try/catch通过$mail->send($transport)进行try/catch ,这样您可以跟踪已发送的内容和未发送的内容,并进行相应记录或在相关内容中进行标记数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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