Mail in PHP

I am listing here the method for sending mail using PHP code. This will be useful in many ways, for example when you want to sent mail after say a form is submitted.

Note: This may work in local , but I am suggesting you to execute on live server.


<?php

ini_set("SMTP","mail.yoursitename.com"); //this is in general case, put here the SMTP address.
ini_set("SMTP_PORT",25); //this is default SMTP port

$to="recipient@abc.com"; //recipient's email address.
$from="sender@efg.com"; //sender's email address.
$subject = "Subject of email"; //email subject.
$headers = 'From: ' . $from . "\r\n" .
			'Reply-To: ' . $from . "\r\n" .
			'X-Mailer: PHP/' . phpversion(); //set headers
$headers.="\r\nMime-Version: 1.0\r\n";
$headers.="Content-type: text/HTML\r\n"; //content type of email, can be simple text

$Body="<b>U have won a Honda CBR</b>"; //content of email, can be html content					

if(mail($to, $subject, $Body, $headers)) //sent mail
{
	echo 'Mail Sent'; //do something after a mail is sent
}

?>

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top