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?
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.
Related
I am confused with here with $pass and $api_key
are they same, because at first $pass is assigned with password of the SendGrid's username but then api_key is assigned with $pass.
if they are same they where we would use that api_key that we generated on SendGrid?
Please Help!!!
<?php
$url = 'http://sendgrid.com/';
$user = 'USERNAME';
$pass = 'PASSWORD';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => 'example3#sendgrid.com',
'subject' => 'testing from curl',
'html' => 'testing body',
'text' => 'testing body',
'from' => 'example#sendgrid.com',
);
$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);
?>
There's a few issues with what you're trying to do:
You should never send an API call to sendgrid.com, only api.sendgrid.com
It looks like you're trying to use the v2 API with the Params. Since you're setting up a new script, you should use the v3 API which uses proper body JSON arguments, and is RESTful.
Instead of setting up your own script, have you looked at the SendGrid PHP Library?
For authentication in a script, you should always use an API Key, not your master username & password.
It looks like you got a really old example script for testing. Where did you get that from?
I've been researching lots of solutions in mail services for azure and I decided that the best solution for me is with phpMailer. I already tried to use the sendgrid API but I want to utilize the Ajax post method with jquery/javascript.
I have also found another solution that involves CURL. But the I'm getting an error on
Dotenv::load(__DIR__);
The error seems to be from sendGrids own php-files.
How do I solve any of these issues on azure.
The code I'm using is the following:
<?php
require 'vendor/autoload.php';
Dotenv::load(__DIR__);
$sendgrid_apikey = getenv('YOUR_SENDGRID_APIKEY');
$sendgrid = new SendGrid($sendgrid_apikey);
$url = 'https://api.sendgrid.com/';
$pass = $sendgrid_apikey;
$template_id = '<your_template_id>';
$js = array(
'sub' => array(':name' => array('Elmer')),
'filters' => array('templates' => array('settings' => array('enable' => 1, 'template_id' => $template_id)))
);
$params = array(
'to' => "test#example.com",
'toname' => "Example User",
'from' => "you#youremail.com",
'fromname' => "Your Name",
'subject' => "PHP Test",
'text' => "I'm text!",
'html' => "<strong>I'm HTML!</strong>",
'x-smtpapi' => json_encode($js),
);
$request = $url.'api/mail.send.json';
// Generate curl request
$session = curl_init($request);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $sendgrid_apikey));
// 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);
?>
Best wishes, Stanner
phpMailer is a mail server which need the PHP runtime enables the SMTP server, and it requires to enable the 25 port. Unfortunately, we don't have the permission to do this on Azure Web Apps.
I suggest you to use the 3rd part mail server to send emails from your applications on Azure Web Apps. On Azure, you can easily use SendGrid to satisfy this, please refer to https://azure.microsoft.com/en-us/documentation/articles/store-sendgrid-php-how-to-send-email/ for more info.
hello all please take a look at the below codes and help me to figure out the issue ..
$tos='soanm2#gmail.com';
$subject09='hello madam223';
$messageto90='this is a test';
$myName_emailis='Sonam verma';
$emaillist_act=array('sonam#gmail.com','sakshi#gmail.com');
$json_string = array( 'to' =>$emaillist_act,'category' => 'activity');
$url = 'https://api.sendgrid.com/';
$user = 'username';
$pass = 'password';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'to' => "$tos",
'subject' => "$subject09",
'html' => "$messageto90",
'fromname' => $myName_emailis,
'from' => "domain.com <contact#doamian.com>"
);
$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);
// Tell PHP not to use SSLv3 (instead opting for TLS)
#curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
print_r($response);
?>
This code works on other server http://www.reurl.in/6626fdd53
but it dosent do anything on my server http://www.reurl.in/26cbacc87
it dosent prints aything it is supposed to print the response success or fail etc..
This code works great on both the server.
<?php
//Your authentication key
$authKey = "somethingapikey";
//Multiple mobiles numbers separated by comma
$mobileNumber = "$mobileno";
//Sender ID,While using route4 sender id should be 6 characters long.
$senderId = "something";
//Your message to send, Add URL encoding here.
if(strlen($textsms) > 300){
$message = urlencode(substr($textsms,0,300));
$message=$message.'...'. ' www.domain.com';
}else{
$textsms=$textsms.' www.domain.com';
$message = urlencode($textsms);
}
//Define route
$route = "domain";
//Prepare you post parameters
$postData = array(
'authkey' => $authKey,
'mobiles' => $mobileNumber,
'message' => $message,
'sender' => $senderId,
'route' => $route
);
//API URL
$url="https://control.msg91.com/api/sendhttp.php";
// init the resource
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
));
//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//get response
$output = curl_exec($ch);
//Print error if any
//if(curl_errno($ch))
//{
// echo 'error:' . curl_error($ch);
//}
curl_close($ch);
//echo $output;
?>
Please help me narrow down the issue as this is very important for me please ...
My server admin says they cant help .
i also narrowed the issue we have curl version 7.38.0 and other server has 7.19.7
All the error reportings are on but still i am not getting anything no errors no success no mail.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
After adding the
$error = curl_error($session);
echo $error;
i get
Unsupported SSL protocol version
For quick fix, Add curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$tos='soanm2#gmail.com';
$subject09='hello madam223';
$messageto90='this is a test';
$myName_emailis='Sonam verma';
$emaillist_act=array('sonam#gmail.com','sakshi#gmail.com');
$json_string = array( 'to' =>$emaillist_act,'category' => 'activity');
$url = 'https://api.sendgrid.com/';
$user = 'username';
$pass = 'password';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'to' => "$tos",
'subject' => "$subject09",
'html' => "$messageto90",
'fromname' => $myName_emailis,
'from' => "domain.com <contact#doamian.com>"
);
$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_SSL_VERIFYPEER, false);
// Tell PHP not to use SSLv3 (instead opting for TLS)
#curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
print_r($response);
?>
Warning: Setting CURLOPT_SSL_VERIFYPEER to false allows for man-in-the-middle-attacks.
Another Solution
If your PHP installation doesn't have an up-to-date CA root certificate bundle, download the one at the curl website and save it on your server:
http://curl.haxx.se/docs/caextract.html
Then set a path to it in your php.ini file, e.g. on Windows:
curl.cainfo=c:\php\cacert.pem
Turning off CURLOPT_SSL_VERIFYPEER allows man in the middle (MITM) attacks, which you don't want!
Try to use a different SSL protocol version.
Change the line:
#curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
Try these possible constants to see which one works:
CURL_SSLVERSION_TLSv1
CURL_SSLVERSION_SSLv2
CURL_SSLVERSION_SSLv3
CURL_SSLVERSION_TLSv1_0
CURL_SSLVERSION_TLSv1_1
CURL_SSLVERSION_TLSv1_2
Also: Remove the # from the beginning of the line. The #suppresses errors. You do want to get errors when any occur.
If neither works, maybe support for the required ssl protocol version is not installed on the server. You would then need to update the CURL used or the OPENSSL used on the server.
I made an html form from which I submit the data to a php file for validation and after successful validation ,the php processes the data into an XML file.
Now i want to send this data to an email.Is there any way to do that.Dont tell me to mail directly through php,because i have tried all that with/without using SMTP(No success in that.)My website is deploed on Azure which has some SMTP issues.I tried to subscribe there Sendgrid service but it shows some billing errors.So is there any way to send mail through XML?
You can set up a sendgrid account in Azure marketplace, as Azure customers can unlock 25,000 free emails each month, so there will be less billing issues I think. To set up a sendgrid account in Azure, you can refer to How to Use the SendGrid Email Service from PHP, or you also can find this server in clicking “New” button and search “SendGrid” in search bar in Azure preview manage portal.
Here is a simple code example leveraging Web API to send an email with the data from xml, the PHP version should be upper than 5.5:
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<root>
<to>tomail#contoso.com</to>
<from>frommail#contoso.com</from>
<subject>Reminder</subject>
<html>Don't forget me this weekend!</html>
<text>text content</text>
</root>";
$xml = simplexml_load_string($myXMLData) or die("Error: Cannot create object");
print_r($xml);
$url = 'https://api.sendgrid.com/';
$user = 'username';
$pass = 'password';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => $xml->{'to'},
'subject' => $xml->{'subject'},
'html' => $xml->{'html'},
'text' => $xml->{'text'},
'from' => $xml->{'from'},
);
$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);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
// 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_SSLVERSION, 6);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
// print everything out
echo ($response);
I am sending email to all of my recents members who have not verified their emails by sndgrid email api
the process has a loop of about 56 members may be less also
here is the code
$getall_NOW_joins=mysqli_query($connection,"select name,email from members
where (some code) order by id desc limit 53");
while($theEMAILS=mysqli_fetch_array($getall_NOW_joins)){
$EMAILesee=$theEMAILS['email'];
$messageto90="<body>some html...... </body>"
$subject09="Verify email - domain";
$json_string = array( 'to' => array("$EMAILesee"),'category' => 'cron_rec_m');
$tos=$EMAILesee;
include_once('emailapi.php');
echo $response."<br/>";
}
the page emailapi contains
<?php
$url = 'https://api.sendgrid.com/';
$user = 'some';
$pass = 'thing';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'to' => "$tos",
'subject' => "$subject09",
'html' => "$messageto90",
'from' => "domain.com<contact#domain.com>",
);
$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);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// obtain response
$response = curl_exec($session);
curl_close($session);
?>
THE problem is the page sends only one email which is the first one in loop other emails are not sent please help me find the bug
I am using this page in cron job
It's probably because you're using include_once('emailapi.php');. Change it to include.