您的问题分为两部分:
- 如何从 MailChimp 中获取我的电子邮件列表?
- 如何向任意电子邮件地址发送电子邮件?
第一个块在这里是最重要的。第二块有 ton 种可能的答案,应该很容易完成。
从 MailChimp 获取列表
MailChimp 提供了一个至关重要的 API。目前,他们正在开发 v3.0,但 v2.0 仍被标记为“当前”,因此我们将依赖该版本的 API。要使用 API,MailChimp recommends a few third-party packages。对于这个例子,我使用的是 mailchimp-api,它可以使用 composer 安装:
$ composer require drewm/mailchimp-api
要向 MailChimp 验证您自己,您将需要一个 API 密钥。 MailChimp provides full instructions 获取 API 密钥,但简短的版本是这样的:
单击您的个人资料名称以展开帐户面板,然后选择
帐户。
点击 Extras 下拉菜单并选择 API 密钥。
复制现有的 API 密钥或单击“创建密钥”按钮。
描述性地命名您的密钥,以便您知道哪个应用程序使用它
键。
接下来,您需要 list id 用于您要从中获取电子邮件的列表。再次,MailChimp provides the best documentation 为此。我的列表 id 是一个包含字母和数字的 10 个字符的字符串。
最后,我们编写 PHP:
$apiKey = /*Your API key*/;
$listId = /*Your List ID*/;
$MailChimp = new \Drewm\MailChimp($apiKey);
$args = array(
'id' => $listId,
);
$result = $MailChimp->call('lists/members', $args);
//Check for any errors first, if none have occured, build the email list.
if(isset($result['status']) && $result['status'] == 'error'){
throw new Exception('call to Mailchimp API has failed.');
} else {
$emails = array();
//Build an array of emails for users that are currently subscribed.
foreach($result['data'] as $recipient){
if($recipient['status'] == 'subscribed' && !empty($recipient['email'])){
$emails[] = $recipient['email'];
}
}
}
$MailChimp->call('lists/members', $args) 返回一个非常庞大的 JSON 响应,其中包含 很多 有趣的信息。如果您通过 MailChimp 中的合并设置存储个性化信息,它们将在此 JSON 响应中可用。但是,为了使这个示例尽可能简单,我只检查了用户是否订阅并存储了他们的电子邮件地址。
在此块的末尾,$emails 现在将所有电子邮件地址存储在您的列表中。由于这每次都会调用 API,因此在 MailChimp 上取消订阅您的邮件列表的任何人也将在此处被删除。
在此阶段可能会出现问题。如果您有一个很大的列表(我只测试了 4 个),您可能会遇到内存问题,即 PHP 试图构建一个巨大的 $emails 数组。如果你遇到这个问题,你应该把阅读邮件分成小块,然后像这样发送邮件。
使用 PHP 发送批量电子邮件
其他人建议使用 Mandrill 发送批量电子邮件。这是个坏主意。 Mandrill 是 MailChimp 的姊妹服务,旨在发送 transactional email - MailChimp 用于批量电子邮件(如时事通讯)。
使用 PHP 发送电子邮件的方法有很多,我选择使用 Sendgrid 作为我的 SMTP 提供程序并使用 SwiftMailer 连接到它。其他替代方法是使用 PHP 的 mail() function 或其他库,如 PHPMailer。
您可以使用 Composer 安装 SwiftMailer:
$ composer require swiftmailer/swiftmailer @stable
我在this question 中详细介绍了 SwiftMailer 和 SMTP 服务(尽管上下文略有不同)。但是这个例子会做它需要做的。
$sendgridUser = /*SendGridUsername*/;
$sendgridPassword = /*SendGridPassword*/;
$subject = "Thank you for using MailChimp Lists!";
$fromAddress = "HPierce@example.com";
$fromName = "Hayden Pierce";
$body = file_get_contents(/*path to content (body.html)*/);
$transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587, 'tls')
->setUsername($sendgridUser)
->setPassword($sendgridPassword)
;
foreach($emails as $email){
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom(array($fromAddress => $fromName))
->setTo($email)
->setBody($body);
$mailer->send($message);
exit();
}
为简单起见,我从静态 HTML 文件中读取了整个正文。您可以考虑使用Twig 之类的模板引擎,以便更好地使用模板实现它。
所有这些代码放在一起看起来像这样:
//Loading in composer dependencies
require "vendor/autoload.php";
//Provided by Mailchimp account settings
$apiKey = /*MailChimp API keys*/;
$listId = /*MailChimp List id*/;
$sendgridUser = /*SendGridUser*/;
$sendgridPassword = /*SendGridPassword*/;
$subject = /*The subject line of your email*/;
$fromAddress = /*The email address for your FROM line*/;
$fromName = /*The name in your FROM line*/;
$body = file_get_contents(/*path to your html content*/);
$MailChimp = new \Drewm\MailChimp($apiKey);
$args = array(
'id' => $listId,
);
$result = $MailChimp->call('lists/members', $args);
//Check for any errors first, if none have occurred, build the email list.
if(isset($result['status']) && $result['status'] == 'error'){
throw new Exception('call to Mailchimp API has failed.');
} else {
$emails = array();
//Build an array of emails for users that are currently subscribed.
foreach($result['data'] as $recipient){
if($recipient['status'] == 'subscribed' && !empty($recipient['email'])){
$emails[] = $recipient['email'];
}
}
}
//Setup for sending emails to an arbitrary list of emails using Sendgrid.
$transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587, 'tls')
->setUsername($sendgridUser)
->setPassword($sendgridPassword)
;
foreach($emails as $email){
//Send emails to each user.
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom(array($fromAddress => $fromName))
->setTo($email)
->setBody($body);
$mailer->send($message);
}