Friday, February 18, 2011

Send E-mail with HTML in it using AS3 and PHP

Today we will learn how to send HTML formatted letter to someone's email using AS3 and PHP.

We'll be using the code from the previous AS3-PHP tutorial, but with a few changes.

Firstly, let's create the html text we want to send. You can use any HTML tags you want. The example below will send an email with bold, italic text and an embedded picture.

var myData:URLVariables = new URLVariables();
myData.myText = "<html><body> <b>Bold</b> <i>Italic</i> <img src="http://mysite.com/myimage.jpg"> </body></html>";

var myRequest:URLRequest = new URLRequest("http://mysite.com/test.php");

myRequest.data = myData;
myRequest.method = URLRequestMethod.POST;

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;

try
{
loader.load(myRequest);
}
catch (error:Error)
{
trace('Error: unable to load the document.');
}

Now, the PHP code on the server:

<?php
$text=$_POST['myText'];
$to="mymail@hotmail.com";
$subject="Message from php";
mail($to,$subject,$text,"Content-Type: text/html; charset=utf-8");
?>

The important thing in the code above is the Content-Type part, it has to have "text/html" as the type.

And that's how you do it.

Thanks for reading!

2 comments:

Anonymous said...

Many thanks! Just what I needed.

Albert Andrada said...

Really Nice Information,Thank You Very Much For Sharing.
Wordpress Development Company

Post a Comment