它提供的功能包括:
*.在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址
*.支持多种邮件编码包括:8bit,base64,binary和quoted-printable
*.支持SMTP验证
*.支持冗余SMTP服务器
*.支持带附件的邮件和Html格式的邮件
*.自定义邮件头
*.支持在邮件中嵌入图片
*.调试灵活
*.经测试兼容的SMTP服务器包括:Sendmail,qmail,Postfix,Imail,Exchange等
*.可运行在任何平台之上
名称 | 默认 | 描述 | 可更改 |
SMTP | "localhost" | Windows 专用:SMTP 服务器的 DNS 名称或 IP 地址。 | PHP_INI_ALL |
smtp_port | "25" | Windows 专用:SMTP 端口号。自 PHP 4.3 起可用。 | PHP_INI_ALL |
sendmail_from | NULL | Windows 专用:规定从 PHP 发送的邮件中使用的 "from" 地址。 | PHP_INI_ALL |
sendmail_path | NULL | Unix 系统专用:路径(通常 /usr/sbin/sendmail 或 /usr/lib/sendmail) | PHP_INI_SYSTEM |
下面是完整示例代码:
配置文件里/protected/config/params.php设置如下
'emailUserName'=>'11111111@qq.com', 'emailPassword'=>'11111111', 'emailHost'=>'smtp.qq.com', 'emailPort'=>'25', 'emailTimeout'=>'20',
方法中
/** * 邮件发送 * @param string $toemail * @param string $subject * @param string $message * @return boolean */ public static function sendMail($toemail = '', $subject = '', $message = '') { $mailer = Yii::createComponent ( 'application.extensions.mailer.EMailer' ); //邮件配置 $mailer->SetLanguage('zh_cn'); $mailer->Host = Yii::app()->params['emailHost']; //发送邮件服务器 $mailer->Port = Yii::app()->params['emailPort']; //邮件端口 $mailer->Timeout = Yii::app()->params['emailTimeout'];//邮件发送超时时间 $mailer->ContentType = 'text/html';//设置html格式 $mailer->SMTPAuth = true; $mailer->Username = Yii::app()->params['emailUserName']; $mailer->Password = Yii::app()->params['emailPassword']; $mailer->IsSMTP (); $mailer->From = $mailer->Username; // 发件人邮箱 $mailer->FromName = Yii::app()->params['emailFormName']; // 发件人姓名 $mailer->AddReplyTo ( $mailer->Username ); $mailer->CharSet = 'UTF-8'; // 添加邮件日志 $modelMail = new MailLog (); $modelMail->accept = $toemail; $modelMail->subject = $subject; $modelMail->message = $message; $modelMail->send_status = 'waiting'; $modelMail->save (); // 发送邮件 $mailer->AddAddress ( $toemail ); $mailer->Subject = $subject; $mailer->Body = $message; if ($mailer->Send () === true) { $modelMail->times = $modelMail->times + 1; $modelMail->send_status = 'success'; $modelMail->save (); return true; } else { $error = $mailer->ErrorInfo; $modelMail->times = $modelMail->times + 1; $modelMail->send_status = 'failed'; $modelMail->error = $error; $modelMail->save (); return false; } }