I'm basically going through Sendgrid's example for sending an email to duplicate email addresses but getting strange results. I receive duplicate emails (4 of them) every time. I've looked through several threads and almost all have been resolved by a duplicate request. However, from what I can see there are none (and again, this is basically the example script from Sendgrid). Any help would be greatly appreciated...
Code:
$url = 'https://api.sendgrid.com/';
$user = 'My Username';
$pass = 'My Password';
$json_string = array(
'to' => array(
'myemail#example.com', 'myemail#gmail.com'
),
'category' => 'test_category'
);
$params = array(
'api_user' => $user,
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'to' => 'example3#sendgrid.com',
'subject' => 'testing from curl',
'html' => 'testing body',
'text' => 'testing body',
'from' => 'senderemail#example.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 everything out
print_r($response);
The printout says success (only one time). The email is delivered fine. The only problem is that 4 emails show up to every emailbox.
If you're running this through a browser, it's likely your have plugins that are causing repeat submissions - which will look like it's only happening once from your browser's point of view. Web logs will also show you if this is happening. Try calling it from curl on a command line to guarantee that it's only submitting the request once.
Related
I am using sendgrid for quite a long time and it was working fine. But since yesterday when I am sending email with more than 10000 characters then starting part of email is appending at last of email but with less than 10K characters its absolutely fine.
I tested my source code its absolutely fine and even as I said earlier it was working fine. I tested with php mail() method as well and its working fine with mail() method
here is my code for sendgrid api.
$from = "bp#xxxxxxxx.com.au";
$headers = 'From: <'.$from.'>' . "\r\n";
$url = 'https://api.sendgrid.com/';
$user = 'xxxxxxxxx';
$pass = 'xxxxxxxxxx';
$ToEmailArray = explode(",",$toEmail);
$json_string = array('to' => $ToEmailArray);
$params = array(
'api_user' => $user,
'api_key' => $pass,
'x-smtpapi' => json_encode($json_string),
'to' => '',
'subject' => $subject,
'html' =>$message,
'from' => $from
);
$request = $url.'api/mail.send.json';
$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
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);
// blinding the CV https certificates
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
// obtain response
$response = curl_exec($session);
//$contents['response'] = $response;
curl_close($session);
I talk to sendgrid customer support as well but they are saying there is nothing wrong with their API.
Can someone help me in this.
Thanks.
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 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.