php邮件发送需要准备2个文件:一个是 class.phpmailer.php',另一个是:class.smtp.php
//文件导入,及参数获取省去。。。。
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username ='sgo@gmail.com'; // GMAIL username
$mail->Password = "123456"; // GMAIL password
$mail->AddReplyTo('sgo@gmail.com', 'test');
$mail->AddAddress($email, $username);//根据传进来的邮件地址参数,发送邮件
$mail->SetFrom('sgo@gmail.com', 'test');
$mail->AddReplyTo('sgo@gmail.com', 'test');
//以上邮箱的配置,qq和gmail都测试过,不过新的qq邮箱会把邮件截住,老的qq邮箱还是挺顺畅的。
$mail->Subject = 'Welcome!';
$mail->AltBody = 'Go!'; // optional - MsgHTML will create an alternate automatically
$body ="";
// 邮件的发送大概有2种方式:
一种是直接发送静态页面 $mail->MsgHTML(file_get_contents('contents.html'));//$mail->MsgHTML(file_get_contents('views/find.html'));这一种方式发送比较简洁,不过很难向静态页面传递参数
另一种是构建body,这一种比较费时,但是容易传参,一下就构建body的常用格式的一点记录
$mail->Body="
//在邮件里构建图片
"
系统自动发送了该邮件
"//一般的文字"
请点击://a标签的写法,双引号要转义
body结束,下面是附件
$mail->AddEmbeddedImage('views/image/you_logo.png',"my-attach"); // 向刚才构建的图片里嵌入一张图片,my-attach是cid
//$mail->AddAttachment('images/phpmailer_mini.gif'); //加入附件,附件形式可以是图片、声音等等
//$mail->Send();邮件发送
//echo $mail->Send();
if($mail->Send()){
$this->redirect('/simple/send?email='.$email);
}else{
echo "mail test!!!";
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
}