通过PHP将Mailchimp简报发送到List

时间:2015-07-10 13:33:24

标签: php email mailchimp

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

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

5 个答案:

答案 0 :(得分: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 :(得分: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 :(得分:1)

您的问题分为两个部分:

  1. 如何从MailChimp中获取我的电子邮件列表?
  2. 如何将电子邮件发送到任意电子邮件地址?
  3. 第一块是这里最重要的。第二个块有一个 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密钥,但简短版本为:

      
        
    1. 点击您的个人资料名称以展开“帐户面板”,然后选择   帐户。

    2.   
    3. 点击其他下拉菜单,然后选择API密钥。

    4.   
    5. 复制现有API密钥或单击“创建密钥”按钮。

    6.   
    7. 描述性地命名您的密钥,以便您知道应用程序使用该密钥   键。

    8.   

    接下来,您需要列表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);
    }
    

答案 3 :(得分:0)

MailChimp API的

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

答案 4 :(得分:0)

使用自定义HTML

制作广告系列

使用广告系列/创建API端点: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