Sending Email with Mailgun via HTTP Request using Curl

孤街醉人 提交于 2020-01-05 04:18:11

问题


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:


I was able to solve the issue.

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);



 ?>


来源:https://stackoverflow.com/questions/38005563/sending-email-with-mailgun-via-http-request-using-curl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!