P
P
prozrost2017-02-17 11:52:36
Yii
prozrost, 2017-02-17 11:52:36

How to send email in yii2?

I want to send data from the form by email, to save the data in the database there is such a function:

public function insertForm()
    {
        $Form= new Form();
        $Form->name = $this->name;
        $Form->email = $this->email;
        $Form->age = $this->age;
        $Form->height= $this->height;
        $Form->weight= $this->weight;
        $Form->city = $this->city;
        $Form->techies = $this->techies;
        $Form->english_level = $this->english_level;
        $Form->images = UploadedFile::getInstances($Form,'images');
        foreach($Form->images as $file){
         $folder = ('uploads/' . $file->baseName . '.' . $file->extension);
         $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
         $Form->image_1.=$folder . ';';
        }
        return $Form->save(false);
    }

How now to send mail with this data?
Can it be done this way, or should it be done differently?
public function contact($email)
   {
       $Form = new Form();
       if ($this->validate()) {
           Yii::$app->mailer->compose()
               ->setTo($this->email)
               ->setFrom('[email protected]')
               ->setSubject('Hello')
               ->setTextBody('Hello')
               ->send();

           return true;
       } else {
           return false;
       }
   }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan, 2017-02-17
@LiguidCool

I don’t understand, maybe I see it badly, but how will your form go to the post office?
You need to ->setTextBody('Hello')put it in.

S
Sergey Doniy, 2017-02-17
@doniys_a

By default, all emails go to files.
option useFileTransport = true
change to false so that sending is via smtp

'mail' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@backend/mail',
        'useFileTransport' => false,//set this property to false to send mails to real email addresses
        //comment the following array to send mail using php's mail function
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => '[email protected]',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'tls',
                        ],
    ],
    ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question