【问题标题】:PHP Mail() Contact-us form works fine if entering a Gmail sending address, but not with Yahoo [duplicate]如果输入 Gmail 发送地址,PHP Mail() 联系我们的表单可以正常工作,但不能使用 Yahoo [重复]
【发布时间】:2023-04-03 14:22:01
【问题描述】:

我使用 Prestashop 设置了一个电子商务网站,在测试他们的联系表时,我发现如果用户输入 Yahoo 电子邮件地址作为发件人地址,我没有收到任何消息。但是,如果用户输入 Gmail 地址,我没有问题。

Prestashop 当前设置为使用 PHP Mail() 函数作为联系表单。可能是什么问题以及我可以查看哪些解决方案,因为我显然需要接收来自所有人的邮件,而不仅仅是那些有 gmail 地址的人。

以下是contact-form.php页面中的代码:-

<?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');

?> 

更新:
我联系了我的电子邮件托管公司,他们给出了以下建议:-

您需要更改电子邮件 字段 $from 中的地址到任何 域名上的电子邮件地址 你正在合并这个 脚本。例如,如果您的域 名称是 abc.com,然后您将定义 发件人电子邮件地址为 some-name@abc.com。此电子邮件地址 不必存在于邮件中 abc.com 的服务器,但是,域 $from 字段中的名称必须是 你的。您可以使用电子邮件地址 比如 Do_Not_reply@abc.com。

$mailto 字段中的值需要 要更改为电子邮件地址, 包含数据的电子邮件 通过表单提交的需要 已送达。

那么在 Prestashop 的contact-form.php(上面给出的代码)的上下文中,我将如何更改它?

【问题讨论】:

    标签: php email e-commerce contact prestashop


    【解决方案1】:

    PHP mail() 确实是一种发送电子邮件的原始方式。如果您不太了解电子邮件 RFC(标准),那么使用 mail() 很容易搞砸事情......

    我建议您使用PHPMailer(或类似的库)或发布您使用的实际代码。

    【讨论】:

    • 例如,如果您在邮件内容中使用 CRLF (\r\n),这可能会搞砸一些邮件服务器。 \r\n 应仅用作电子邮件标题的“分隔符”,\n 应用作邮件正文(内容)中的换行符。
    • Prestashop 中的 'send email to' 配置仅提供 PHP mail() 函数或 SMTP 选项。
    • 你如何“喂”送什么给 Prestashop(我不知道这个应用程序)。
    • Prestashop 类似于 wordpress、Joomla、Drupal 等。但它专注于电子商务网站。登录管理部分后,您可以调整首选项中的设置。我想显示contact.php的代码,所以你打电话看看,但似乎无法添加它。
    • 通过编辑您的原始问题来添加它。
    【解决方案2】:

    您不能将未绑定到服务器的地址用作发件人地址。这将被每个自尊的垃圾邮件阻止机制阻止。它与 GMail 地址一起工作实际上是一个奇迹。

    如果您希望能够直接回复人们通过您的联系表单发送给您的邮件,请将以下标题添加到您的mail() 呼叫的第 4 个参数:

    reply-to: customers_email_address
    

    但作为物理发件人地址,总是使用

    from: something@yourserver.com
    

    【讨论】:

    • 我的电子邮件托管公司也说了同样的话。但是在上面给出的contact-form.php 代码的上下文中,我需要做什么。下一步是什么?
    • 我不知道,因为我无法确定邮件在您的代码中发送到哪里。对不起。 Prestashop 的配置中的某处应该有一个“发送电子邮件”部分。此时插入标题,必须由更熟悉该商店的人来回答。
    【解决方案3】:

    您可以为mail() 调用提供第五个参数。

    这是我用来修复我的 drupal 邮件的内容(简化版):

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

    由于 AlexV 正确地指出使用非转义值是危险的,因此这是完整版本。请注意,它取自 drupal 资源(有一个小修复)。

        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'] : '') );
        }
    

    希望对您有所帮助, 克里斯

    【讨论】:

      【解决方案4】:

      我一直在使用这个小代码在我自己的通讯系统中发送电子邮件。此摘录专门处理单个消息。它基于 RFC 规范。

      
          /**
           * @package  jaMailBroadcast
           * @author   Joel A. Villarreal Bertoldi (design at joelalejandro.com)
           *
           * @usage    
           *
           *     $msg = new jaMailBroadcast_Message("My Website", "my@website.com");
           *     $msg->to = new jaMailBroadcast_Contact("John Doe", "john@doe.com");
           *     $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;
              }
          }
      
      

      【讨论】:

        【解决方案5】:

        我将“发件人”更改为$_REQUEST['from']。您也可以更改它$_POST['from']。用此替换 2 'from' 并将 $contact-&gt;email 更改为您要发送该电子邮件的任何所需电子邮件。

        它对我有用。

        【讨论】:

          猜你喜欢
          • 2013-12-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-24
          • 1970-01-01
          • 2014-05-07
          • 1970-01-01
          相关资源
          最近更新 更多