PHP发送邮件示例

一、通过mail()

此方法,需要配置邮件服务器

参考代码:

<?php
 $toaddress = 'test@163.com';
 $subjects = 'just test mail';
 $mailcontent = wordwrap('test miali content!',70);//到了70个字符就换行
 $fromaddress = 'From: 123@163.com';
 mail($toaddress,$subjects,$mailcontent,$fromaddress);
 ?>

二、开源项目PHPMailer实现邮件发送

项目地址:http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list

   里面有示例,参考代码:

<?php
 /**
 * Simple example script using PHPMailer with exceptions enabled
 * @package phpmailer
 * @version $Id$
 */
 
 require '../class.phpmailer.php';
 
 try {
 	$mail = new PHPMailer(true); //New instance, with exceptions enabled
 
 	$body             = file_get_contents('contents.html');
 	$body             = preg_replace('/\\\\/','', $body); //Strip backslashes
 
 	$mail->IsSMTP();                           // tell the class to use SMTP
 	$mail->SMTPAuth   = true;                  // enable SMTP authentication
 	$mail->Port       = 25;                    // set the SMTP server port
 	$mail->Host       = "smtp.qq.com"; // SMTP server
 	$mail->Username   = "yourname@qq.com";     // SMTP server username
 	$mail->Password   = "password";            // SMTP server password
 
 	//$mail->IsSendmail();  // tell the class to use Sendmail
 
 	$mail->AddReplyTo("name@domain.com","First Last");
 
 	$mail->From       = "yourname@qq.com";
 	$mail->FromName   = "First Last";
 
 	$to = "mail@qq.com";
 
 	$mail->AddAddress($to);
 
 	$mail->Subject  = "First PHPMailer Message";
 
 	$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
 	$mail->WordWrap   = 80; // set word wrap
 
 	$mail->MsgHTML($body);
 
 	$mail->IsHTML(true); // send as HTML
 
 	if(!$mail->Send())
 		echo "错误原因: " . $mail->ErrorInfo;
 	echo 'Message has been sent.';
 } catch (phpmailerException $e) {
 	echo $e->errorMessage();
 }
 ?>


原文地址:https://www.cnblogs.com/owenyang/p/3579125.html