Jump to content
Larry Ullman's Book Forums

Hi Oop Experts - Please Help Me - I Am Stucked.


Recommended Posts

Hi,

 

There is a new version of PHPMailer released (v. 5.2.2).

 

It seems this new version is working well with MetYii's wrapper but I can't figure out how to use a great new PHPMailer 'callback' feature.

 

PHPMailer documents it only by example:

 

/* Callback (action) function
*   bool    $result	    result of the send action
*   string  $to		    email address of the recipient
*   string  $cc		    cc email addresses
*   string  $bcc		   bcc email addresses
*   string  $subject	   the subject
*   string  $body		  the email body
* @return boolean
*/

require_once '../class.phpmailer.php';
$mail = new PHPMailer();

function callbackAction ($result, $to, $cc, $bcc, $subject, $body) {
 /*
 this callback example echos the results to the screen - implement to
 post to databases, build CSV log files, etc., with minor changes
 */
 $to  = cleanEmails($to,'to');
 $cc  = cleanEmails($cc[0],'cc');
 $bcc = cleanEmails($bcc[0],'cc');
 echo $result . "\tTo: "  . $to['Name'] . "\tTo: "  . $to['Email'] . "\tCc: "  . $cc['Name'] . "\tCc: "  . $cc['Email'] . "\tBcc: "  . $bcc['Name'] . "\tBcc: "  . $bcc['Email'] . "\t"  . $subject . "<br />\n";
 return true;
}

try {
 $mail->IsMail(); // telling the class to use SMTP
 $mail->SetFrom('you@yourdomain.com', 'Your Name');
 $mail->AddAddress('another@yourdomain.com', 'John Doe');
 $mail->Subject = 'PHPMailer Lite Test Subject via Mail()';
 $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
 $mail->MsgHTML(file_get_contents('contents.html'));
 $mail->AddAttachment('images/phpmailer.gif');	  // attachment
 $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
 $mail->action_function = 'callbackAction';
 $mail->Send();
 echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
 echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
 echo $e->getMessage(); //Boring error messages from anything else!
}

 

You can see a callback function and e-mail sending block.

 

When using a wrapper, sending e-mail looks quite similar:

 

Yii::app()->mailer->IsSMTP();
		    try{
			    Yii::app()->mailer->Host = Yii::app()->params['mail']['smtp_host'];
			    Yii::app()->mailer->Encoding = Yii::app()->params['mail']['smtp_encoding'];
			    Yii::app()->mailer->CharSet = Yii::app()->params['mail']['smtp_charset'];
			    Yii::app()->mailer->WordWrap = Yii::app()->params['mail']['smtp_word_wrap'];
			    Yii::app()->mailer->From = $model->email;
			    Yii::app()->mailer->FromName = $name;
			    Yii::app()->mailer->AddReplyTo($model->email);
			    Yii::app()->mailer->AddAddress(Yii::app()->params['adminEmail']);
			    Yii::app()->mailer->Subject = $subject;
			    Yii::app()->mailer->getView('contact_form', array('name'=>$model->name, 'email'=>$model->email, 'subject'=>$model->subject, 'body'=>$body), 'main');

			    Yii::app()->mailer->action_function = 'mailerCallback';

			    Yii::app()->mailer->Send();

		    }catch(phpmailerException $e){
			    Yii::app()->user->setFlash('contact', $e->errorMessage());
		    }catch(Exception $e){
			    Yii::app()->user->setFlash('contact', $e->getMessage());
		    }
		    $this->refresh();

           }

 

You may have notice I already added:

 

Yii::app()->mailer->action_function = 'mailerCallback';

 

but... I have no idea where to put the 'mailerCallback' function within Yii application context. I tried to add it to controller class but it does not work this way.

 

Can you help me please?

Rgs,

Ziggi

Link to post
Share on other sites

You have to dig into the source code of PHP mailer a little bit. This is what I found:

 

 /**
  * Callback Action function name.
  * The function that handles the result of the send email action.
  * It is called out by Send() for each email sent.
  *
  * Value can be:
  * - 'function_name' for function names
  * - 'Class::Method' for static method calls
  * - array($object, 'Method') for calling methods on $object
  * See http://php.net/is_callable manual page for more details.
  *
  * Parameters:
  *   bool    $result        result of the send action
  *   string  $to            email address of the recipient
  *   string  $cc            cc email addresses
  *   string  $bcc           bcc email addresses
  *   string  $subject       the subject
  *   string  $body          the email body
  *   string  $from          email address of sender
  * @var string
  */
 public $action_function = ''; //'callbackAction';

 

In other words, it will invoke a function upon $mail->Send(); You should store the result in a variable and print it for response purposes. It will give you general information about the mail being sent.

 

As you can also see, this property says to look for "is_callable()" in the manual. I take from that they use a function like call_user_func() with your method name. For this to work, the function (or static function call) must obviously be declared (or required/called upon) in the same file. That is why you can see the function "callBackAction" above in your code.

 

Do you have enough sources to get this to work now? Alternatives are placing this function below your controller definition or create a static "helper" class. I would go for this last approach, where you could also wrap this wrapper code. (Yo dog, we heard you like wrappers, so we wrapped a wrapper in a wrapper.) That way, it should be very clean:

 

Lots of into on different ways to create small helpers/helper function and how to call them here:

- http://www.yiiframew...-functions/#hh1

  • Upvote 1
Link to post
Share on other sites

They do it the way you guessed:

 

protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body, $from=null) {
   if (!empty($this->action_function) && is_callable($this->action_function)) {
  $params = array($isSent, $to, $cc, $bcc, $subject, $body, $from);
  call_user_func_array($this->action_function, $params);
   }
}

 

The helper function is the right "Yii-istic" way to go - thank you once again, Antonio.

 

BTW - did you study IT on the university or you've learnt it all by your own? I would say... I like the way you explain - it is more about "how to understand the code and solve the problem" rather than "how to solve the problem only". I am missing academic background and too often get lost in various scenarios...

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...