使用SwiftMailer批量发送电子邮件 [英] Batch Send Email with SwiftMailer

查看:268
本文介绍了使用SwiftMailer批量发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 SwiftMailer 向多个用户(最多50个)发送电子邮件.我已经设置好并且可以正常工作,但是,我不确定如何从MySQL数据库中提取收件人并重复发送.

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.

这是我目前拥有的:

<?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('myfrom@domain.com' => 'From Me'))

  ->setTo(array('tom_jones@domain.com' => 'Tom Jones', 'jsmith@domain.com' => 'Jane Smith', 'j_doe@domain.com' => 'John Doe', 'bill_watson@domain.com' => '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中,我想从数据库中的用户进行迭代.像这样:

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

文档指出:

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.

但是,我不确定: 1:如何在此脚本中从日期数据库中进行选择,以及: 2:如果我需要使用addTo()在我的情况下的方法.有关如何正确设置此设置的任何建议?

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?

谢谢!

推荐答案

我不太确定我的问题是对的,但这是一种解决方法:

I am not quite sure that I got your question right, but here is a way of doing it:

<?php
$message = Swift_Message::newInstance()
  ->setSubject('Let\'s get together today.')
  ->setFrom(array('myfrom@domain.com' => 'From Me'))
  ->setBody('Here is the message itself')
  ->addPart('<b>Test message being sent!!</b>', 'text/html')
;

$data = mysql_query('SELECT first, last, email FROM users WHERE is_active=1') or die(mysql_error());
while($row = mysql_fetch_assoc($data))
{
   $message->addTo($row['email'], $row['first'] . ' ' . $row['last']);
}

$message->batchSend();
?>

我希望那是你想要的.

这篇关于使用SwiftMailer批量发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