0

I am trying to send mail from localhost using Sendgrid API integration, with PHP and Curl. When I run the code below nothing happens (no email and no error/ message):

<?php

$email = "****@mail.com";
$name = "Miguel Martins";
$body = "Miguel is the king. <br><br><a href='https://google.com'>Google</a>";
$subject = "Teste Email";

$headers = array(
    'Authorization: Bearer SG.gfgffgfgfdrgreeegv.lX-fgfgegehynte-frdvdyvryryhavaryuyyuyy',
    'Content-Type: application/json'
);

$data = array(
    "personalizations" => array(
        array(
            "to" => array(
                array(
                    "email" => $email,
                    "name" => $name
                )
            )
        )
    ),
    "from" => array(
        "email" => "****@mail.com"
    ),
    "subject" => $subject,
    "content" => array(
        array(
            "type" => "text/html",
            "value" => $body
        )
    )
);

$ch = curl_init();
curl_setopt($ch, CURLINFO_HEADER_OUT, true); // enable tracking
curl_setopt($ch, CURLOPT_URL, "https://api.sendgrid.com/v3/mail/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT); // request headers
curl_close($ch);

echo $response;

?>

What am I missing?

Thanks for your help.

SOLUTION:

Finally found what was the problem here. Cacert.pem was missing and mod_ssl wasn't enabled in Apache's httpd.conf . Found the solution here: cURL error 60: SSL certificate: unable to get local issuer certificate

DoukeN
  • 33
  • 7
  • have you got error logging switched on in PHP? Also, what response content did you expect from the sendgrid API exactly? Have you also checked the response headers (e.g. status code etc) as well as just the body? I mention this last point because the example at the end of https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html suggests the response body would normally be empty. It would therefore make sense to check the headers too. – ADyson Oct 26 '20 at 23:23
  • P.S. I hope that auth code is temporary, otherwise you just gave world+dog access to your API account. – ADyson Oct 26 '20 at 23:24
  • ADYSon, thanks for your answer. I expect it to send an email (it is the code for a "submit" button" that email a form's info.) PS: it is a fake auth. :) – DoukeN Oct 26 '20 at 23:38
  • Ok. So, if the email isn't being received (note that this isn't necessarily the same as the email not having been sent!), have you checked the things I mentioned above? – ADyson Oct 26 '20 at 23:41
  • We need to know if the blank output is because PHP threw an error, or whether the HTTP request returned an error status code. Or it might simply be that the request was sent successfully to sendgrid, and you need to check its status (see https://sendgrid.com/docs/ui/analytics-and-reporting/email-activity-feed/) there to see if it's been delivered yet (and if so, then you need to check the recipient's junk mail, or see if your domain has been blacklisted or something) – ADyson Oct 26 '20 at 23:44
  • I turned PHP error log on and no errors are shown. I also checked sendgrid activity and there is no activity to show... – DoukeN Oct 27 '20 at 23:21
  • I am trying to send through gmail. Do I have to configure something on gmail? – DoukeN Oct 28 '20 at 00:18
  • If no activity is shown in sendgrid it suggests maybe your curl request failed. Did you check the status code returned from that request? Check the response headers in curl – ADyson Oct 28 '20 at 00:35
  • How can I check the response headers? – DoukeN Oct 28 '20 at 07:55
  • It's quite easy to google that. But basically you need to use this function: https://www.php.net/manual/en/function.curl-getinfo.php – ADyson Oct 28 '20 at 09:14
  • Ok I changed my code to request headers, but nothing happens (please see above the edited post/code). What am I doing wrong? Thanks in advance for all your help. – DoukeN Oct 29 '20 at 08:38
  • It's the **response** headers you need to get, not the request headers. (You already know what the request headers are, because it's your cURL code which created them!!). The main item you should be interested is CURLINFO_RESPONSE_CODE. And also you can get cURL to output the response headers as per this post: https://stackoverflow.com/a/9183272/5947043 – ADyson Oct 29 '20 at 09:41
  • Added this and still a blank page: $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); // ... $response = curl_exec($ch); // Then, after your curl_exec call: $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($response, 0, $header_size); – DoukeN Oct 30 '20 at 22:55
  • Well did you echo any of those values? I don't see any echo in that code. – ADyson Oct 30 '20 at 23:46
  • I echoed $response", $header_size, $header and $body. The response was "0". – DoukeN Oct 31 '20 at 09:48
  • 1
    Finally found what was the problem here. Cacert.pem was missing and mod_ssl wasn't enabled in Apache's httpd.conf . Found the solution here: https://stackoverflow.com/questions/29822686/curl-error-60-ssl-certificate-unable-to-get-local-issuer-certificate – DoukeN Nov 07 '20 at 20:01
  • If you found a solution please add it below as an answer with an explanation of exactly what you changed. It might be useful for future readers with a similar issue. Glad you fixed it. – ADyson Nov 07 '20 at 23:11

1 Answers1

0

Finally found what was the problem here. Cacert.pem was missing and mod_ssl wasn't enabled in Apache's httpd.conf . Found the solution here: cURL error 60: SSL certificate: unable to get local issuer certificate

DoukeN
  • 33
  • 7