在YII中发送邮件

时间:2014-03-22 11:24:51

标签: php yii

我想用yii发送邮件。我已为此编写代码,在我看来,以下行无效。

mail($adminemail, $subject, $message, $headers);

我的控制员:

public function actionIndex()
{
$model=new ContactForm;
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
if($model->validate())
{
//$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
//Mail
$subject='Contact Message from '.$model->name.'';
$adminemail = Yii::app()->params['adminEmail'];
$projectname = Yii::app()->params['projectname'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
$headers .= "Reply-To: ".$model->name." <{$model->email}>\r\n"; 
$headers .= "Return-Path: ".$model->name." <{$model->email}>\r\n";
$headers .= "From: ".$model->name." <{$model->email}>\r\n";             
$message ="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
</head><body><div style='width:800px; height:100% auto; border:3px solid #088ef6; border-radius:5px; padding:10px'>
<table width='800' border='0' cellspacing='0' cellpadding='0'>
<tr><td> <a href='".Yii::app()->homeUrl."' target='_blank'> <img src='".Yii::app()->homeUrl."images/logo.jpg' width='425' height='65' align='absmiddle' /> </a></td></tr>
<tr><td><div style='padding:10px'><div style='font-size:14px'><div align='left' style='color:#d50000;   font-weight:bold; font-family:Arial, Helvetica, sans-serif'>Dear Administrator,</div> <br /><div align='left' style='font-family:Arial, Helvetica, sans-serif'>Received Contact Message from ".$model->name.", <br /> 
            Email: ".$model->email.".<br /> 
            Phone: ".$model->mobile.".<br /> <br /> 
            ".$model->message."
            </div>
</html>";

mail($adminemail, $subject, $message, $headers);

$this->refresh();

        }
    }


    $this->render('index',array('model'=>$model));


}

我的yii代码是否有问题,或者我的服务器灯设置是否有任何更改

1 个答案:

答案 0 :(得分:1)

如果没有什么特别的话,我建议你使用像PHPMailer或SwiftMailers这样的库。它们减轻了你的负担,调试它们就像设置单行一样简单

例如,使用PHPMailer,您将其作为examples之一,并启用debugging就像$mail->SMTPDebug = 1;一样简单

同时结帐Yii邮寄地址here

<?php
require '../PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();
// Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer sendmail test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
?>