simplecoder
simplecoder

Reputation: 69

Sending Email with Mailgun via HTTP Request using Curl

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 <[email protected]>',
    'to'      => '[email protected]',
    '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);

 ?>

Upvotes: 1

Views: 5368

Answers (1)

simplecoder
simplecoder

Reputation: 69

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 <[email protected]>',
    'to'      => '[email protected]',
    '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);



 ?>

Upvotes: 3

Related Questions