[SOLVED] send email fail

hi, my register function used to work, I spent some time tweaking something in the UserIdentity class, when I got back registering a test account, it doesn’t save nor send email anymore, I don’t really know what’s wrong as the yii log nor php logs doesn’t tell any error at all, it simply doesn’t work…but the validations are alright?




	public function actionRegister()

	{

		$form = new Wsmembers;

		

		if(isset($_POST['Wsmembers']))

		{

			$form->attributes = $_POST['Wsmembers'];

			if($form->validate())

			{

				$pass = sha1($_POST['Wsmembers']['LoginPassword']);

				$form->EmailAddress = $_POST['Wsmembers']['EmailAddress'];

				$form->LoginName = $_POST['Wsmembers']['LoginName'];

				$form->LoginPassword = $pass;

				$form->MembershipContactName = $_POST['Wsmembers']['MembershipContactName'];

				$form->MembershipSecretQuestion = $_POST['Wsmembers']['MembershipSecretQuestion'];

				$form->MembershipSecretAnswer = $_POST['Wsmembers']['MembershipSecretAnswer'];

				$form->MembershipDateSetup = new CDbExpression('NOW()');

				$form->save();

				

				if($form->save()){

					Yii::app()->user->setFlash('success','Thank you for joining!,Please check your email and activate your account');

					

					$to = $_POST['Wsmembers']['EmailAddress'];

					$subject = "Welcome To BuggyMan!";

					$message = "Thank you for joining!, we have sent you a separate email that contains your activation link";

					$from = "FROM: buggyman@gmail.com";

					

					mail($to,$subject,$message,$from);

					

					$headers  = 'MIME-Version: 1.0' . "\r\n";

					$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

					$headers .= 'From: buggy man <tesmail@gmail.com>' . "\r\n";

					

					$subject2 = "Your Activation Link";


					$message2 = "<html><body>Please click this below to activate your membership<br />".

								 Yii::app()->createAbsoluteUrl('wsmembers/activate', array('email' => $_POST['Wsmembers']['EmailAddress'])).

								"</body></html>";

								

					mail($to, $subject2, $message2, $headers);

					

				}

								

				$this->redirect($this->createUrl('register', array('r'=>'wsmembers/register')));

			}

		}

		$this->render('register', array('form' => $form));



can you help me spot the not ? :unsure: the culprit that prevents it to save the data and send email? … i tested the default contact page and it actually sends email :(

You call save() 2 times, so remove the first one. Does the data get saved at least?

it didn’t work still, but even though the save() is inside that if, it used to work.

here’s my model, am not sure if there’s something about it that causes the mess





class Wsmembers extends CActiveRecord

{

	public $LoginPassword_repeat;

	public $verifyCode;

	/**

	 * Returns the static model of the specified AR class.

	 * @return Wsmembers the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'wsmembers';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('EmailAddress, LoginName, LoginPassword, MembershipContactName, MembershipSecretQuestion, MembershipSecretAnswer', 'required'),

			array('LoginPassword','compare'),

			array('LoginPassword_repeat','safe'),

			array('EmailConfirmed, MembershipLoginAttempts, MembershipLockedOut, MembershipSystemLockedOut, IntroducingMemberID', 'numerical', 'integerOnly'=>true),

			array('EmailAddress, LoginName, LoginPassword, MembershipContactName, LoginNameAllUsers', 'length', 'max'=>50),

			array('LoginPasswordAllUsers', 'length', 'max'=>40),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('MemberShipID, WSEmailAddress, WSLoginName, LoginPassword, EmailConfirmed, MembershipDateSetup, MembershipDateConfirmed, MembershipContactName, MembershipSecretQuestion, MembershipSecretAnswer, MembershipLoginAttempts, MembershipLockedOut, MembershipLockOutDateTime, MembershipSystemLockedOut, LoginNameAllUsers, LoginPasswordAllUsers, IntroducingMemberID', 'safe', 'on'=>'search'),

			array('verifyCode', 'captcha', 'allowEmpty' => !extension_loaded('gd')),

			array('EmailAddress, LoginName' , 'unique')

		);

	}



Your code should be much more simple, for example:




 {

                $form = new Wsmembers;

                

                if(isset($_POST['Wsmembers']))

                {

                        $form->attributes = $_POST['Wsmembers'];

                        if($form->validate())

                        {


                                $form->LoginPassword = sha1($form->LoginPassword);

                                if($form->save()){

                                        Yii::app()->user->setFlash('success','Thank you for joining!,Please check your email and activate your account');

                                        

                                        $to = $form->email_address;

                                        $subject = "Welcome To BuggyMan!";

                                        $message = "Thank you for joining!, we have sent you a separate email that contains your activation link";

                                        $from = "FROM: buggyman@gmail.com";

                                        

                                        mail($to,$subject,$message,$from);

                                        

                                        $headers  = 'MIME-Version: 1.0' . "\r\n";

                                        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

                                        $headers .= 'From: buggy man <tesmail@gmail.com>' . "\r\n";

                                        

                                        $subject2 = "Your Activation Link";


                                        $message2 = "<html><body>Please click this below to activate your membership<br />".

                                                                 Yii::app()->createAbsoluteUrl('wsmembers/activate', array('email' => $form->EmailAddress)).

                                                                "</body></html>";

                                                                

                                        mail($to, $subject2, $message2, $headers);

                                        

                                }

                                                                

                                $this->redirect($this->createUrl('register', array('r'=>'wsmembers/register')));

                        }

                }

                $this->render('register', array('form' => $form));




This should work. Instead of $_POST use $form, is more object oriented.

If the save is not working, maybe there are some error. Try doing:




print_r($form->errors);



If is not an empty array, it means that some rule detected an error.

here’s what i did, i commented the who




                              $pass = sha1($_POST['Wsmembers']['LoginPassword']);

                                $form->EmailAddress = $_POST['Wsmembers']['EmailAddress'];

                                $form->LoginName = $_POST['Wsmembers']['LoginName'];

                                $form->LoginPassword = $pass;

                                $form->MembershipContactName = $_POST['Wsmembers']['MembershipContactName'];

                                $form->MembershipSecretQuestion = $_POST['Wsmembers']['MembershipSecretQuestion'];

                                $form->MembershipSecretAnswer = $_POST['Wsmembers']['MembershipSecretAnswer'];

                                $form->MembershipDateSetup = new CDbExpression('NOW()');

                                $form->save();

                                

                                if($form->save()){



and the email got sent, the only part that I really edited was the model

area, wherein, I added these lines




array('LoginPassword','compare'),

array('LoginPassword_repeat','safe'),

array('EmailAddress, LoginName' , 'unique')



here’s what i saw, when i did your suggestion




Array ( [LoginPassword] => Array ( [0] => Password must be repeated exactly. ) ) 


Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\yii\myapp\protected\controllers\WsmembersController.php:211)



but i did fill in the forms correctly including the repeat password, any idea what’s going on?

You should configure the compare validator in order to the the right comparison:

In your rules:




array('LoginPassword','compare', 'compareAttribute'=>'LoginPassword_repeat'),




I revised my rule now to this




			array('EmailAddress, LoginName, LoginPassword, MembershipContactName, MembershipSecretQuestion, MembershipSecretAnswer', 'required'),

			array('LoginPassword','compare','compareAttribute'=>'LoginPassword_repeat'),

			array('LoginPassword_repeat','required','on'=>'register),



now it always say, password must be repeated exactly even though i typed the exact words :(

I found!!!

We shoud sha1 even LoginPassword_repeat!!

Or, better, we can call:


$form->save(false)

This will prevent the save to call the validation. This is not needed because we have just validation, and validation fails because we sha1 password but not password repeat.

I did




$pass2 = sha1($_POST['Wsmembers']['LoginPassword_repeat']);

$form->WSLoginPassword = $pass;

$form->WSLoginPassword_repeat = $pass2;

....

if($form->save(false))



ti’s still the same…or do you mean I should sha1 in another place ?

i also tried , removing the sha1 from the controller $form and added an afterValidate at the model like this




	protected function afterValidate()

	{

		parent::afterValidate();

		$pass = $this->encrypt($this->LoginPassword);

		$this->LoginPassword = $pass;

		return true;

	}


	protected function encrypt($value)

	{

		return sha1($value);

	}



still no effect :(

save with false, in order not to repeat the validation.




$form->save(false)



Your first validation works, in fact you get in the if and you try to save. The second fails because you encrypt the password, but is not needed to repeat the validation.

Simply do:





if($form->validate())

                        {


                                $form->LoginPassword = sha1($form->LoginPassword);

                                $form->save(false);

                                Yii::app()->user->setFlash('success','Thank you for joining!,Please check your email and activate your account');

                                        

                                $to = $form->email_address;

                                $subject = "Welcome To BuggyMan!";

                                $message = "Thank you for joining!, we have sent you a separate email that contains your activation link";

                                $from = "FROM: buggyman@gmail.com";

                                 [...]

                                        



Thanks for this, but here’s what I did,

I removed the first $form->save

and then leaved the 2nd save that is located inside the if()

problem solved