Sending a plain text email from a PHP script

From Knowledge Kitchen
Jump to navigation Jump to search


Basic example

This example uses PHP's mail() function to send an email to one recipient.

<?php

//who to send the email to
$to = 'rodrigo@mailinator.com';

//the subject of the email
$subject = 'Congratulations on a job well done!';

//the body of the email message
$message = <<<END
Dear Rodrigo,
I'd just like to congratulate you on being an excellent teacher.  I hear your work in web development is yet to be surpassed.

Love,
President Barack Obama

END;

//additional SMTP headers to indicate who the email is from
//.. and you can include any additional headers you might need
$headers = <<<END
From: President Barack Obama <obama@whitehouse.gov>
Reply-To: no-reply@whitehouse.gov
END;


//send the email
mail($to, $subject, $message, $headers);	

?> 

Sending an email from a form on a website

This same basic mechanism, combined with an understanding of how to send data from client to server, can be used used to create a typical "Contact Us" page on a website.

Send an email to multiple recipients from a PHP script

If you understand how to create and loop through arrays, it's easy enough to create a script that shoots out emails to multiple recipients.

This example uses PHP's mail() function to send a single email to a large list of recipients. This same technique can be used to send newsletters or other email marketing.

<?php

//in a mailing list, the recipients are often stored in an array
$to = array(
	'rodrigo@mailinator.com',
	'bob@mailinator.com',
	'jane@mailinator.com'
);

$subject = 'Congratulations on a job well done!';

$message = <<<END
Dear Precious Teacher,
I'd just like to congratulate you on being an excellent teacher.  I hear your work in web development is yet to be surpassed.

Love,
President Barack Obama

END;

$headers = <<<END
From: President Barack Obama <obama@whitehouse.gov>
Reply-To: no-reply@whitehouse.gov
END;


//loop through the array of recipients
for ($i=0; $i<sizeof($to); $i++) {
	//send an email to each recipient
	mail($to[$i], $subject, $message, $headers);	
}

?>

How to make a spam bot, in case you were wondering

This example shows how to create spam by automatically generating email addresses to send an email to.

DON'T DO THIS AT HOME

<?php

//in a spam bot, the recipient address is generated on the fly

$subject = 'Congratulations on a job well done!';

$message = <<<END
Dear Valued Educator,
I'd just like to congratulate you on being an excellent teacher.  I hear your work in web development is yet to be surpassed.

Love,
President Barack Obama

END;

$headers = <<<END
From: President Barack Obama <obama@whitehouse.gov>
Reply-To: no-reply@whitehouse.gov
END;


//looping a set number of iterations
for ($i=0; $i<1000; $i++) {
	//generate the recipient email address on the fly
	$to = "ab" . $i . "@mailinator.com";

	//send an email to each recipient
	mail($to, $subject, $message, $headers);	
}

?>


Send an HTML email

To send an email with HTML code in it and have it render correctly in the recipient's email client, you will need to include an indication of the use of HTML in the Content-Type header of the email.


What links here