简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM