phpmailer发送邮件(强大啊 呵呵)

我简单的封装了一下:下载地址可以到网上搜一下,搜不到的mail给我就好了

<?php
require_once dirname(__FILE__)."/PHPMailer_5.2.1/class.phpmailer.php";
include dirname(__FILE__)."/PHPMailer_5.2.1/class.smtp.php";
class Mail{

	public function postMail($to,$subject,$body){
		$mail             = new PHPMailer(); //new一个PHPMailer对象出来
	    $body             = eregi_replace("[\]",'',$body); //对邮件内容进行必要的过滤
	    $mail->CharSet ="UTF-8";//设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
	    $mail->IsSMTP(); // 设定使用SMTP服务
	    $mail->SMTPDebug  = 1;                     // 启用SMTP调试功能
	                                           // 1 = errors and messages
	                                           // 2 = messages only
	    $mail->SMTPAuth   = true;                  // 启用 SMTP 验证功能
	    $mail->SMTPSecure = "ssl";                 // 安全协议
	    $mail->Host       = "xxxxxxxxxxxxxx";      // SMTP 服务器
	    $mail->Port       = xxxxx;                   // SMTP服务器的端口号
	    $mail->Username   = "xxxxxxxxxxxx";  // SMTP服务器用户名
	    $mail->Password   = "xxxxxxx";            // SMTP服务器密码
	    $mail->SetFrom('[email protected]', 'xxxxxxxxx');
	    $mail->AddReplyTo("[email protected]","xxxxxxx");
	    $mail->Subject    = $subject;
	    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer! - From www.xxxxxxxxx.com"; // optional, comment out and test
	    $mail->MsgHTML($body);
	    $address = $to;
	    $mail->AddAddress($address, "xxxxxxx");
	    //$mail->AddAttachment("images/phpmailer.gif");      // attachment 
	    //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
	    if(!$mail->Send()) {
	    	// header("Content-Type:text/html;charset=utf-8");
	        echo "Mailer Error: " . $mail->ErrorInfo;
	    } else {
	    	// header("Content-Type:text/html;charset=utf-8");
	        echo "Message sent!恭喜,邮件发送成功!";
	    }
	}
}
?>

测试小例子

<?php
require_once dirname(__FILE__).'/lib/mail.php';
$obj = new Mail();
$content = <<<EOT
<html>
	<head></head>
	<body>
		<table style="background-color:#eee;background-image:url('http://index.youku.com/index/img/logo/logo_vr.png');border:1px solid #999">
			<th>
				<td>This is a email</td>
			</th>
			<tr>
				<td>Yes</td>
			</tr>
		</table>
	</body>
</html>
EOT;
$obj->postMail('[email protected]','Important File',$content);

你可能感兴趣的:(phpmailer发送邮件(强大啊 呵呵))