EN49

Page 339

can be used to set the envelope sender address when using sendmail/postďŹ x with the -f sendmail option. $fifth

= '-fno-reply@example.com';

Although using mail() can be pretty reliable, it is by no means guaranteed that an email will be sent when mail() is called. To see if there is a potential error when sending your email, you should capture the return value from mail(). TRUE will be returned if the mail was successfully accepted for delivery. Otherwise, you will receive FALSE. $result = mail($to, $subject, $message, $headers, $fifth);

NOTE: Although mail() may return TRUE, it does not mean the email was sent or that the email will be received by the recipient. It only indicates the mail was successfully handed over to your system's mail system successfully. If you wish to send an HTML email, there isn't a lot more work you need to do. You need to: 1. Add the MIME-Version header 2. Add the Content-Type header 3. Make sure your email content is HTML $to = 'recipient@example.com'; $subject = 'Email Subject'; $message = '<html><body>This is the email message body</body></html>'; $headers = implode("\r\n", [ 'From: John Conde <webmaster@example.com>', 'Reply-To: webmaster@example.com', 'MIME-Version: 1.0', 'Content-Type: text/html; charset=ISO-8859-1', 'X-Mailer: PHP/' . PHP_VERSION ]);

Here's a full example of using PHP's mail() function <?php // Debugging tools. Only turn these on in your development environment. error_reporting(-1); ini_set('display_errors', 'On'); set_error_handler("var_dump"); // Special mail settings that can make mail less likely to be considered spam // and offers logging in case of technical difficulties. ini_set("mail.log", "/tmp/mail.log"); ini_set("mail.add_x_header", TRUE); // The components of our email $to = 'recipient@example.com'; $subject = 'Email Subject'; $message = 'This is the email message body'; $headers = implode("\r\n", [ 'From: webmaster@example.com', 'Reply-To: webmaster@example.com', 'X-Mailer: PHP/' . PHP_VERSION ]); // Send the email

GoalKicker.com – PHP Notes for Professionals

326


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.