Question

This is a bit similar to another post but different if that makes sense. We have a custom phtml page contact form. Our issue is when it sends email we are only getting one of the variables to be displayed in the email and none of the others. Specifically the persons first and last name is displayed in the email our staff get but none of the email variables. Below is our code.

<style type="text/css">
    @media all and (max-width: 600px) {
        #lftimg {
            display: none;
        }
    }
</style>
<?php

// Or whatever the path to your app/Mage.php happens to be ...
require_once('app/Mage.php');

// Initialize Magento ...
Mage::app("default");

// Email Form Template ID in magento admin transactional emails admin page   
$templateId = 16;

//lets see what site (US or SG) the user is on.
$show_singapore = 'false';
if (Mage::app()->getWebsite()->getId() != 1) {
    $show_singapore = 'true';
}

?>
<div class="page-title">
    <h1>
        CONTACT US
    </h1>

    <p>We would love to hear from you!</p>
</div>
<div style='float:left; padding-right:10px; width:90%;'><br/><br/>
    <?php
    if (isset($_POST['sendit'])) {

        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $fullname = "$first_name $last_name";
        $emailadr = $_POST['emailadr'];
        $telephonenum = $_POST['telephonenum'];
        $comment = $_POST['comment'];
        if ($first_name == '' || $emailadr == '' || $comment == '') {
            echo "All fields are required, please fill out the <a href=\"\">the form</a> again.";
        } else {

            $confirmmsg = "Hello $first_name $last_name,<br /><br />
                                 Thank you for contacting our company. We have received your email and will contact you back shortly.";

            // Set recepient information
            if ($show_singapore == 'true') {
                $headers = "From: staff@singapore.com\r\n";
            } else {
                $headers = "From: staff@usa.com\r\n";
            }

            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
            mail($email, $subject, $confirmmsg, $headers);

            // Set sender information
            $senderName = 'Customer Service';
            $senderEmail = 'staff@usa.com';
            $sender = array('name' => $senderName, 'email' => $senderEmail);

            // Set recepient information
            if ($show_singapore == 'true') {
                $recepient_email = 'staff@singapore.com';
            } else {
                $recepient_email = 'staff@usa.com';
            }
            $recepient = array('name' => 'Customer Service', 'email' => $recepient_email);

            // Get Store ID
            $store = Mage::app()->getStore()->getId();

            // Set variables that can be used in email template
            $var = array(
                'name' => $fullname,
                'emailadr' => $emailadr,
                'telephonenum' => $telephonenum,
                'comment' => $comment
            );


            $translate = Mage::getSingleton('core/translate');

            // Send Transactional Email
            try {
                Mage::getModel('core/email_template')->sendTransactional($templateId, $sender, $recepient_email, $var);
            } catch (Exception $e) {
                print_r($e);
            }

        }

        echo "<div id='messages_product_view'>Your message has been sent!</div>";
    } else {
        ?>
        <form action="<?php $_SERVER["PHP_SELF"] ?>" method="POST">
            <div class="row">
                <div class="col-sm-6">
                    <label class="required"><em>*</em> <?php echo Mage::helper('contacts')->__('FIRST NAME') ?></label>

                    <div class="input-box">
                        <input name="first_name" id="first_name" class="input-text required-entry" type="text"/>
                    </div>
                </div>
                <div class="col-sm-6">
                    <label class="required"><em>*</em> <?php echo Mage::helper('contacts')->__('LAST NAME') ?></label>

                    <div class="input-box">
                        <input name="last_name" id="last_name" type="text" class="input-text required-entry"/>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <label
                        for="telephone"><?php echo Mage::helper('contacts')->__('TELEPHONE NUMBER (OPTIONAL)') ?></label>

                    <div class="input-box">
                        <input name="telephonenum" id="telephonenum"
                               title="<?php echo Mage::helper('contacts')->__('Telephone') ?>" class="input-text"
                               type="text"/>
                    </div>
                </div>
                <div class="col-sm-6">
                    <label for="email"
                           class="required"><em>*</em> <?php echo Mage::helper('contacts')->__('EMAIL ADDRESS') ?>
                    </label>

                    <div class="input-box">
                        <input name="emailadr" id="emailadr" title="<?php echo Mage::helper('contacts')->__('Email') ?>"
                               class="input-text required-entry validate-email" type="text"/>
                    </div>
                </div>
            </div>
            <div class="row ">
                <div class="col-sm-12">
                    <label for="comment"
                           class="required"><em>*</em> <?php echo Mage::helper('contacts')->__('MESSAGE') ?></label>

                    <div class="input-box">
                        <textarea name="comment" id="comment"
                                  title="<?php echo Mage::helper('contacts')->__('Comment') ?>"
                                  class="required-entry input-text" cols="5" rows="7"></textarea>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12">
                    <div class="buttons-set">
                        <p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
                        <input type="text" name="hideit" id="hideit" value="" style="display:none !important;"/>
                        <button name="sendit" type="submit" title="<?php echo Mage::helper('contacts')->__('SEND') ?>"
                                class="button"><span><span
                                    style="padding-left:40px; padding-right:40px;"><?php echo Mage::helper('contacts')->__('SEND') ?></span></span>
                        </button>
                    </div>
                </div>
            </div>
        </form>
        <?php
    }
    ?>
</div>
<div style='float:left; width:10%' id="lftimg">
    <img title="Contact Us" src="<?php echo $this->getUrl('') ?>media/wysiwyg/contactimage.jpg" alt="Contact Us"
         style='float:left'/>
</div>

My transnational template in Magento looks like the below. Again the strange part is the name gets populated in the email that is sent without any issues just not the other fields. You can see how I have the below code in my Transactional email template in Magento via the screen shot http://postimg.org/image/6sln10007/

Name: {{var name}}
</br>
 E-mail: {{var emailadr}}
 </br>
Telephone: {{var telephonenum}}
 </br>
Comment: {{var comment}}
Was it helpful?

Solution

You did not include your template as a whole, but my best guess is you did not declare those variables in your template.

The template-specific variables need to be included in the <!--@vars @--> comment at the top of the template.

<!--@vars
{"var name":"Sender Name",
"var emailadr":"Sender Email",
"var telephonenum":"Sender Telephone",
"var comment":"Comment"}
@-->
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top