symfony发邮件

symfony1.1 and swiftMailer发邮件的代码


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’);
}

RSS feed for comments on this post · TrackBack URL

发表评论