2011-07-28 2 views
2

私はaction.class.phpが非常に大きくなっているという質問をしました。便利な回答がたくさんありました。あなたはここで読むことができます:Multiple action.class.phpsymfonyの電子メール - コードはどこに置くべきですか?

action.class.phpが大きくなっているので、そこに多くのものがあるかもしれないという問題があるかもしれません。はい、私はモデルを持っています。私はクエリをlib/model/doctrine /に置くことができ、カップルフォームを動かすことができます。

symfonyでメールに関するアクションを動かす最良の方法は何ですか?コードはどこに置くべきですか? symfonyで電子メールを送る良い方法はありますか?

ありがとうございます!

Craphunter

私の例:

if ($this->form->isValid()) { 
      $formData = $this->form->getValues(); 
      $firstname = $this->userdata->getFirstname(); 
      $lastname = $this->userdate->getLastname(); 

      try { 
       $message = Swift_Message::newInstance() 
         ->setFrom('[email protected]') 
         ->setTo($this->shopdata->getEmail()) 
         ->addReplyTo($this->userdata->getEmail()) 
         ->setSubject($formData['subject']) 
         ->setBody($this->getPartial('emailcontact', $arguments = array(
            'firstname' => $firstname, 
            'lastname' => $lastname, 
            'message' => $formData['message'], 
           ))); 
       $this->getMailer()->send($message); 
       $this->getUser()->setFlash('shop_name', $formData['email']); 
       $this->getUser()->setFlash('name', $formData['name']); 

      } catch (Exception $e) { 

       $this->getUser()->setFlash('mailError', 'Technical Problem'); 
      } 
      $this->redirect('aboutus/sendfeedbackthankyou'); 

答えて

2

私はそれが役に立つsfActionsを拡張するメインクラスで送信するコードを置くことを見つけます。私は通常、プロジェクト全体で使用される一般的なもの(Ajaxの使用を助けるコードなど)をすべてそこに投げます。

特に電子メールのために、私は静的メソッド

public static function mail($options, $mailer) { 

    sfProjectConfiguration::getActive()->loadHelpers('Partial'); 

    $required = array('subject', 'parameters', 'to_email', 'to_name', 'html', 'text'); 
    foreach ($required as $option) 
    { 
     if (!isset($options[$option])) { 
     throw new sfException("Required option $option not supplied to ef3Actions::mail"); 
     } 
    } 

    $address = array(); 
    if (isset($options['from_name']) && isset($options['from_email'])) { 
     $address['fullname'] = $options['from_name']; 
     $address['email'] = $options['from_email']; 
    } else 
     $address = self::getFromAddress(); 


    if(!isset($options['body_is_partial']) || $options['body_is_partial']==true){ 

     $message = Swift_Message::newInstance() 
      ->setFrom(array($address['email'] => $address['fullname'])) 
      ->setTo(array($options['to_email'] => $options['to_name'])) 
      ->setSubject($options['subject']) 
      ->setBody(get_partial($options['html'], $options['parameters']), 'text/html') 
      ->addPart(get_partial($options['text'], $options['parameters']), 'text/plain'); 

    } else { 

     $message = Swift_Message::newInstance() 
      ->setFrom(array($address['email'] => $address['fullname'])) 
      ->setTo(array($options['to_email'] => $options['to_name'])) 
      ->setSubject($options['subject']) 
      ->setBody($options['html'], 'text/html') 
      ->addPart($options['text'], 'text/plain'); 

    } 

    if (isset($options['cc']) && !is_null($options['cc'])) { 
     if (is_array($options['cc'])) { 
     foreach ($options['cc'] as $key => $value) { 
      if (!is_number($key)) 
      $message->addCc($key, $value); 
      else 
      $message->addCc($value); 
     } 
     } elseif (is_string($options['cc'])) { 
     $message->addCc($options['cc']); 
     } 
    } 

    if (isset($options['bcc']) && !is_null($options['bcc'])) { 
     if (is_array($options['bcc'])) { 
     foreach ($options['bcc'] as $key => $value) { 
      if (!is_number($key)) 
      $message->addBcc($key, $value); 
      else 
      $message->addBcc($value); 
     } 
     } elseif (is_string($options['bcc'])) { 
     $message->addBcc($options['bcc']); 
     } 
    } 

    if (isset($options['attachments'])) { 
     $atts = $options['attachments']; 
     if (is_array($atts)) { 
     foreach ($atts as $att) { 
      $message->attach(Swift_Attachment::fromPath($att)); 
     } 
     } elseif (is_string($atts)) { 
     $message->attach(Swift_Attachment::fromPath($atts)); 
     } 
    } 

    try 
    { 
     return $mailer->send($message); 
    } catch (Exception $e) 
    { 
     throw new Exception("Erro ao tentar enviar um email: " . $e->getMessage()); 
    } 

    } 

はなぜ静的メソッドである必要がありますか?まあ、イベントリスナーのような別のコンテキストからもこのメソッドを使用しています。電子メールを送信するための一般的な呼び出しは、次のようになります。

// SEND EMAIL FROM ACTION 
$opts = array(); 
$opts['from_name'] = 'Webmaster'; 
$opts['from_email'] = '[email protected]'; 

$variables = array(); 
// variables passed to the partial template 
$variables['%client_firstname%'] = $firstName; 
$variables['%client_lastname%'] = $lastName; 
$variables['%client_email%'] = $client_email; 
// preenche opcoes de envio 
$opts['parameters'] = array(); 
$opts['body_is_partial'] = false; 
$opts['to_name'] = $variables['%client_firstname%'] . " " . $variables['%client_lastname%']; 
$opts['to_email'] = $variables['%client_email%']; 
$opts['subject'] = __($subject, $variables); 
$opts['html'] = __($message_html, $variables); 
$opts['text'] = __($message_text, $variables); 
if (isset($cc)) $opts['cc'] = $cc; 
$bcc = sfDoctrineKeyValueProjectStore::get("{$contexto}_message_bcc"); 
if (isset($bcc)) $opts['bcc'] = $bcc; 

return dcActions::mail($opts, sfContext::getInstance()->getMailer()); 

また、両方のバージョンの電子メール(HTMLとテキスト)の送信が強制されます。このようなコードを使用すると、パーシャルを使用して電子メールメッセージを配信することもできます。私はそれが非常に便利だと思う。

+0

ありがとうございます。それは役に立ちません! – craphunter

関連する問題