简体   繁体   English

如何使用Mailgun API设置这些电子邮件标题

[英]How to set up these email headers with Mailgun API

I'm using curl with PHP to communicate with MailGun API. 我正在使用curl和PHP与MailGun API通信。 I have two emails which I need my recipients to receive the email with following headers. 我有两封电子邮件,我需要我的收件人来接收带有以下标题的电子邮件。 Could you give me an example code snippet in curl (or curl library for PHP) for achieve these mail headers? 您能否给我一个curl(或PHP的curl库)中的示例代码片段,以实现这些邮件标题?

-- Email 1 -- -电子邮件1-

From: "My Name"

Content-type: text/plain;charset="iso-8859-1"

Content-Transfer-Encoding: 8bit

-- Email 2 -- -电子邮件2-

MIME-Version: 1.0

From:my.domain.com <customerservice@mydomain.com>

Reply-To: my.domain.com <customerservice@mydomain.com>

Content-Type: multipart/alternative;boundary="=_730dc78ab764a3e997c2c451d9352d87"

Message-ID: <ndfmzn.jquf15@my.domain.com>

You can add as many headers as you want inside the array as long as they're prefixed with "h:" look at the line 您可以在数组内添加任意数量的标头,只要它们以“ h:”为前缀即可

'h:Message-Id'=>  'ndfmzn.jquf15@my.domain.com', //Your Message-ID

Below some larger explanation of how you can implement this: 下面是一些有关如何实现此功能的较大解释:

<?php
$mg_api = 'key-XXX'; //YOUR API KEY
$mg_version = 'api.mailgun.net/v2/'; //ROOT URL FOR MAILGUN
$mg_domain = "samples.mailgun.org"; //YOUR DOMAIN AS SPECIFIED IN THE CONTROL PANEL
$mg_from_email = "info@samples.com"; // YOUR FROM EMAIL
$mg_reply_to_email = "info@samples.org"; // YOUR REPLY-TO EMAIL (USUALLY MATCHES FROM EMAIL)

$mg_message_url = "https://".$mg_version.$mg_domain."/messages";


$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

curl_setopt ($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_VERBOSE, 0);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_USERPWD, 'api:' . $mg_api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_URL, $mg_message_url);
curl_setopt($ch, CURLOPT_POSTFIELDS,                
        array(  'from'      => 'you@domain.com',
                'to'        => 'receiver@receiver.com',
                'h:Reply-To'=>  ' <' . $mg_reply_to_email . '>',
                'h:Message-Id'=>  'ndfmzn.jquf15@my.domain.com', //Your Message-ID
                'subject'   => 'Hello',
                'html'      => 'Mailgun POW POW!'
            ));
$result = curl_exec($ch);
curl_close($ch);
$res = json_decode($result,TRUE);
print_r($res);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM