通过PHP将Mailchimp简报发送到List

问题描述 投票:17回答:5

有没有办法从.php页面发送自行设计的HTML和CSS电子邮件到Mailchimp列表?我想通过我自己的新闻稿模板将新闻稿功能集成到管理面板,然后从那里发送。

每次我想发送电子邮件时,我都不想登录Mailchimp,特别是因为模板每次都会相同。

php email mailchimp
5个回答
2
投票

如果您不想将模板上传到Mailchimp并通过点击他们的API发送活动,Mandrill(如评论中上面提到的@Whymarrh)可能是一个不错的选择。

虽然它适用于交易电子邮件(欢迎,密码恢复等),但您可以通过SMTP一次向最多1000个用户发送邮件。此外,您可以将您的Mailchimp帐户连接到“集成”部分中的Mandrill帐户,以跟踪收件人活动。

我的建议是安装Mandrill PHP API客户端,将模板上传到Mandrill,点击用户列表的Mailchimp API,然后将其输入您通过管理面板触发的Mandrill send-template call。 (关于发送大量电子邮件的专业提示:Mandrill sending to multiple people as separate message via the REST API)。


2
投票

是的你可以。 MailChimp的详细信息和示例可通过登录其控制面板获得。使用他们的表单字段,设置自己的表单样式。

<form action='http://xxxx.xxxxlist-manage.com/subscribe' method='post'>
    <p><input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="enter email address"></p>
    <p><input type="submit" value="Sign Up" name="subscribe" id="mc-embedded-subscribe" class="btn"></p>
    <input type='hidden' name='u' value='xxxxxxx'>
    <input type='hidden' name='id' value='xxxxxxx'>
</form>

1
投票

您的问题分为两个部分:

  1. 如何从MailChimp中获取我的电子邮件列表?
  2. 如何将电子邮件发送到任意电子邮件地址?

第一块是这里最重要的。第二个块有很多可能的答案,应该很容易实现。


从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密钥,但短版本是这样的:

  1. 单击您的配置文件名称以展开“帐户面板”,然后选择“帐户”
  2. 单击Extras下拉菜单,然后选择API密钥。
  3. 复制现有API密钥或单击“创建密钥”按钮。
  4. 描述性地命名您的密钥,以便您知道哪个应用程序使用该密钥。

接下来,您需要列表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 = "[email protected]";
$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);
}

0
投票

MailChimp API的v2.0(已弃用)具有Campaign CreationCampaign Send方法。这些并不是最简单的方法,但是目前的API(v3.0)还没有它们,所以这是你最好的选择。


0
投票

使用自定义HTML创建广告系列

使用campaign / create api endpoint:https://apidocs.mailchimp.com/api/2.0/campaigns/create.php

PHP包装器在这里:https://bitbucket.org/mailchimp/mailchimp-api-php

看来Mailchimp_Campaigns :: create是你可以使用的功能。密切关注$ content参数(原始/粘贴HTML内容的html字符串)

创建广告系列后,您将获得该ID。

发送已创建的广告系列

使用函数Mailchimp_Campaigns :: send与先前创建的广告系列的ID

© www.soinside.com 2019 - 2024. All rights reserved.