首先mail.class.php :
include_once('class.phpmailer.php');
class mail{
private
$mail;
private
$body = '/p>
"-//W3C//DTD HTML 4.01
Transitional//EN">
Whty EmailCenter
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
';
public
function Instance($user = 'lxx@xx',$pwd = 'xx',$host = 'xx')
{
$this->mail = new PHPMailer(true);
$this->mail->CharSet =
'utf-8';
// 设置编码
$this->mail->IsSMTP();
// tell the class to use SMTP
//$mail->IsMail();
// tell the class to use MAIL
$this->mail->SMTPAuth
=
false;
// enable SMTP authentication
//$this->mail->Port
=
25;
// set the SMTP server port
$this->mail->Host
=
$host;
// SMTP server
$this->mail->Username
=
$user;
// SMTP server username
$this->mail->Password
=
$pwd;
// SMTP server password
//$this->MFrom();
//$this->MFromName();
}
public
function mFrom($from = 'rttrtr@saa')
{
$this->mail->From = $from;
}
public
function mFromName($name = 'linweijie')
{
$this->mail->FromName = $name;
}
public
function mTo($to,$name)
{
if($to)
$this->mail->AddAddress($to,$name);
else die('mail error : 收件人不能为空');
}
public
function mSubject($subject)
{
$this->mail->Subject =
$subject;
}
public
function mAttachment($attachment,$attachmentName = '')
{
if(file_exists(iconv("UTF-8",
"GB2312", $attachment))){
if(!$attachmentName){
$arr = explode('/',$attachment);
$attachmentName = $arr[count($arr) - 1];
}
$this->mail->AddAttachment(iconv("UTF-8",
"GB2312",
$attachment),$attachmentName);
}
else die('mail error : 附件不存在');
}
public
function mBody($body)
{
if($body) {
$this->body =
str_replace('',$body,$this->body);
$this->body = preg_replace('/\\\\/','',
$this->body); //Strip backslashes
}
else die('mail error : 邮件内容不能为空');
}
public
function mSend()
{
try{
$this->mail->MsgHTML($this->body);
$this->mail->IsHTML(true);
$this->mail->Send();
return true;
}
catch (phpmailerException $e){
die($e->errorMessage());
}
}
}
?>
调用邮件发送类:
include_once('mail.class.php');
$mail = new mail();
$mail->Instance();
$mail->mFrom(‘xx’);
$mail->mFromName('xxx');
$mail->mTo('xxx@xx','lin');
$mail->mBody('
$name,
你好:
align="center">xxx
align="center">
src="test.png"
alt="测试图片"
/>
');//test.png 在当前目录
$mail->mSubject('邮件测试--主题');
$mail->mAttachment('README');//附件
$back = $mail->mSend();
echo
"";
?>