PHP自带的mail函数比较蛋疼,在win下配置了sendmail还是无法发送邮件。而使用第三方的pear/mail可以直接通过smtp连接邮件发送服务器。如(smtp.163.com)。从而没有必要在本机上安装sendmail等类
确保PEAR Mail包已经安装。
composer安装方式:composer require pear/mail pear/net_smtp
参考:
http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm
http://stackoverflow.com/questions/2284468/problem-with-php-pear-mail
1. [代码]非加密方式
<?php require_once "vendor/autoload.php"; $from = "test<test@163.com>"; $to = "test <test@outlook.com>"; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; $host = "smtp.163.com"; $port = "25"; $username = "test@163.com"; $password = "test123"; $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, // 'debug'=>true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo("<p>" . $mail->getMessage() . "</p>"); } else { echo("<p>Message successfully sent!</p>"); } ?>