Metallic Skeleton
Metallic Skeleton

Reputation: 699

Email sending error by mailgun API in php

I am trying to send mail with mailgum API. As it shared server, I can't install composer( to load mailgun SDK). Instead, I am using curl. here is my code:

function send_simple_message() {  
$api_key = "key-9dc0bf ... 2e4c1007d0ebd7c8";
$domain=     "https://api.mailgun.net/v3/sandbox.mailgun.org";

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
 curl_setopt($ch, CURLOPT_USERPWD, $api_key);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
 curl_setopt($ch, CURLOPT_URL, $domain);
 curl_setopt($ch, CURLOPT_POSTFIELDS, 
            array('from' => 'Ruhul Amin <[email protected]>',
                  'to' => 'Michael Scott <[email protected]>',
                  'subject' => 'The Printer Caught Fire',
                  'text' => 'We have a problem.'));
 $result = curl_exec($ch);
 curl_close($ch);
 return $result;
}

var_dump ( send_simple_message() );

?>

Output is: bool(false)

But not email is sent. Could you please tell me what is my problem. Thanks.

Ruhul

Upvotes: 0

Views: 1030

Answers (3)

Mukesh Tivari
Mukesh Tivari

Reputation: 334

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => $mailgun_api_url,
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_USERPWD => "api:{$mailgun_api_key}",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_SSL_VERIFYPEER => 0,
    CURLOPT_SSL_VERIFYHOST => 0,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        "from" => "[email protected]",
        "to" => "[email protected]",
        "subject" => "subject of mail",
        "text" => "description/content of the mail."
    ]
]);
$res = curl_exec($ch);
curl_close($ch);

make sure that $mailgun_api_url and $mailgun_api_key should be correct and check the $res it should be json like below

{
    "id": "<[email protected]>",
    "message": "Queued. Thank you."
}

the message is send if the receiving email id is correct.

Upvotes: 0

Metallic Skeleton
Metallic Skeleton

Reputation: 699

i solved the problem. I am posting here, hope it will help some one. Actually, CURLOPT_USERPWD - user name and password to use in authentication So, In my case, the user name is : api and user password is the API-key I need to merge the same while defining the $api-key

function send_simple_message() {  
$api_key = "api:key-9dc0bf ... 2e4c1007d0ebd7c8";
$domain=     "https://api.mailgun.net/v3/sandbox.mailgun.org";

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
 curl_setopt($ch, CURLOPT_USERPWD, $api_key);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
 curl_setopt($ch, CURLOPT_URL, $domain);
 curl_setopt($ch, CURLOPT_POSTFIELDS, 
        array('from' => 'Ruhul Amin <[email protected]>',
              'to' => 'Michael Scott <[email protected]>',
              'subject' => 'The Printer Caught Fire',
              'text' => 'We have a problem.'));
 $result = curl_exec($ch);
 curl_close($ch);
 return $result;
}

var_dump ( send_simple_message() );

Upvotes: 1

zeulus
zeulus

Reputation: 44

If curl_exec returns false, it means there was an error. You can fetch the error message with curl_error function. See the output of the curl_error function and you will be closer to have it working. Example code:

$result = curl_exec($ch);
if ($result === false)
{
    $error = curl_error($ch);
    // do something with the error
}

Upvotes: 0

Related Questions