Vhosts: How to send email from PHP

This is a brief document giving an example of how to properly send email from PHP.

Here's an example of how to properly send email from a PHP script run on our web hosting platform so that it will bounce correctly in the case of an incorrect target address. A more complete example might perform some additional checks for things like MX DNS records or recipient validity on the target domain's servers (ie: sender address verification callback):

$to = 'someuser@somedomain';
$from = 'you@cae.wisc.edu';
$subject = 'An example message from PHP';
$msg = <<<MSG 
This is a test.
This is only a test.
if (!mail($to, $subject, $msg, "From: $from", "-f$from")) {
	print "ERROR: Failed to send mail!";

In general it's just the stock PHP mail() function. The important take away from this should be the "-f$from" section. This instructs the system who to bounce the message message to in the case that the target (someuser@somedomain in this example), doesn't exist. Your $from address must be a real address. In the absence of one, web-EDITING_GROUP@cae.wisc.edu will be used, which will redirect back to group/EDITING_GROUP@cae.wisc.edu.

Keywords:php email vhost vhosts   Doc ID:21445
Owner:Jeff B.Group:Computer-Aided Engineering
Created:2011-11-23 13:50 CDTUpdated:2017-06-22 04:52 CDT
Sites:Computer-Aided Engineering
Feedback:  2   0