Magento添加BCC不起作用

时间:2014-03-20 22:15:47

标签: magento email bcc

您好我写了以下代码,将发送电子邮件,一切正常,但不发送BCC,我不知道是什么导致它不添加BCC。任何帮助表示赞赏。

 public function sendFraudEmail($observer)
    {
    /* @var $mailTemplate Mage_Core_Model_Email_Template */
    $mailTemplate = Mage::getModel('core/email_template');
    $template = Mage::getStoreConfig('sales_email/order/template', Mage::app()->getStore()->getId());
    $template_collection =  $mailTemplate->load($template);
    $template_data = $template_collection->getData();
    $templateId =  Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE);
    $mailSubject = $template_data['template_subject'];
    $sender  = array(
        'name' => Mage::getStoreConfig('trans_email/ident_support/name', Mage::app()->getStore()->getId()),
        'email' => Mage::getStoreConfig('trans_email/ident_support/email', Mage::app()->getStore()->getId()));
    $obj = $mailTemplate->setReplyTo($sender['email'])->setTemplateSubject($mailSubject);
    $vars = NULL;
    $order = $observer->getEvent()->getOrder();
    $storeId          = $order->getStoreId();
    $IncrementId = $order->getIncrementId();
    $status = $order->getStatus();
    $customer_name = $order->getCustomerName();
    $customer_email = $order->getCustomerEmail();
     /*ADD BCC*/
    $copyTo = explode(",",Mage::getStoreConfig(self::XML_PATH_EMAIL_BCC));
    if (!empty($copyTo) && isset($copyTo)) {
        // Add bcc to customer email
        foreach ($copyTo as $bccemail) {
            $mailTemplate->addBcc($bccemail);
        }
    }try{
        $obj->sendTransactional($templateId, $sender, $customer_email,customer_name, $vars, $storeId);
    } catch (exception $e){
        Mage::log("something went wrong.." . $e->getMessage());
    }

2 个答案:

答案 0 :(得分:1)

我实际上遇到过类似的问题一次。我理解它的方式,你需要在使用BCC时向电子邮件标题发送信息。这是我的类似代码的副本,其中包含对我遇到的问题的评论:

    // Get the destination email addresses to send copies to
    $copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);

    // Retrieve corresponding email template id and customer name
    $templateId = Mage::getStoreConfig($emailTemplate);
    $mailer = Mage::getModel('core/email_template_mailer');

    //Set it to send it to the user.
    $emailInfo = Mage::getModel('core/email_info');
    $emailInfo->addTo($emailTo);
    $mailer->addEmailInfo($emailInfo);

    if ($copyTo) {
        foreach ($copyTo as $email) {
            $emailInfo = Mage::getModel('core/email_info');
//                *Just* using add Bcc throws an exception which asks for a "To" field.      Because all the emails are
//                sent separately (not one mass email), To, CC, and BCC are all essentially the same thing
//                If for some reason you need CC or BCC, you will likely need to set something to the To header.
//                $emailInfo->addBcc($email);
            $emailInfo->addTo($email);
            $mailer->addEmailInfo($emailInfo);
        }
    }

答案 1 :(得分:0)

我认为你在这里弄错了

  $copyTo = explode(",",Mage::getStoreConfig(self::XML_PATH_EMAIL_BCC)); 

  if (!empty($copyTo) && isset($copyTo)) { 
      // Add bcc to customer email 
       foreach ($copyTo as $bccemail) {  
            $mailTemplate->addBcc($bccemail); 
  }

$copyTo返回一个数组值。您可以在没有foreach的情况下添加。

所以你需要改变这样的代码。

 if (!empty($copyTo) && isset($copyTo)) { 
      // Add bcc to customer email 
            $mailTemplate->addBcc($copyTo);
  }

希望你能得到输出。