How to install phpMailer on azure Web App - php

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.

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.

Send Email with SendGrid using Web Api 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?

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.

"Unsupported SSL protocol version" when trying to call SendGrid API

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.

Send xml data to an email

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);

Categories