使用Curl通过HTTP请求使用Mailgun发送电子邮件

时间:2016-06-24 04:22:18

标签: api http postman mailgun

我遇到一些问题,运行api通过Mail Gun发送带附件的电子邮件。我试图通过邮递员运行api,它运行得非常好所以我知道api没有问题,但我无法发送通过代码请求。

我在下面添加了一个代码示例:

<?php
$curl_post_data=array(
    'from'    => 'Excited User <noreply@test.com>',
    'to'      => 'test@gmail.com',
    'subject' => 'Hello',
    'text'    => 'test'   
//,'attachment[1]' => '@https://www.smsglobal.com/docs/HTTP-2WAY.pdf'
,array( 'attachment' => 'https://www.smsglobal.com/docs/HTTP-2WAY.pdf')
);

$service_url = 'https://api.mailgun.net/v3/test.com/messages';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:key-testtesttesttes"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 

$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);

var_dump($response);

 ?>

1 个答案:

答案 0 :(得分:3)

我能够解决这个问题。

我的发现: 1-)您无法提供实时网址,因为附件http://test.com/images/logo.png无效。 2-)只能给出服务器上托管的文件的地址 /var/images/logo.png

<?php

$filePath='@0Wealth_AC_AMF.pdf';

$curl_post_data=array(
    'from'    => 'Excited User <noreply@test.com>',
    'to'      => 'test@gmail.com',
    'subject' => 'Hello',
    'text'    => 'test',
'attachment[1]' => $filePath
);

$service_url = 'https://api.mailgun.net/v3/test.com/messages';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:key-test"); 

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 


$curl_response = curl_exec($curl);  
$response = json_decode($curl_response);
curl_close($curl);

var_dump($response);



 ?>