Send Email with SendGrid using Web Api php - php

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?

Related

Sendgrid example php sending duplicate emails

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.

How to install phpMailer on azure Web App

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.

Using sendgrid PHP on microsoft Azure

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.

Send mail using send grid api key

I'm using send grid for sending mails.
This is the script I used.
$url = 'https://api.sendgrid.com/';
$params = array(
'api_user' => 'xxx', // My send grid username
'api_key' => xxx', // My send grid password
'to' => tomail,
'subject' => 'sub',
'html' => 'message',
'from' => frommail,
);
$request = $url.'api/mail.send.json';
$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
It's working fine and sending mails successfully.
I wan't to use send grid api key for sending mails without using the password.
I generated it from 'app.sendgrid.com/settings/api_keys' got api key id and long key.
How can I use this keys from web api call. I'm replacing api_user and api_keys with newly generated api key name and key but mails are not sending.
I ran into this today as well. To add to the answer provided by #bwest:
$pass = 'your api token' // not the key, but the token
$url = 'https://api.sendgrid.com/';
//remove the user and password params - no longer needed
$params = array(
'to' => tomail,
'subject' => 'sub',
'html' => 'message',
'from' => frommail,
);
$request = $url.'api/mail.send.json';
$headr = array();
// set authorization header
$headr[] = 'Authorization: Bearer '.$pass;
$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// add authorization header
curl_setopt($session, CURLOPT_HTTPHEADER,$headr);
$response = curl_exec($session);
curl_close($session);
To send with an API key, you need to add an Authorization header to your request. It is an HTTP Basic auth header, meaning it is in the format username:password, base64 encoded, as a bearer token. You can see an example in the docs.
I use the Send Grid php library where I can use username and password OR just the API Key.
Try removing the api_user line and using the api key with api_key. It should work.
For more info: https://sendgrid.com/docs/Integrate/Code_Examples/php.html
Using the API key with the Autorization: Bearer key header works.

emails by sendgrid in a loop sending only one

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.

Categories