Home Blog Archive Single Blog

How to send emails with PHP

how can I easily send emails with a simple script?

There is a PHP function that lets you do just that:

mail('mailto@gmail.com',$subject,$content,$headers);

The structure of the php function is pretty simple. The fun part is that using variables, we can write an email in html format: we do that by specifying in the headers that we want our email viewer to view the message as an html document

$headers = "From: sent_from@samplemail.com \r\n";
$headers .= "Reply-To: reply@sample.com \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

Then use this variable as the header in our mail function, which is the last parameter; in the example above, we've called it "$headers".

We can go ahead now and do the same for the body of the email: for example:


$content = '<html><body>';<br>
$content .= '<img src="http://www.yoursite.com/logo.png" alt="logo" />';<br>
$content .= '<br><H1> This is a test email "</H1><BR><BR><BR>' ;<br>
$content .= '<p>Hello world! </p><br>';<br>
$content .= '</body></html>';<br>

As the subject, we can put any text as a variable:

$subject: "test mail";

This is what the final php script should look like:


<?php  
					 
					 

$headers = "From: sent_from@samplemail.com \r\n";<br>
$headers .= "Reply-To:  reply@sample.com \r\n";<br>
$headers .= "MIME-Version: 1.0\r\n";<br>
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
					
					
$content = '<html><body>';
$content .= '<img src="http://www.yoursite.com/logo.png" alt="logo" />';
$content .= '<br><H1> This is a test email "</H1><BR><BR><BR>' ;
$content .= '<p>Hello world! </p><br>';
$content .= '</body></html>';

$subject: "test mail";					

mail('mailto@gmail.com',$subject,$content,$headers);

?> 

YOU MAY ALSO LIKE

03 COMMENTS

Shuhein Chui, Nov 21, 2017

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.

LIKE REPLY
Shuhein Chui, Nov 21, 2017

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.

LIKE REPLY
Shuhein Chui, Nov 21, 2017

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam.

LIKE REPLY

LEAVE A COMMENT

  • 1 BTC = $13,2323
  • 1 BCH = $13,2323
  • 1 ETH = $13,2323
  • 1 LTC = $13,2323
  • 1 DAS = $13,2323
  • 1 BCC = $13,2323

NEWSLETTER

Subscribe to our newsletter to get notification about new updates, information, discount, etc..