Snippets

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

Navigation

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 

Comments on this snippet

gravatar icon
#1 marvin ochieng' on 2009-07-02 at 09:47

hi Susan, Thanks alot for your snippet, it got me out of the bush. However, whenever i try to send my mail, i get everything except the message..im posting my snippet below so you show me where im going wrong..thanks in advance: there was also an error, Action "contact/sendEmailHTML" does not exist. i guessed than you meant the function executeSendHTMLText(), and renamed this line: // htmlversion $msg->attach(new Swift_Message_Part( sfContext::getInstance()->getController()->getPresentationFor( 'tellafriend', 'sendHTMLText', $viewName = null), "text/html") )

here's my snippet:

// header $from = new Swift_Address( $values['email'], $values['names']); $to = new Swift_address( 'marvin@marvin.com', 'e-Registry'); // subject/message $msg = new Swift_Message( $values['subject'], '', 'contact'); // 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( 'contact', 'sendEmailText', $viewName = null) ));
        // htmlversion
        $msg->attach(new Swift_Message_Part( sfContext::getInstance()->getController()->getPresentationFor( 'contact', 'sendHTMLText', $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();
        $this->redirect('/contact/submit');
 
You need to create an account or log in to post a comment or rate this snippet.