0

The following is from a custom extension a previous developer wrote for our site. Customers can leave comments or questions when they place their order and it gets emailed to our customer service department. Currently the "Sender" address is set to our default transactional email address. I would like to have it sent from the customer's email address so that our customer service department can simply hit reply and send an email in response to the customer.

I attempted to include $customerEmail into the "$vars = array" but it seems I was way off. I tried Googling "Magento customer email field" and a few variations but I can't track down the correct reference. Any direction would be appreciated.

<?php

class Commentemail_Model_Observer {

    private function sendOrderCommentEmail($name, $orderNumber, $comment){
        $receiveEmail = Mage::getStoreConfig('contacts/email/order_comment_email');
        $templateId = "order_comment_email";
        $emailTemplate = Mage::getModel('core/email_template')->loadByCode($templateId);
        $vars = array('customerName' => $name, 'orderNumber' => $orderNumber, 'comment' => $comment);
        $emailTemplate->getProcessedTemplate($vars);
        $storeId = Mage::app()->getStore()->getId();

        $senderEmail = Mage::getStoreConfig('trans_email/ident_general/email', $storeId);
        $senderName = Mage::getStoreConfig('trans_email/ident_general/name', $storeId);

        $emailTemplate->setSenderEmail($senderEmail);
        $emailTemplate->setSenderName($senderName);
        $emailTemplate->send($receiveEmail, $senderName, $vars);
    }

    public function sendCommentEmail($observer){
        if(!Mage::getStoreConfig('contacts/email/enable_comment_sending'))
            return;
        $order = $observer->getOrder();
        $orderNumber = $order->getRealOrderId();
        $params = Mage::app()->getRequest()->getParams();
        if(isset($params['onestepcheckout_comments']) && $params['onestepcheckout_comments'] != ''){
            $this->sendOrderCommentEmail($order->getCustomerName(), $orderNumber, $params['onestepcheckout_comments']);
        }

    }

}
1
  • could you also paste your config.xml here?
    – Dopamedia
    May 30, 2014 at 21:39

1 Answer 1

1

You don't want to actually use the customer's email address as the sender email address, as your server will not have the appropriate authorization to send email using the customer's email address (it will be flagged as spam by most email systems).

Instead you want to set the Reply To using the customer's email address. This will allow you hit reply in your email client and reply to your customer's email address directly.

To do this, you can use the setReplyTo($email) method included in the core/email_template'core/email_template' model.

First, modify your sendOrderCommentEmail() function to accept the $email parameter:

sendOrderCommentEmail($name, $email = false, $orderNumber, $comment){

Then pass the $email variable into the setReplyTo() method:

if($email)
    $emailTemplate->setReplyTo($email);  

Complete modified function:

private function sendOrderCommentEmail($name, $email = false, $orderNumber, $comment){
    $receiveEmail = Mage::getStoreConfig('contacts/email/order_comment_email');
    $templateId = "order_comment_email";
    $emailTemplate = Mage::getModel('core/email_template')->loadByCode($templateId);
    $vars = array('customerName' => $name, 'orderNumber' => $orderNumber, 'comment' => $comment);
    $emailTemplate->getProcessedTemplate($vars);
    $storeId = Mage::app()->getStore()->getId();

    $senderEmail = Mage::getStoreConfig('trans_email/ident_general/email', $storeId);
    $senderName = Mage::getStoreConfig('trans_email/ident_general/name', $storeId);

    if($email)
        $emailTemplate->setReplyTo($email);  

    $emailTemplate->setSenderEmail($senderEmail);
    $emailTemplate->setSenderName($senderName);
    $emailTemplate->send($receiveEmail, $senderName, $vars);
}

Now modify your sendCommentEmail() method to pass the customer's email using $order->getCustomerEmail() into the $this->sendOrderCommentEmail() method:

 $this->sendOrderCommentEmail($order->getCustomerName(), $order->getCustomerEmail(), $orderNumber, $params['onestepcheckout_comments']);

Complete modified function:

public function sendCommentEmail($observer){
    if(!Mage::getStoreConfig('contacts/email/enable_comment_sending'))
        return;
    $order = $observer->getOrder();
    $orderNumber = $order->getRealOrderId();
    $params = Mage::app()->getRequest()->getParams();
    if(isset($params['onestepcheckout_comments']) && $params['onestepcheckout_comments'] != ''){
        $this->sendOrderCommentEmail($order->getCustomerName(), $order->getCustomerEmail(), $orderNumber, $params['onestepcheckout_comments']);
    }

}
5
  • This works perfectly however it seems that Gmail (we have Google Business Apps) doesn't make use of the Reply-To field... new issue. "...12.34.56.78 as permitted sender) client-ip=12.34.56.78; Reply-To: [email protected] To: =?utf-1?BTWF0dCBTd1FuY1hpZQ==?=..."
    – sparecycle
    Jun 3, 2014 at 14:42
  • 1
    Your solution works. Thank you! And here is the solution to Gmail problem if ever you come across it. magento.stackexchange.com/questions/22081/…
    – sparecycle
    Jun 3, 2014 at 15:50
  • Glad it worked, and nice of you to post the tip on the Reply To field with Google apps. I'm sure it will help someone else :)
    – Axel
    Jun 3, 2014 at 16:39
  • Final question: Is it possible to echo the customer's email address as a variable in the transactional email?
    – sparecycle
    Jun 10, 2014 at 21:11
  • Yes. Simply include the customer email in the $vars variable passed into the $emailTemplate->send($receiveEmail, $senderName, $vars); function.
    – Axel
    Jun 30, 2014 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.