user1920187
user1920187

Reputation: 870

Magento adding BCC is not working

Hi I have written below code that will send an email out, everything works, except its not sending the BCC out, I am not sure what its causing it not to add BCC. any help is appreciated.

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

Upvotes: 1

Views: 2407

Answers (2)

123
123

Reputation: 1065

I actually ran into a similar problem once. The way I understand it, you need to send information to the email headers when using BCC. Here is a copy of my similar code, with the comments on the issue I ran in to:

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

Upvotes: 1

MeenakshiSundaram R
MeenakshiSundaram R

Reputation: 2837

I think you made mistake here

  $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 return a array value. You can just add that without foreach.

so you need to change the code like this.

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

hope you can get the output.

Upvotes: 0

Related Questions