Send out html emails in php using the mail func...
2012-10-26 13:56
316 查看
http://www.binarytides.com/send-out-html-emails-in-php-using-the-mail-function/
Here is a easy to use function that does the task and has a form very similar to the mail function.
The syntax of the function has been kept similar to the mail function to make it look more like a replacement. The 4th option however is not for headers, but additional options/parameters for the email which are processed by the function itself.
Usage
Here is an example of how the above function can be used.
So all that needs to be done is make this function available to the php script and replace 'mail' with 'html_mail'. That much is enough to convert all plain text emails into html (and yes the content has to be html too).
I find this function great for single script programs that do some sort of maintenance task and send out email reports of the task status. This function can be put right into a script without the need for any extra includes.
This technique cannot be easily used for sending attachments or use a specific smtp server. For full scale features use a more robust email library like Phpmailer.
Send out html emails in php using the mail function
The mail function of php can be used to send not only plain text emails, but html emails too. The documentation page shows how to do that.Here is a easy to use function that does the task and has a form very similar to the mail function.
function html_mail($to, $subject, $message, $options) { if(isset($options['from_name'])) { $headers = "From: " . $options['from_name'] . "<".$options['from_email'].">" . "\r\n"; } $headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n"; //$headers .= "CC: someone@example.com\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; mail($to, $subject, $message, $headers); }
The syntax of the function has been kept similar to the mail function to make it look more like a replacement. The 4th option however is not for headers, but additional options/parameters for the email which are processed by the function itself.
Usage
Here is an example of how the above function can be used.
$to = 'm00n.silv3r@gmail.com'; $subject = 'Welcome to website'; $from_name = 'Sunny'; $from_email = 'no-reply@example.com'; $message = '<html><body>'; $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">'; $message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>Silver Moon</td></tr>"; $message .= "<tr><td><strong>Email:</strong> </td><td>m00n.silv3r@gmail.com</td></tr>"; $message .= "<tr><td><strong>Location:</strong> </td><td>Moon</td></tr>"; $message .= "</table>"; $message .= "</body></html>"; html_mail($to, $subject, $message, array('from_email' => $from_email, 'from_name' => $from_name));
So all that needs to be done is make this function available to the php script and replace 'mail' with 'html_mail'. That much is enough to convert all plain text emails into html (and yes the content has to be html too).
I find this function great for single script programs that do some sort of maintenance task and send out email reports of the task status. This function can be put right into a script without the need for any extra includes.
This technique cannot be easily used for sending attachments or use a specific smtp server. For full scale features use a more robust email library like Phpmailer.
相关文章推荐
- Using the string repalce action in SharePoint workflow designer to send mail from mail template
- [Python] Send emails to the recepients specified in Message["CC"]
- Sending HTML content in an email using PHP
- using JS to control two select(html),the data can be loaded from database and XML,and show in the select
- [Javascript] A function works like 'print_r()' in PHP to print out the details of an object for JS debugging
- [转]让国外邮件服务商不再拒收你的邮件,The IP you're using to send mail is not authorized
- [html] PHP使用Google map web services来计算两点间的距离 -- Compute the distance between two place via Google map services in PHP
- Sending Emails in .NET with the System.Net.Mail Namespace
- Configuring ELMAH to send emails without putting your password in the config file
- A full-duplex audio player in C# using the waveIn/waveOut APIs
- How to delete all email in the mail server using telnet & WCScript - 如何用 Telnet 删除邮件服务器上的所有邮件
- Setting the Reply-To Header in an Email using CDONTS.NewMail Object and CDO Message
- Using the HTMLControl Class in Adobe AIR to parse HTML as a data source
- Send mail using Gmail SMTP in Magento
- How to attach multiple files in the Send Mail Task in SSIS
- PHP using mcrypt and store the encrypted in MySQL
- 解决Mysql报错:PHP Warning: mysql_connect(): mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication.
- PHP: Send the Authorization token in a header instead of on the querystring and 读取 XML
- 解决Mysql报错:PHP Warning: mysql_connect(): mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication.
- 让国外邮件服务商不再拒收你的邮件,The IP you're using to send mail is not authorized