views:

1320

answers:

6

I've setup an ecommerce site using Prestashop and when testing their contact form, I found that I was not receiving any messages if user enters Yahoo email address as the senders address. I have no problems, however, if the user enters a Gmail address.

Prestashop is set up currently to use the PHP Mail() function for the contact form. What could be the problem and what solutions could I look at as I obviously need to receive mails from everyone, not just those with gmail addresses.

The following is the code in the contact-form.php page:-

<?php

$useSSL = true;

include(dirname(__FILE__).'/config/config.inc.php');
include(dirname(__FILE__).'/header.php');

$errors = array();

$smarty->assign('contacts', Contact::getContacts(intval($cookie->id_lang)));

if (Tools::isSubmit('submitMessage'))
{
    if (!($from = Tools::getValue('from')) OR !Validate::isEmail($from))
        $errors[] = Tools::displayError('invalid e-mail address');
    elseif (!($message = nl2br2(Tools::getValue('message'))))
        $errors[] = Tools::displayError('message cannot be blank');
    elseif (!Validate::isMessage($message))
        $errors[] = Tools::displayError('invalid message');
    elseif (!($id_contact = intval(Tools::getValue('id_contact'))) OR !(Validate::isLoadedObject($contact = new Contact(intval($id_contact), intval($cookie->id_lang)))))
        $errors[] = Tools::displayError('please select a contact in the list');
    else
    {
        if (intval($cookie->id_customer))
            $customer = new Customer(intval($cookie->id_customer));
        if (Mail::Send(intval($cookie->id_lang), 'contact', 'Message from contact form', array('{email}' => $_POST['from'], '{message}' => stripslashes($message)), $contact->email, $contact->name, $from, (intval($cookie->id_customer) ? $customer->firstname.' '.$customer->lastname : $from)))
            $smarty->assign('confirmation', 1);
        else
            $errors[] = Tools::displayError('an error occurred while sending message');
    }
}

$email = Tools::safeOutput(Tools::getValue('from', ((isset($cookie) AND isset($cookie->email) AND Validate::isEmail($cookie->email)) ? $cookie->email : '')));
$smarty->assign(array(
    'errors' => $errors,
    'email' => $email
));

$smarty->display(_PS_THEME_DIR_.'contact-form.tpl');
include(dirname(__FILE__).'/footer.php');

?>
+1  A: 

PHP mail() is really a raw way to send emails. It's quite easy to screw up things with mail() if you don't know well the email RFCs (standards)...

I suggest you to use PHPMailer (or similar librairies) or post the actual code your using.

AlexV
For example if you use CRLF (\r\n) in the message content this can screw up some mail servers. \r\n should be only used as "separator" for email headers and \n as new line character in message body (content).
AlexV
The 'send email to' configuration in Prestashop only really gives option of PHP mail() function or SMTP.
nitbuntu
And how are you "feeding" what to send to Prestashop (I don't know this app).
AlexV
Prestashop is like wordpress, Joomla, Drupal etc. But it concentrates on e-commerce sites. Once you log into the admin section, you can ajust settings in preferences. I want to display the code for contact.php so you call have have a look, but can't seem to be able to add it.
nitbuntu
Add it by editing your original question.
AlexV
I've added it now. I wasn't able to do it before as I wasn't highlighting the code properly.
nitbuntu
+1  A: 

You can't use addresses that are not bound to your servers as sender addresses. This will be blocked by every self-respecting spam blocking mechanism. It's actually a miracle that it works with GMail addresses.

If you want to be able to directly reply to mails that people send to you through your contact form, add the following header to the 4th parameter to your mail() call:

reply-to: customers_email_address

but as the physical sender address, always use

from: [email protected]
Pekka
My email hosting company said the same thing. But in the context of the contact-form.php code given above, what would I need to do. What would be the next step?
nitbuntu
I don't know, as I can't really tell where the mail is sent in your code. Sorry. There should be a "send email from" section somewhere in the configuration of Prestashop. At which point to insert the headers, somebody more familiar with that shop will have to answer.
Pekka
A: 

There's a fifth parameter you can supply to the mail() call.

Here is what I used to fix my drupal mail (simplified):

return @mail($message['to'],
             $message['subject'],
             $message['body'],
             join("\n", $mimeheaders),
             '-f' . $message['from'] );

Since AlexV correctly remarked that using unescaped values is dangerous, this is the full version. Please note that it is taken from the drupal sources (with a small fix).

    function drupal_mail_wrapper($message)
    {
        $mimeheaders = array();

        foreach ($message['headers'] as $name => $value) 
        {
            $mimeheaders[] = $name .': '. mime_header_encode($value);
        }

        return @mail(
            $message['to'],
            mime_header_encode($message['subject']),
            // Note: e-mail uses CRLF for line-endings, but PHP's API requires LF.
            // They will appear correctly in the actual e-mail that is sent.
            str_replace("\r", '', $message['body']),
            // For headers, PHP's API suggests that we use CRLF normally,
            // but some MTAs incorrecly replace LF with CRLF. See #234403.
            join("\n", $mimeheaders),
            ($message['from'] ? '-f' . $message['from'] : '') );
    }

Hope that helps, Chris

mnemosyn
A: 

I've been using this small code to send e-mails in a newsletter system of my own. This excerpt handles specifically a single message. It's based on RFC specs.


    /**
     * @package  jaMailBroadcast
     * @author   Joel A. Villarreal Bertoldi (design at joelalejandro.com)
     *
     * @usage    
     *
     *     $msg = new jaMailBroadcast_Message("My Website", "[email protected]");
     *     $msg->to = new jaMailBroadcast_Contact("John Doe", "[email protected]");
     *     $msg->subject = "Something";
     *     $msg->body = "Your message here. You <b>can use</b> HTML.";
     *     $msg->send();
     **/

    class jaMailBroadcast_Contact
    {
        public $id;
        public $name;
        public $email;

        function __construct($contactName, $contactEmail, $contactId = "")
        {
            $this->name = $contactName;
            $this->email = $contactEmail;
            if (!$contactId)
                $this->id = uniqid("contact_");
            else
                $this->id = $contactId;
        }

        function __toString()
        {
            return json_encode
            (
                array
                (
                  "id" => $this->id,
                  "name" => $this->name,
                  "email" => $this->email
                )
            );
        }

        function rfc882_header()
        {
            return sprintf('"%s" ', $this->name, $this->email);
        }
    }

    class jaMailBroadcast_Message
    {
        public $from;
        public $to;
        public $subject;
        public $body;
        public $mime_boundary;
        public $headerTemplate;
        public $footerTemplate;

        function __construct($fromName, $fromEmail)
        {
            $this->from = new jaMailBroadcast_Contact($fromName, $fromEmail);
            $this->mime_boundary = "==" . md5(time());
        }

        private function mail_headers($EOL = "\n")
        {
            $headers
              = "From: " . $this->from->rfc882_header() . $EOL
              . "Reply-To: from->email . ">" . $EOL
              . "Return-Path: from->email . ">" . $EOL
              . "MIME-Version: 1.0" . $EOL
              . "Content-Type: multipart/alternative; boundary=\"{$this->mime_boundary}\"" . $EOL
              . "User-Agent: jaMailBroadcast/1.0" . $EOL
              . "X-Priority: 3 (Normal)" . $EOL
              . "Importance: Normal" . $EOL
              . "X-Mailer: jaMailBroadcast";

            return $headers;
        }

        private function rfc882_body_format($EOL = "\r\n")
        {
          return wordwrap($this->body, 70, $EOL);
        }

        function send()
        {
            $EOL =
                 (
                    stripos($this->to->email, "hotmail") !== false
                  ||
                    stripos($this->to->email, "live") !== false
                 )
                  ? "\n"
                  : "\n";

            return mail
            (
                $this->to->rfc882_header(),
                $this->subject,
                $this->multipart_alternative_body($EOL),
                $this->mail_headers($EOL),
                "-f" . $this->from->email
            );
        }

        private function multipart_alternative_body($EOL = "\r\n")
        {
            $multipart
                    = "Content-Transfer-Encoding: 7bit" . $EOL
                    . "This is a multi-part message in MIME format. This part of the E-mail should never be seen. If you are reading this, consider upgrading your e-mail client to a MIME-compatible client." . $EOL . $EOL
                    = "--{$this->mime_boundary}" . $EOL
                    . "Content-Type: text/plain; charset=iso-8859-1" . $EOL
                    . "Content-Transfer-Encoding: 7bit" . $EOL . $EOL
                    . strip_tags($this->br2nl($this->headerTemplate)) . $EOL . $EOL
                    . strip_tags($this->br2nl($this->body)) . $EOL . $EOL
                    . strip_tags($this->br2nl($this->footerTemplate)) . $EOL . $EOL
                    . "--{$this->mime_boundary}" . $EOL
                    . "Content-Type: text/html; charset=iso-8859-1" . $EOL
                    . "Content-Transfer-Encoding: 7bit" . $EOL . $EOL
                    . $this->headerTemplate . $EOL
                    . $this->body . $EOL
                    . $this->footerTemplate . $EOL
                    . "--{$this->mime_boundary}--" . $EOL;

            return $multipart;
        }

        private function br2nl($text, $EOL = "\n")
        {
            $text = str_ireplace("<br>", $EOL, $text);
            $text = str_ireplace("<br />", $EOL, $text);
            return $text;
        }
    }

Joel Alejandro
A: 

I contacted my email hosting company and they gave the following suggestion:-

You would need to change the Email address in the field $from to any Email address on the domain name on which you are incorporating this script. For example, if your Domain Name is abc.com, then you would define the From Email address as [email protected]. This Email address need not be existing on the Mail Server of abc.com, however, the domain name in the $from field has to be yours. You may use an Email address such as [email protected].

The value in the $mailto field needs to be changed to the Email address, where email containing the data submitted through the form needs to be delivered.

So in the context of Prestashop's contact-form.php (code given above), how would I go about changing it?

nitbuntu
A: 

The solution for this has been found as this questions is almost identical to:- http://stackoverflow.com/questions/2245506/reconfiguring-php-mail-smarty-contact-form.

nitbuntu