0
votes

I am working on a PHP script to send emails out to users from a site which is hosted on Azure. I'm using SendGrid, with the following code:

$url = 'https://api.sendgrid.com/';
$user = 'username';
$pass = 'password'; 

$params = array(
    'api_user' => $user,
    'api_key' => $pass,
     'to' => $email,
     'subject' => "Bid Submission",
     'html' => "",
     'text' => "y",
     'from' => $emailFrom,
 );

 $request = $url.'api/mail.send.json';

 // Generate curl request
 $session = curl_init($request);

 // Tell curl to use HTTP POST
 curl_setopt ($session, CURLOPT_POST, true);

 // Tell curl that this is the body of the POST
 curl_setopt ($session, CURLOPT_POSTFIELDS, $params);

 // Tell curl not to return headers, but do return the response
 curl_setopt($session, CURLOPT_HEADER, false);
 curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

 // obtain response
 $response = curl_exec($session);
 curl_close($session);

 // print everything out
 print_r($response);

However the above code does not run and no email is sent. I've already setup the SendGrid account, and the provided username and my own password is being used. Can anybody tell me, what I'm doing wrong?

1

1 Answers

0
votes

By default, Azure doesn't generate a SSL peer certificate for Azure Web Apps. If you add the code in your test code $error = curl_error($session);, you will get the error SSL certificate problem: self signed certificate in certificate chain.

You can simply add the code curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0); to bypass the verification.

Otherwise, you can follow Verify Peer Certificate from PHP cURL for Azure Apps to add a certificate on Azure Web Apps.