Reputation: 4342
i'm trying to send a mail. i want to use Gmail server to send, for now i just want it to send to the same account from its being sent.
i'm using YiiMailer extension because it seems to be easy to use, this is the action i have in my controller:
public function actionContact()
{
$model=new ContactForm;
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
if($model->validate())
{
$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
$headers="From: $name <{$model->email}>\r\n".
"Reply-To: {$model->email}\r\n".
"MIME-Version: 1.0\r\n".
"Content-Type: text/plain; charset=UTF-8";
// mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
$mail = new YiiMailer('contact', array(
'message'=>$model->body,
'name'=>$model->name,
'description'=>'Contact form'
));
$mail->setFrom($model->email, $model->name);
$mail->setSubject($model->subject);
$mail->setTo(Yii::app()->params['adminEmail']);
$mail->isSMTP();
$mail->host = "smtp.gmail.com";
$mail->port = 465;
$mail->Username = "[email protected]";
$mail->Password = "password";
if($mail->send())
{
Yii::app()->user->setFlash('contact','Gracias por contactarnos, te responderemos tan pronto como podamos.');
$this->refresh();
}
else
{
Yii::app()->user->setFlash('error','Error enviando correo.');
$this->refresh();
}
}
}
$this->render('contact',array('model'=>$model));
}
the adminMail is the same like [email protected]
i dont't know if i have the right settings for gmail, i'm sure the condition of the function doesn't exist so it does nothing.
just to make sure. this is my config:
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
'ext.YiiMailer.YiiMailer'
),
PD: i found the gmail settings around post. i'm not sure if they changed the settings.
Upvotes: 2
Views: 5966
Reputation: 4342
i solved this by install stunnel, so openssl is enabled and let me send emails from my server
Upvotes: 0
Reputation: 8146
This works for me!
$mail = new YiiMailer();
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->Port = 587;
//set mail properties
$mail->setFrom('[email protected]', 'YYY');
$mail->setTo($model->to_email);
$mail->setSubject($model->subject);
$mail->setBody($model->message, 'text/html');
$mail->send();
You need to set the smtpauth
to true
when using Gmail to send email.Ckeck your sendMail box after executing the action!
Upvotes: 0
Reputation: 11887
Try using method getError()
to find out what the error was (you can use special debug options to get more information on possible errors)
$mail->SMTPDebug = 1; //optional
if($mail->send())
{
Yii::app()->user->setFlash('contact','Gracias por contactarnos, te responderemos tan pronto como podamos.');
$this->refresh();
}
else
{
echo '<pre>';
var_dump($mail->getError());
echo '</pre>';
exit;
}
Additionaly, try the following:
$mail->SMTPSecure = 'ssl'
; or $mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
This question could interest you: send email using Gmail SMTP server through PHP Mailer
Upvotes: 2