Send xml data to an email - php

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

Related

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?

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.

how to implement push notification using GCM in android studio using php?

I am trying to get notification in my app using GCM. i am using android studio tool and i tried lots of tutorials. most of them where done using eclips tool.
finally, i tried this one,
http://rmarcejaeger.com/2015/09/18/tutorial-how-to-implement-push-notifications-using-google-cloud-messaging-for-android-part-1-client-app/#comment-576070,
https://github.com/googlesamples/google-services/tree/master/android/gcm
and i finish android client part using above tutorial. But the problem is, i need to setup app server in php which connect my application through GCM. i have no idea to make it possible. i tried the follwing link,
https://developers.google.com/cloud-messaging/server
but i couldn't get much more.So please anyone suggest me better solution.
you need to understand below steps
for more details Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL
First you need to create browser key from Console.
This is the code you need to add for Sending notification GOOGLE_API_KEY is the key you created from Console.
<?php
class GCM {
//put your code here
// constructor
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// include config
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
}
?>
Now using this method of GCM class you can send notification to the device by calling its method.
pass reg.id that is token id you got from Android device and message you want to pass.
$gcm = new GCM();
$result = $gcm->send_notification($registatoin_ids, $message);
echo $result;

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