user
user

Reputation: 1759

Sending mail in YII

I want to send mails in yii. I have written code for this and it seems to me that the following line is not working.

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

my controller:

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));


}

Is there any problem with my yii code, or should any changes be made for my server lamp settings

Upvotes: 0

Views: 1568

Answers (1)

Stefano Mtangoo
Stefano Mtangoo

Reputation: 6534

If there is nothing special I suggest you use libraries like PHPMailer or SwiftMailers. They take a burden off your should and debugging them is as easy as setting single line

for example with PHPMailer you have this as one of examples and turning on debugging is as simple as $mail->SMTPDebug = 1;

Also checkout Yii Mailing extensions 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('[email protected]', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('[email protected]', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('[email protected]', '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!";
}
?>

Upvotes: 1

Related Questions