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

问题描述:

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

I am having some issue running the api to send email with attachment via Mail Gun.I tried to run the api via postman and it runs perfectly fine so i know there is no problem with the api but i am unable to send a request via code.

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

I have added a code sample below:

<?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-) 您不能提供实时网址作为附件 http://test.com/images/logo.png 将不起作用.2-) 只能提供托管在服务器上的文件的地址/var/images/logo.png

My Findings: 1-) You cannot give a live url address as attachment http://test.com/images/logo.png will not work. 2-) only address of files hosted on the server can be given /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);



 ?>