Yii framework : Send phpmailer , SMTP


Reference Link : Download extension : smtp-mail

Place This Code in protected/config/main.php file

'components'=>array(
'Smtpmail'=>array(
'class'=>'application.extensions.smtpmail.PHPMailer',
'Host'=>"mail.yourdomain.com",
'Username'=>'test@yourdomain.com',
'Password'=>'test',
'Mailer'=>'smtp',
'Port'=>26,
'SMTPAuth'=>true,
),
),

Place This Code in protected/components/Controller.php file

public function mailsend($to,$from,$from_name,$subject,$message,$cc=array(),$attachment=array())
{
$mail = Yii::app()->Smtpmail;
$mail->SetFrom($from,$from_name);
$mail->Subject = $subject;
$mail->MsgHTML($this->mailTemplate($message));
$mail->AddAddress($to, "");

// Add CC
if(!empty($cc)){
foreach($cc as $email){
$mail->AddCC($email);
}
}

// Add Attchments
if(!empty($attachment)){
foreach($attachment as $attach){
$mail->AddAttachment($attach);
}
}

if(!$mail->Send()) {
return false; // Fail echo "Mailer Error: " . $mail->ErrorInfo;
}else {
return true; // Success
}
}

Call this function from your controller like this

$this->mailsend($to,$from,$from_name,$subject,$message,$cc,$attachment);

Advertisements

One thought on “Yii framework : Send phpmailer , SMTP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s