I am using yii2 default contact us page to get mail from user.. It is working fine but I am getting mail from email which I mention in code, to adminEmail
which is also my email id. here is code=>
public function sendEmail($email)
{
return Yii::$app->mailer->compose()
->setTo($email)
->setFrom([$this->email => $this->name])//want the data from email field..how to achieve that??
->setSubject($this->subject)
->setTextBody($this->body)
->send();
}
if I try like this ->setFrom([$this->email => $this->email])
in above code I get 2 email id in recieved email 1st $this->email
is username
which is mention in mailer of below code 2nd $this->email
is email id which is filled in contact us form which I want. in file- common\\config\\main-local.php
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'pharmadarshu@gmail.com',
'password' => '*****',
'port' => '587',
'encryption' => 'tls',
],
but then I am unable to get name ? I want to receive all fields of contact us form ie name, email, subject, body ? I fact all are getting properly except email ? can anyone help??
hope described properly my question..
If you are using the default ContactForm
model you simply need to create a table in database. It can have fields like name
and email
Add the tableName method in your Contact form model
public static function tableName()
{
return '{{%contact_table name}}';
}
Then in Yii contact action after validation call the save
method
/**
* Displays contact page.
*
* @return mixed
*/
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
//save data
$model->save();
//end save
if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
} else {
Yii::$app->session->setFlash('error', 'There was an error sending email.');
}
return $this->refresh();
} else {
return $this->render('contact', [
'model' => $model,
]);
}
}
Add email to the body
public function sendEmail()
{
return Yii::$app
->mailer
->compose(
['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'],
['user' => $user]
)
->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot'])
->setTo($this->email)//$this->email can be added to the message body
->setSubject('Password reset for ' . Yii::$app->name)
->send();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.