dongyi1015 2014-10-14 13:05
浏览 82

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

I'm using curl with PHP to communicate with 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?

-- Email 1 --

From: "My Name"

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

Content-Transfer-Encoding: 8bit

-- Email 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>
  • 写回答

1条回答 默认 最新

  • doumou3883 2014-10-19 20:11
    关注

    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: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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决