使用 SwiftMailer 批量发送电子邮件
我目前正在使用 SwiftMailer 向多个用户(最多 50 个)发送电子邮件。我已将其设置并正常工作,但是,我不太确定如何从 MySQL 数据库中提取收件人并迭代发送它们。
这是我目前拥有的:
<?php
require_once 'swift/lib/swift_required.php';
$mailer = Swift_Mailer::newInstance(
Swift_SmtpTransport::newInstance('smtp.connection.com', 25)
->setUsername('myUserName')
->setPassword('myPassword')
);
$mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(9));
$message = Swift_Message::newInstance()
->setSubject('Let\'s get together today.')
->setFrom(array('[email protected]' => 'From Me'))
->setTo(array('[email protected]' => 'Tom Jones', '[email protected]' => 'Jane Smith', '[email protected]' => 'John Doe', '[email protected]' => 'Bill Watson',))
->setBody('Here is the message itself')
->addPart('<b>Test message being sent!!</b>', 'text/html')
;
$numSent = $mailer->batchSend($message, $failures);
printf("Sent %d messages\n", $numSent);
正如您在上面看到的,在 setTo 中,我想从数据库中的用户进行迭代。类似于:
从用户 WHERE is_active=1 中选择第一个、最后一个电子邮件
注意:多次调用 setTo() 不会添加新的收件人 - 每次调用都会覆盖之前的调用。如果您想迭代地添加收件人,请使用 addTo() 方法。
但是,我不确定:1:如何从此脚本中的数据库中进行选择以及: 2:如果我需要在我的情况下使用 addTo() 方法。关于如何正确设置它有什么建议吗?
谢谢!
I'm currently using SwiftMailer to send out emails to several users (up to 50). I have it set up and working properly, however, I'm not quite sure how to pull the recipients from my MySQL database and iterate to send them.
Here is what I currently have:
<?php
require_once 'swift/lib/swift_required.php';
$mailer = Swift_Mailer::newInstance(
Swift_SmtpTransport::newInstance('smtp.connection.com', 25)
->setUsername('myUserName')
->setPassword('myPassword')
);
$mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(9));
$message = Swift_Message::newInstance()
->setSubject('Let\'s get together today.')
->setFrom(array('[email protected]' => 'From Me'))
->setTo(array('[email protected]' => 'Tom Jones', '[email protected]' => 'Jane Smith', '[email protected]' => 'John Doe', '[email protected]' => 'Bill Watson',))
->setBody('Here is the message itself')
->addPart('<b>Test message being sent!!</b>', 'text/html')
;
$numSent = $mailer->batchSend($message, $failures);
printf("Sent %d messages\n", $numSent);
As you can see above, in the setTo, I'd like to iterate from my users in the database. Something like:
SELECT first, last, email FROM users WHERE is_active=1
The documentation states:
Note: Multiple calls to setTo() will not add new recipients – each call overrides the previous calls. If you want to iteratively add recipients, use the addTo() method.
But, I'm not sure: 1: How to select from my datebase in this script and: 2: If I would need to use the addTo() method in my case. Any suggestions on how to set this up properly?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定我是否正确回答了你的问题,但这里有一种方法:
我希望这就是你想要的。
I am not quite sure that I got your question right, but here is a way of doing it:
I hope that's what you wanted.