【问题标题】:Simple php function to send an email with Mandrill使用 Mandrill 发送电子邮件的简单 php 函数
【发布时间】:2013-01-06 13:42:07
【问题描述】:

通过 Mailchimp 的 Mandrill 服务(使用 API)发送电子邮件的最简单方法是什么。

这里是发送方法:https://mandrillapp.com/api/docs/messages.html#method=send

这是 API 包装器:https://bitbucket.org/mailchimp/mandrill-api-php/src/fe07e22a703314a51f1ab0804018ed32286a9504/src?at=master

但我不知道如何制作一个通过 Mandrill 发送和发送电子邮件的 PHP 函数。

谁能帮忙?

【问题讨论】:

    标签: php mailchimp mandrill


    【解决方案1】:

    我们还有一个官方的 PHP API 包装器,可通过 on Bitbucket 或通过 Packagist 获得,它为您包装了 Mandrill API。

    如果您的 Mandrill API 密钥存储为环境变量,下面是一个使用模板发送的简单示例,其中包含一些合并变量和元数据:

    <?php
    require 'Mandrill.php';
    
    $mandrill = new Mandrill(); 
    
    // If are not using environment variables to specific your API key, use:
    // $mandrill = new Mandrill("YOUR_API_KEY")
    
    $message = array(
        'subject' => 'Test message',
        'from_email' => 'you@yourdomain.com',
        'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>',
        'to' => array(array('email' => 'recipient1@domain.com', 'name' => 'Recipient 1')),
        'merge_vars' => array(array(
            'rcpt' => 'recipient1@domain.com',
            'vars' =>
            array(
                array(
                    'name' => 'FIRSTNAME',
                    'content' => 'Recipient 1 first name'),
                array(
                    'name' => 'LASTNAME',
                    'content' => 'Last name')
        ))));
    
    $template_name = 'Stationary';
    
    $template_content = array(
        array(
            'name' => 'main',
            'content' => 'Hi *|FIRSTNAME|* *|LASTNAME|*, thanks for signing up.'),
        array(
            'name' => 'footer',
            'content' => 'Copyright 2012.')
    
    );
    
    print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));
    
    ?>
    

    【讨论】:

    • 作为对未来用户的说明,您需要在 Outbound -> 模板菜单下的 Mandrill 中创建一个模板,其最小 html 如下:
      模板 slug 必须匹配 Stationary。关于模板的更多信息可以在这里找到help.mandrill.com/entries/…
    【解决方案2】:

    Mandrill 为所有 API 方法接受 HTTP POST 请求,并将您的输入作为 JSON 字符串。这是发送电子邮件的基本示例。它使用cURL 进行HTTP 请求:

    $uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
    
    $postString = '{
    "key": "YOUR KEY HERE",
    "message": {
        "html": "this is the emails html content",
        "text": "this is the emails text content",
        "subject": "this is the subject",
        "from_email": "someone@example.com",
        "from_name": "John",
        "to": [
            {
                "email": "blah@example.com",
                "name": "Bob"
            }
        ],
        "headers": {
    
        },
        "track_opens": true,
        "track_clicks": true,
        "auto_text": true,
        "url_strip_qs": true,
        "preserve_recipients": true,
    
        "merge": true,
        "global_merge_vars": [
    
        ],
        "merge_vars": [
    
        ],
        "tags": [
    
        ],
        "google_analytics_domains": [
    
        ],
        "google_analytics_campaign": "...",
        "metadata": [
    
        ],
        "recipient_metadata": [
    
        ],
        "attachments": [
    
        ]
    },
    "async": false
    }';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $uri);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
    
    $result = curl_exec($ch);
    
    echo $result;
    

    【讨论】:

    • 这段代码运行良好。但无法渲染 html 文件来发送邮件。你能帮助先生
    【解决方案3】:
    // Simply Send Email Via Mandrill...
    
    require_once 'Mandrill.php';
    $mandrill = new Mandrill($apikey);
    
    $message = new stdClass();
    $message->html = "html message";
    $message->text = "text body";
    $message->subject = "email subject";
    $message->from_email = "address@test.com";
    $message->from_name  = "From Name";
    $message->to = array(array("email" => "recipient@test.com"));
    $message->track_opens = true;
    
    $response = $mandrill->messages->send($message);
    

    【讨论】:

      【解决方案4】:

      这是我能给你的最基本的代码,我只是在几秒钟前为客户制作的,它运行顺利。

      require_once 'path/to/your/mandrill/file/Mandrill.php';
       try {
          $mandrill = new Mandrill('your-API-key');
          $message = array(
              'html' => $htmlMessage,
              'subject' => $subject,
              'from_email' => $fromEmail,
              'from_name' => $fromName,
              'to' => array(
                  array(
                      'email' => $toEmail,
                      'name' =>  $toName,
                      'type' => 'to'
                  )
              )
          );
          $result = $mandrill->messages->send($message);
          print_r($result);
      } catch(Mandrill_Error $e) {
          echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
          throw $e;
      }
      

      还可以查看他们的发送方法以获取更多选项,例如标题、元数据、附件等。https://mandrillapp.com/api/docs/messages.php.html#method-send

      【讨论】:

        【解决方案5】:

        【讨论】:

          猜你喜欢
          • 2014-05-04
          • 2013-06-22
          • 1970-01-01
          • 2013-10-13
          • 2017-04-30
          • 2016-02-28
          • 2023-03-28
          • 2014-10-18
          相关资源
          最近更新 更多