## 使用[PHPMailer](https://code.google.com/a/apache-extras.org/p/phpmailer/)
經 PHPMailer 5.1 測試
PHP 提供了一個?[mail()](http://php.net/manual/zh/function.mail.php)?函數,看起來很簡單易用。 不幸的是,與 PHP 中的很多東西一樣,它的簡單性是個幻象,因其虛假的表面使用它會導致嚴重的安全問題。
Email 是一組網絡協議,比 PHP 的歷史還曲折。完全可以說發送郵件中的陷阱與 PHP 的 mail() 函數一樣多,這個可能會令你有點「不寒而栗」吧。
[PHPMailer](http://code.google.com/a/apache-extras.org/p/phpmailer/)?是一個流行而成熟的開源庫,為安全地發送郵件提供一個易用的接口。 它關注可能陷阱,這樣你可以專注于更重要的事情。
## 示例
~~~
<?php
// Include the PHPMailer library
require_once('phpmailer-5.1/class.phpmailer.php');
// Passing 'true' enables exceptions. This is optional and defaults to false.
$mailer = new PHPMailer(true);
// Send a mail from Bilbo Baggins to Gandalf the Grey
// Set up to, from, and the message body. The body doesn't have to be HTML;
// check the PHPMailer documentation for details.
$mailer->Sender = 'bbaggins@example.com';
$mailer->AddReplyTo('bbaggins@example.com', 'Bilbo Baggins');
$mailer->SetFrom('bbaggins@example.com', 'Bilbo Baggins');
$mailer->AddAddress('gandalf@example.com');
$mailer->Subject = 'The finest weed in the South Farthing';
$mailer->MsgHTML('You really must try it, Gandalf!-Bilbo');
// Set up our connection information.
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = 'ssl';
$mailer->Port = 465;
$mailer->Host = 'my smpt host';
$mailer->Username = 'my smtp username';
$mailer->Password = 'my smtp password';
// All done!
$mailer->Send();
?>
~~~