Add the SMTP Details in main.php ¶
Download the extension and add it into protected/extension folder. Now configure the smtp details in protected/config/main.php
'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,
),
),
Create Mailfunction ¶
public function mailsend($to,$from,$subject,$message){
$mail=Yii::app()->Smtpmail;
$mail->SetFrom($from, 'From NAme');
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->AddAddress($to, "");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}else {
echo "Message sent!";
}
}
Gmail
If you know How to handle mail using Gmail host, Please post here
Gmail Configuration
Just configure like this in your config/main
'Smtpmail'=>array( 'class'=>'application.extensions.smtpmail.PHPMailer', 'Host'=>"smtp.gmail.com", 'Username'=>'your@gmail.com', 'Password'=>'password here', 'Mailer'=>'smtp', 'Port'=>587, 'SMTPAuth'=>true, 'SMTPSecure' => 'tls', ),
And Don't forget to activate your php_openssl in PHP.ini
regarding file attachment
can anyone please tell me how to attach pdf file using this extension?
Gmail Smtp
@mbala
Gmail smtp is : Gmail SMTP server address: smtp.gmail.com Gmail SMTP user name: Your full Gmail address (e.g. example@gmail.com) Gmail SMTP password: Your Gmail password Gmail SMTP port: 465 Gmail SMTP TLS/SSL required: yes
I am unable to send HTML in email
Hi, can you please guide me how to send html in email via this extension. i have used
$mail->IsHTML(true);
but it is not sending htm. please guide me ho i can do this. code is given below.
$mail = Yii::app()->Smtpmail; $mail->SetFrom("from", 'from'); //$recipient = yii::app()->Smtpmail->mail_recipient; $mail->Subject = "Welcome to Karmora"; $message = '<img src='logo'><br>welcome to YII'; $mail->MsgHTML($message); $mail->AddAddress("email_to", ""); if(!$mail->Send()){ return false; } else{ return true; }
Thank you
I can send email from localhost WAMP using this without editing php.ini. Thank you @mbala :D
And thank you @reptildarat for gmail config that you shared, i use that, terima kasih ^^
Create Mailfunction where ?
A lot of members ask where we can put the function mailsend
So, there are two best practice
Either you can put this function in components/Controller.php and call it in an controller/action as
$this->mailsend($to,$from,$subject,$message);
OR
if you are use a module that its controllers not extend the components/Controller.php you can make the mailsend as a static function
public static function mailsend($to,$from,$subject,$message) ...
and call it as Controller::mailsend($to,$from,$subject,$message);
Best of all:
Make a globally class with static function in combonents like it described in this wiki
http://www.yiiframework.com/wiki/688/shortcut-usefull-access-roles-methods-rbac
and call the function like this
SYii::mailsend($to,$from,$subject,$message); ..anywhere in controllers/extensions/modules/combonents/models/views
IMPORTANT: Add $mail->ClearAddresses();
If you use the function in a loop, the addresses will accumulate one over another. The proper function is:
public function mailsend($to,$from,$subject,$message){ $mail=Yii::app()->Smtpmail; $mail->SetFrom($from, 'From NAme'); $mail->Subject = $subject; $mail->MsgHTML($message); $mail->AddAddress($to, ""); if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; }else { echo "Message sent!"; } $mail->ClearAddresses(); //clear addresses for next email sending }
how to hide the sender email address?
HI,
the code is work for me, but i would like to hide the sender email address from display.
Can anyone help me on it?
thanks.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.