11 08 2015

18. 使用 mail() 发送邮件

之前我们提供了如何使用 Mandrill 发送邮件的 PHP 代码片段,但是如果你不想使用第三方服务,那么可以使用下面的 PHP 代码片段。

?
1
2
3
4
5
6
7
8
9
10
function send_mail($to,$subject,$body)
{
$headers = "From: KOONK\r\n";
$headers .= "Reply-To: blog@koonk.com\r\n";
$headers .= "Return-Path: blog@koonk.com\r\n";
$headers .= "X-Mailer: PHP5\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$body,$headers);
}

语法:

?
1
2
3
4
5
6
<?php
$to = "admin@koonk.com";
$subject = "This is a test mail";
$body = "Hello World!";
send_mail($to,$subject,$body);
?>
发表评论