$to = "heartchaincxj@163.com";
$subject = "Test";
$message = "This is a test mail!";
ini_set('SMTP','smtp.163.com');
ini_set('smtp_port',25);
ini_set('sendmail_from',"sinllychenfjnp@163.com"
mail($to,$subject,$message);
当然也可以将
ini_set('SMTP','smtp.163.com');
ini_set('smtp_port',25);
ini_set('sendmail_from',sinllychenfjnp@163.com);
通过配置php.ini.但这样运行之后会出现如下错误:SMTP server response:553 authentication is required,smtp2,DNGowKD7v5BTDo9NnplVBA--.1171S21301220947.错误的原因是说SMTP服务器需要验证信息,百度了一下发现很多人出现了类似的问题,究其原因是因为mail()函数使用需要一台无需SMTP验证就可以发信的邮件服务器。这种方式对服务器本身做了一些限制,不太适合开发。
对于PHP发送邮件实现,普遍使用的是通过第三方的框架PHPMailer来实现。PHPMailer是一个开源的PHP发送邮件的框架,由一个外国人编写。在PHP邮件发送上非常好用,无需SMTP服务器也无需在PHP.ini文件里面配置信息。PHPMailer可以到http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list下载。解压之后,我们将class.phpmailer.php,class.pop3.php,class.smtp.php.放到当前工程路径下面,这里我通过封装一个方法来实现发送邮件的功能。代码如下: