• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP安全发送邮件:PHPMailer

php 海叔叔 4年前 (2021-09-22) 44次浏览 已收录 0个评论

要使用PHP安全地发送邮件可以使用PHPMailer,说明如下:

PHP 本身提供了一个mail()函数,看起来简单易用。 不幸的是,与 PHP 中的很多东西一样,它的简单性只是个幻象,事实上使用不当它可能有严重的安全问题。

Email 是一组网络协议,比 PHP 的历史还曲折。完全可以说发送邮件中的陷阱与 PHP 的mail()函数一样多,这个可能会令你有点「不寒而栗」吧。

PHPMailer 是一个流行而成熟的开源库,为安全地发送邮件提供一个易用的接口。 它关注和解决可能的陷阱,这样你可以专注于更重要的事情。

示例PHP代码如下,经 PHPMailer 5.1 测试

<?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 = '[email protected]';
$mailer->AddReplyTo('[email protected]', 'Bilbo Baggins');
$mailer->SetFrom('[email protected]', 'Bilbo Baggins');
$mailer->AddAddress('[email protected]');
$mailer->Subject = 'The finest weed in the South Farthing';
$mailer->MsgHTML('<p>You really must try it, Gandalf!</p><p>-Bilbo</p>');

// 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();
?>

PHPMailer的特性

要用PHP发送邮件,PHPMailer应该是时间上最受欢迎的代码库。
PHPMailer在许多著名的开源项目想使用,如:WordPress, Drupal, 1CRM, SugarCRM, Yii, Joomla!
集成SMTP支持,不需要本地服务器即可发送。
支持使用多个TOs, CCs, BCCs and REPLY-TOs进行发送。
多种邮件形式,支持不能读取HTML的邮件客户端。
支持的编码:UTF-8 content and 8bit, base64, binary, and quoted-printable。
支持使用LOGIN, PLAIN, NTLM, CRAM-MD5 and Google’s XOAUTH2 mechanisms over SSL and TLS transports进行SMTP身份认证。
支持47中语言显示错误消息。
支持DKIM and S/MIME signing。
兼容PHP5.0及之后的版本。
还有很多其他特性,这里不意义列出了,详见:PHPMailer。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP安全发送邮件:PHPMailer
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址