Meome
Meome

Reputation: 11

Magento Contact Form - been receiving email from myself

For some reason, when a customer submit the contact form, it appears in my gmail that the email was sent from myself, not from my customer. Please check out the pictures so you know what I am talking about.

https://i.sstatic.net/QsACc.jpg This image shows the email came from myself

https://i.sstatic.net/nghG2.jpg Look at the arrow, this is what I see every email that comes from the contact from. Same name, same title.

This is really annoying because when many people use this, there is no way I can tell which one is which.

This is my contact page: meome.vn/lien-he There might be some code to put in the email template that I don't know. Anyway if anyone knows how to fix this, please help me. I really appreciate it.

Upvotes: 1

Views: 2984

Answers (2)

Krista K
Krista K

Reputation: 21871

Technically, this is a duplicate of Change 'From' field of magento contact form email to the sender but I put a complete and thorough answer here. A simple core hack is one answer provided there.

I am determined to not hack core files, so I solved this problem by making a custom controller. I read up and it seemed easy enough... and also no extensions on the market offer the goals I set out to accomplish: 1) have customer email & name on the From; 2) simple human input for anti spam; and 3) a couple custom fields, 3 of which are actually product attributes.

The files to accomplish this are:

$ find -type f
./Cycleworks/Cycleworks_ContactExtended.xml
./Cycleworks/ContactExtended/controllers/IndexController.php
./Cycleworks/ContactExtended/Helper/Data.php
./Cycleworks/ContactExtended/etc/config.xml

And now for the files themselves... The first XML file tells Magento about your custom override.

// copy this to app/etc/modules/
./Cycleworks/Cycleworks_ContactExtended.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Cycleworks_ContactExtended>
            <active>true</active>
            <codePool>local</codePool>
        </Cycleworks_ContactExtended>
    </modules>
</config>  

The remainder of the files go in app/code/local/. Below, I copied the postAction() function from the original controller and then added my code at the top and then made the one change in ->sendTransactional()

