Dominus77 / yii2-advanced-start

Yii2 Start Project Advanced Template

Home Page:https://dominus77.github.io/yii2-advanced-start/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SignupForm not send out mail.

polinwei opened this issue · comments

Hi Sir:
Why the function : $user->save() always is false?

if ($user->save()) {
	Yii::$app->mailer->compose(['text' => '@modules/users/mail/emailConfirm'], ['user' => $user])
		->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
		->setTo($this->email)
		->setSubject(Module::t('frontend', 'EMAIL_CONFIRMATION') . ' ' . Yii::$app->name)
		->send();
}

Hi!
See if all data is being filled and validated

\yii\helpers\VarDumper::dump($user, 10, 1);
die;

Damn leg break)))

what's mean?

This means you need to format what you sent))

Much better)

 modules\users\models\frontend\User#1
(
    [currentPassword] => null
    [newPassword] => null
    [newPasswordRepeat] => null
    [role] => null
    [imageFile] => null
    [isDel] => null
    [yii\db\BaseActiveRecord:_attributes] => [
        'username' => 'wei'
        'email' => 'xx.xx@msn.com'
        'password_hash' => '$2y$13$ilPOrewgQorK7YB4zYQHle64Wh1iyensHw8pX9gHGb5rnxmTKNyru'
        'status' => 2
        'auth_key' => 'zMZcaTN-M2V0tQNtQanlHOy1EE97Zlfb'
        'email_confirm_token' => 'vn-HUxUzUDm-fFdT3OxcDJsdYqrbXv00'
    ]
    [yii\db\BaseActiveRecord:_oldAttributes] => null
    [yii\db\BaseActiveRecord:_related] => []
    [yii\base\Model:_errors] => null
    [yii\base\Model:_validators] => null
    [yii\base\Model:_scenario] => 'default'
    [yii\base\Component:_events] => [
        'beforeInsert' => [
            0 => [
                0 => [
                    0 => yii\behaviors\TimestampBehavior#2
                    (
                        [createdAtAttribute] => 'created_at'
                        [updatedAtAttribute] => 'updated_at'
                        [value] => null
                        [attributes] => [
                            'beforeInsert' => [
                                0 => 'created_at'
                                1 => 'updated_at'
                            ]
                            'beforeUpdate' => 'updated_at'
                        ]
                        [skipUpdateOnClean] => true
                        [owner] => modules\users\models\frontend\User#1(...)
                    )
                    1 => 'evaluateAttributes'
                ]
                1 => null
            ]
        ]
        'beforeUpdate' => [
            0 => [
                0 => [
                    0 => yii\behaviors\TimestampBehavior#2(...)
                    1 => 'evaluateAttributes'
                ]
                1 => null
            ]
        ]
    ]
    [yii\base\Component:_behaviors] => [
        'timestamp' => yii\behaviors\TimestampBehavior#2(...)
    ]
) 

May you give me the sample for yii2-advanced-start/common/config/params.php ?

Change the function as follows:

public function signup()
    {
        if ($this->validate()) {
            $user = new \modules\users\models\User(); // <<<
            $user->username = $this->username;
            $user->email = $this->email;
            $user->setPassword($this->password);
            $user->status = User::STATUS_WAIT;
            $user->generateAuthKey();
            $user->generateEmailConfirmToken();

            if ($user->save()) {
                Yii::$app->mailer->compose(['text' => '@modules/users/mail/emailConfirm'], ['user' => $user])
                    ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
                    ->setTo($this->email)
                    ->setSubject(Module::t('frontend', 'EMAIL_CONFIRMATION') . ' ' . Yii::$app->name)
                    ->send();

                return $user; // <<<
            }
        }
        return null;
    }

Or add a scenario to

public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_PROFILE_UPDATE] = ['email', 'first_name', 'last_name'];
        $scenarios[self::SCENARIO_AVATAR_UPDATE] = ['isDel'];
        $scenarios[self::SCENARIO_PASSWORD_UPDATE] = ['currentPassword', 'newPassword', 'newPasswordRepeat'];
        $scenarios[self::SCENARIO_PROFILE_DELETE] = ['status'];
        // add
        $scenarios['default'] = ['username', 'email', 'password_hash', 'status', 'auth_key', 'email_confirm_token'];
        return $scenarios;
    }

then

public function signup()
    {
        if ($this->validate()) {
            $user = new User();
            $user->username = $this->username;
            $user->email = $this->email;
            $user->setPassword($this->password);
            $user->status = User::STATUS_WAIT;
            $user->generateAuthKey();
            $user->generateEmailConfirmToken();

            if ($user->save()) {
                Yii::$app->mailer->compose(['text' => '@modules/users/mail/emailConfirm'], ['user' => $user])
                    ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
                    ->setTo($this->email)
                    ->setSubject(Module::t('frontend', 'EMAIL_CONFIRMATION') . ' ' . Yii::$app->name)
                    ->send();

                return $user; // <<<
            }
        }
        return null;
    }

Done!

May you give me the sample for yii2-advanced-start/common/config/params.php ?

Same https://github.com/Dominus77/yii2-advanced-start/blob/v1.0.1/common/config/params.php

When I try again , It say : This user name is already in use. How to reset it?

When I try again , It say : This user name is already in use. How to reset it?

What exactly? Do you register the same username?
Username and Email must be unique.
You can delete a user by going to the admin panel or using the console command:
php yii users/user/remove

Yes. I register the same username. I remove the account then it can work . but not send mail out.
###My common/config/main.php is


        'mailer' => [
            'useFileTransport' => false,
        	'class' => 'yii\swiftmailer\Mailer',
        	'transport' => [
        		'class' => 'Swift_SmtpTransport',
        		'host' => 'localhost',
        		'username' => 'username',
        		'password' => 'password',
        		'port' => '587',
        		'encryption' => 'tls',
        	],
        ],

###common/config/params.php


return [
    'adminEmail' => 'admin@myweb.com',
    'supportEmail' => 'admin@myweb.com',
    'user.passwordResetTokenExpire' => 3600,
    'domainFrontend' => 'myweb.com',
    'domainBackend' => 'myweb.com',
];

does mail arrive in /frontend / runtime / mail?

That the mail was sent not to the file but to the mail, check the 'useFileTransport' parameter in common / config / main-local.php it should be false

My Configuration for production:
/common / config / main-local.php

<?php
return [
    'components' => [
        //...
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            // 'useFileTransport' => true, // Comment out to send letters
        ],
    ],
];

/common/config/main.php

/...
'mailer' => [
     'class' => 'yii\swiftmailer\Mailer',
     'useFileTransport' => false,
],
/...

Mailer configuration see http://www.yiiframework.com/doc-2.0/yii-swiftmailer-mailer.html
But I work with this.

Refinement

###common/config/params.php
return [ 'adminEmail' => 'admin@myweb.com', 'supportEmail' => 'admin@myweb.com', 'user.passwordResetTokenExpire' => 3600, 'domainFrontend' => 'myweb.com', 'domainBackend' => 'myweb.com', ];

return [ 
    'adminEmail' => 'admin@myweb.com',
    'supportEmail' => 'admin@myweb.com',
    'user.passwordResetTokenExpire' => 3600,
    'domainFrontend' => 'http://myweb.com',
    'domainBackend' => 'http://myweb.com/admin',
 ];

Thanks a lot . It is workable. But I got the mail which sender is 'support@example.loc'

See the configuration in frontend
/frontend/config/params.php
and
/frontend/config/params-local.php

These values have priority over /common/config

Got it . thanks.