1

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 i.e. name, email, subject, body ? I fact all are getting properly except email? can anyone help??

hope described properly my question..

Goli
  • 436
  • 5
  • 19
  • Hey,who was the original sender of the email? it looks like you are send mail to yourself.Then don't post your Gmail passwords in here...its risky :) – ovicko Oct 31 '17 at 08:00
  • ok I'll edit it.. for testing am sending mail to myself.. I have used same email id for both adminEmail and username.. – Goli Oct 31 '17 at 08:22
  • https://stackoverflow.com/questions/47068889/how-to-get-email-field-of-contact-us-form-in-the-email-which-admin-got-yii2 here is the question which i want – Goli Nov 02 '17 at 06:34
  • I think you get it from this link [https://stackoverflow.com/questions/47068889/how-to-get-email-field-of-contact-us-form-in-the-email-which-admin-got-yii2/47136298?noredirect=1#comment81292635_47136298][1] – Shailesh Yadav Nov 08 '17 at 04:39

1 Answers1

0

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();
    }
ovicko
  • 2,242
  • 3
  • 21
  • 37
  • Is it possible to get email id of user directly in the mail received by Admin?? – Goli Oct 31 '17 at 08:59
  • like this from: Guestname – Goli Oct 31 '17 at 09:06
  • I have updated the answer,you just need to add the email to message body – ovicko Oct 31 '17 at 09:06
  • Check this line in the above code ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot']) It will show like this:From Namexxx – ovicko Oct 31 '17 at 09:10
  • I already have this code for passwordResetToken. where to use in contact us..cant understand – Goli Oct 31 '17 at 09:18
  • this is my code................ public function sendEmail($email) { return Yii::$app->mailer->compose() ->setTo($email) ->setFrom([$this->email => $this->name]) /////what should write to get email field here ->setSubject($this->subject) ->setTextBody($this->body) ->send(); } – Goli Oct 31 '17 at 10:26