Here’s a new problem : I want to be able to send mails using a template system. I use this for registration purepose mostly (email account confirmation, registration, etc.). Here’s a function that does this with Zend_Mail :

  1. public function sendEmail($arguments)
  2. {
  3. // Get the template arguments
  4. $template  = $arguments[‘template’];
  5. $subject  = $arguments[‘subject’];
  6. $templatearguments = $arguments[‘templateArguments’];
  7. // build a view
  8. $view = new Zend_View;
  9. $view->setScriptPath(Zend_Registry::get(‘config’)->email->folder);
  10. // Assign each item
  11. foreach($templatearguments as $k => $v){
  12. $view->assign($k, $v);
  13. }
  14. // render the template
  15. $body = $view->render($template);
  16. // Send the password by mail ;
  17. // NOTE : the message is in the $body variable (in the mail)
  18. $mail = new Zend_Mail();
  19. $mail->setBodyText($body)
  20. ->setFrom(“me@fromme.com”,”Z”)
  21. ->addTo($this->email)
  22. ->setSubject($subject);
  23. $transport = new Zend_Mail_Transport_Smtp(“my.smtp.server.com”);
  24. $logger = Zend_Registry::get(‘logger’);
  25. // Try to send the email, or log the exception
  26. try
  27. {
  28. $mail->send($transport);
  29. $logger->notice(“mail sent to ” . $this->email);
  30. }
  31. catch (Exception $e)
  32. {
  33. $logger->warn(“Error sending mail “. $e->getMessage());
  34. }
  35. }

Ok, so how would I use this ? First set up the arguments, and then use the function…

  1. $emailarguments = array(‘template’ => ‘userRegistrationEmail.phtml’,
  2. ‘subject’  => ‘Account creation’,
  3. ‘templateArguments’ => array(‘uid’ => $user->Id(),
  4. ‘key’ => $user->key,
  5. ’email’=> $user->email)
  6. );
  7. // ok done, now send the mail
  8. sendEmail($emailarguments)

Let’s find out how to format the mail, well simply like this :

  1. $body = “\n Dear user,”;
  2. $body .= “\n”;
  3. $body .= “\nThank you for your registration.  :”;
  4. (…)
  5. $body .= “\nWeb Site Administrator”;
  6. echo $body;