Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Refine Tags

Snippets tagged "symfony11" Snippets tagged "symfony11"

mail with symfony1.1 and swiftMailer

If you looking for an example to send an email from a tell-a-friend-form or other forms. this might help you:

function executeSend()
{
  [..] // your new sfForm-vodoo [..]
  // after posting form and validation
  $values = $this->form->getValues();
 
 // header
  $from = new Swift_Address( $values['email'], $values['firstname'] . ' ' . $values['lastname'] );
  $to   = new Swift_address( $values['rcpt_email'], $values['rcpt_name']);
  // subject/message
  $msg  = new Swift_Message( __( 'tellafriend.email.subject', '', 'tellafriend') ); // at this moment no body-text etc, we want multipart ...
 
  // content
  $placeholder = array();
  foreach ( $values as $key => $val ) {
    $placeholder['%'.$key.'%'] = $val;
  }
 
  // stuff, we need in the email
  sfContext::getInstance()->getRequest()->setAttribute( 'placeholder', $placeholder );
 
  // textversion
  $msg->attach(new Swift_Message_Part( sfContext::getInstance()->getController()->getPresentationFor( 'tellafriend', 'sendEmailText', $viewName = null) ));
  // htmlversion
  $msg->attach(new Swift_Message_Part( sfContext::getInstance()->getController()->getPresentationFor( 'tellafriend', 'sendEmailHTML', $viewName = null), "text/html") );
 
  $stream = $msg->build();
  //echo $stream->readFull(); die; //Dumps the email contents
 
  // and send
  $swift = new Swift(new Swift_Connection_NativeMail());
  $swift->send( $msg, $to, $from );
  $swift->disconnect();
 
}
 
function executeSendEmailText()
{
  // holds your text-version-template
  $this->placeholder = $this->getRequest()->getAttribute('placeholder');
}
 
function executeSendHTMLText()
{
  // holds your html-version-template
  $this->placeholder = $this->getRequest()->getAttribute('placeholder');
}
 

full documentation for swiftMailer you find here: http://www.swiftmailer.org/wikidocs/

by Susan Lau on 2008-04-15, tagged sfmail  swift  symfony11