2011-07-28 4 views
2

action.class.php가 매우 커지고 있다는 질문을했습니다. 유용한 답변이 많았습니다. 여기에서 읽을 수 있습니다 : Multiple action.class.php심포니의 전자 메일 - 코드를 어디에 두어야합니까?!

action.class.php가 커지면서 거기에 많은 것들이있을 수 있습니다. 예, 모델이 있습니다. 나는 질의를 lib/model/doctrine /에 넣을 수 있고 몇 개의 폼을 옮길 수있다.

심포니에서 이메일 관련 조치를 취하는 가장 좋은 방법은 무엇입니까? 코드를 어디에 넣어야합니까? 심포니에서 이메일을 보내는 더 좋은 방법이 있습니까?

감사합니다.

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를 확장하는 기본 클래스에서 보내는 코드를 넣어 찾을 수 있습니다. 일반적으로 프로젝트 전반에 걸쳐 사용되는 모든 것을 던져 버립니다 (아약스 사용에 도움이되는 코드 등).

특히 이메일, 나는 정적 메서드

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

    } 

왜 정적 방법 있나요? 음, 이벤트 리스너와 같은 다른 컨텍스트에서도이 메서드를 사용했습니다. 또한 이메일 (HTML 및 텍스트)의 두 버전을 보내는의 사용을 강제

// 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()); 

같은 이메일을 보내는 전형적인 전화 보인다. 이러한 코드를 사용하면 partial을 사용하여 전자 메일 메시지를 전달할 수도 있습니다. 나는 그것을 매우 유용하다고 느낀다.

+0

감사합니다. 유용하지 않습니다. – craphunter

관련 문제