./Cycleworks/ContactExtended/controllers/IndexController.php
<?php
require_once 'Mage/Contacts/controllers/IndexController.php';
class Cycleworks_ContactExtended_IndexController extends Mage_Contacts_IndexController
{
    public function postAction()
    {
    $post = $this->getRequest()->getPost();
    if ( $post ) {
        if( stripos( $post["people"],"tires") ===FALSE ){
            Mage::getSingleton('customer/session')->addError("Please correctly answer the question to confirm you are human.<br>\"".$this->getRequest()->getPost("people")."\" is not correct.");
                $this->_redirect('*/*/');
                return;         
        }
        $extras=Array( "bike_year","bike_make","bike_model","bike_model_ext" );
        foreach($extras as $field) {
            if( $post[$field] == "empty" )
                $post[$field]= "----";
        }
        $comment = $post['comment']."\nMage::getStoreConfig(self::XML_PATH_EMAIL_SENDER)=\n'".Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER)."'";
        $post['comment']= nl2br($comment);

        $translate = Mage::getSingleton('core/translate');
        /* @var $translate Mage_Core_Model_Translate */
        $translate->setTranslateInline(false);
        try {
            ...
            ...
            ...
            $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                ->setReplyTo($post['email'])
                ->sendTransactional(
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                    array( 'name'=>$post['name'],'email'=> $post['email'] ),        // Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER), //
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                    null,
                    array('data' => $postObject)
                );
            ...
            ...
            ...
    }
}

Nice and empty shell. And yes, these all have opening <?php tags but not closing ones??

./Cycleworks/ContactExtended/Helper/Data.php
<?php
class Cycleworks_ContactExtended_Helper_Data extends Mage_Core_Helper_Abstract
{

}

And the XML in the custom module:

./Cycleworks/ContactExtended/etc/config.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Cycleworks_ContactExtended>
            <version>0.0.01</version>
        </Cycleworks_ContactExtended>
    </modules>
    <frontend>
        <routers>
            <contacts>
                <args>
                    <modules>
                        <Cycleworks_ContactExtended before="Mage_Contacts">Cycleworks_ContactExtended</Cycleworks_ContactExtended>
                    </modules>
                </args>
            </contacts>
        </routers>
    </frontend>
    <global>
        <helpers>
            <contactextended>
                <class>Cycleworks_ContactExtended_Helper</class>
            </contactextended>
        </helpers>        
    </global>
</config>

That's all the backend. As for the form itself, I added this code to form.phtml below the comments block and above the closing </ul> tag. For most people this file is in app/code/design/frontend/default/default/template/contacts. Since I have a TM template that I purchased, mine is in default/a034/template/contacts

<?php
$confirm_people_question="Motorcycles have two of these and rhymes with plires";    // CKCK form anti-spam
?>
<li>
    <label for="people" class="required"><em>*</em><?php echo $confirm_people_question ?></label>
    <div class="input-box">
        <input name="people" id="people" title="Please confirm you are people" value="" class="required-entry input-text" type="text" />
    </div>
</li>
<?php
// from http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/
$wanted=Array("make","model","engine_size");    // note that each attribute needs to be searchable
$attributes = Mage::getModel('catalogsearch/advanced')->getAttributes();    // $productAttrs = Mage::getResourceModel('catalog/product_attribute_collection');
$attributeArray=array();
foreach($attributes as $a){
    if(  in_array( $a->getAttributeCode(), $wanted) ){
        foreach($a->getSource()->getAllOptions(false) as $option){
            $attributeArray[$a->getAttributeCode()][$option['value']] = $option['label'];
        }
    }
}

?>
<li>
    <div class="ymm">
    <label for="bike_year">Year</label><br>
        <select id="year" name="bike_year">
            <option value="empty"></option>
            <?  for( $idx=date("Y"); $idx >= 1985; $idx-- ) 
                    echo "                      <option value=\"$idx\">$idx</option>\n";
            ?>
        </select>
    </div>
    <div class="ymm">
    <label for="bike_make">Make</label><br>
        <select id="make" name="bike_make">
            <option value="empty"></option>
            <?  foreach( $attributeArray['make'] as $id => $brand ) 
                    echo "                      <option value=\"$brand\">$brand</option>\n";
            ?>
        </select>
    </div>
    <div class="ymm">
    <label for="bike_model">Model</label><br>
        <select id="model" name="bike_model">
            <option value="empty"></option>
            <?  foreach( $attributeArray['model'] as $id => $model )    
                    echo "                      <option value=\"$model\">$model</option>\n";
            ?>
        </select>
    </div>
    <div class="ymm">
        <label for="bike_model_ext">More</label>
        <div class="input-box">
            <input type="text" size="15" value="" id="model_ext" name="bike_model_ext" class="input-text">
        </div>
    </div>
</li>

I almost forgot, the final key to the puzzle is the mail template in the admin area: System - Transactional Emails. Find your HTML contact template (or make a new one and do not convert it to plain text) and this is what I've got:

<body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
<div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
<table>
<tr><td>Name</td><td>{{var data.name}}</td></tr>
<tr><td>E-mail</td><td>{{var data.email}}</td></tr>
<tr><td>Telephone</td><td>{{var data.telephone}}</td></tr>
</table>
<P>
<fieldset><legend>Subject: &nbsp; {{var data.subject}}</legend>
{{var data.comment}}
</fieldset>
Bike info: {{var data.bike_year}} {{var data.bike_make}} {{var data.bike_model}} {{var data.bike_model_ext}}
</div>
</body>

I never really thought I would have made my own custom module, but I followed the recipes I found on here and elsewhere and put this together. It also behaves correctly when the capcha fails. I did try to research how to give the option to CC the customer with their own contact, but I couldn't find anything.

Later, I tried to make a custom module to allow an alternate admin notification email template for new orders but the knowledge I had learned from above wasn't enough. :P So my knowledge and comfort with Magento is perhaps above "dangerous hack", but I still have a long way to go.

Upvotes: 1

Palanikumar
Palanikumar

Reputation: 1746

Did you check your email configuration in the backend. Admin -> System -> configuration -> Store Email Addressess and Admin -> System -> configuration -> Contacts -> Email options

Upvotes: 1

Related Questions