以php联系表格回复地址 [英] reply-to address in php contact form

查看:140
本文介绍了以php联系表格回复地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我买了一个简单的网页模板,并附上了一个php联系表单。一切工作都很好,一个很小的例外是实际接收通过表单发送的消息。也就是说,联系表单将显示一条成功消息,但该消息将永远不会到达。



在我的托管服务经过漫长的回落之后,我发现在为了避免欺骗,他们不会允许邮件发送到他们不承载的FROM地址。也就是说,如果网站的访问者以表单的形式写下了他的Gmail /雅虎等,我将不会得到它。



他们建议使用托管的电子邮件地址将其作为FROM地址,并将访客的输入电子邮件作为REPLY-TO地址。这似乎是合理的。



所以我挖了(例如这里:
PHP回复错误 - 管理员电子邮件不是联系表单的发件人

php联系表格在网站和回复 - 电子邮件



,答案建议添加标题组件:

  $ headers ='From:webmaster@example.com'。 \\\\。 
'Reply-To:webmaster@example.com'。 \\\\。
'X-Mailer:PHP /'。 phpversion();

,并将其添加到

  mail($ to,$ subject,$ message,$ headers); 

所以这就是我做的。
$ email在这个模板中定义为访客的电子邮件,所以我做的是:

  $ subject = 联络表:$ name; 
$ message =$ message;
$ headers ='From:myemail@my_domain.com'。 \\\\。
'回复:$ email'。 \\\\。
'X-Mailer:PHP /'。 phpversion();

mail($ to,$ subject,$ message,$ headers);

这个都很好,很漂亮,但仍然不能正常工作。
电子邮件 do 现在,但详细信息是:

  from:myemail @ my_domain.com via servername.hosting_company.com 
**回复:$email@servername.hosting_company.com**
to:myemail@my_domain.com

所以,回复地址仍然不是访客离开的。



帮我这个吗不知道还能做些什么。



非常感谢!






如果有人有兴趣,这里是完整的php文件:

 <?php 

//清理输入值
foreach($ _ POST as $ key => $ value){
if(ini_get('magic_quotes_gpc'))
$ _POST [$ key] =的stripslashes($ _ POST [$键]);

$ _POST [$ key] = htmlspecialchars(strip_tags($ _ POST [$ key]));
}

//将输入值分配给变量以便于引用
$ name = $ _POST [name];
$ email = $ _POST [email];
$ message = $ _POST [message];

//测试错误的输入值
$ errors = array();
if(strlen($ name)< 2){
if(!$ name){
$ errors [] =你必须输入一个名字。
} else {
$ errors [] =名称必须至少为2个字符。
}
}
如果(!$ email){
$ errors [] =您必须输入电子邮件。
} else if(!validEmail($ email)){
$ errors [] =您必须输入有效的电子邮件。
}
if(strlen($ message)< 10){
if(!$ message){
$ errors [] =您必须输入一条消息。
} else {
$ errors [] =消息必须至少为10个字符。
}
}

if($ errors){
//输出错误并死机,失败消息
$ errortext =;
foreach($ errors as $ error){
$ errortext。=< li>。$ error。< / li>;
}
die(< span class ='failure'>< h3>对不起,出现以下错误:< / h3>< ol>$ errortext。 / ol>< a href ='contact.html'class ='more'>刷新表单< / a>< / span>);
}


// ------------------------------- ------- //
//发送电子邮件//插入您的电子邮件
$ to =myemail@my_domain.com;
// -------------------------------------- //


$ subject =联系表单:$ name;
$ message =$ message;
$ headers ='From:myemail@my_domain.com'。 \\\\。
'回复:$ email'。 \\\\。
'X-Mailer:PHP /'。 phpversion();


mail($ to,$ subject,$ message,$ headers);

//成功发送消息
die(< span class ='success'>< h3>成功发送< / h3>您的消息正在路上,我们将尽快回复您。< / span>);

//检查
//电子邮件是否有效的函数
函数validEmail($ email)
{
$ isValid = true ;
$ atIndex = strrpos($ email,@);
if(is_bool($ atIndex)&!$ atIndex)
{
$ isValid = false;
}
else
{
$ domain = substr($ email,$ atIndex + 1);
$ local = substr($ email,0,$ atIndex);
$ localLen = strlen($ local);
$ domainLen = strlen($ domain);
if($ localLen< 1 || $ localLen> 64)
{
//本地零件长度超过
$ isValid = false;
}
else if($ domainLen< 1 || $ domainLen> 255)
{
//域名长度超过
$ isValid = false;
}
else if($ local [0] =='。'|| $ local [$ localLen-1] =='。')
{
// local部分以'。'开头或结尾
$ isValid = false;
}
else if(preg_match('/ \\\\\。/',$ local))
{
//本地零件有两个连续的点
$ isValid = false;
}
else if(!preg_match('/ ^ [A-Za-z0-9\\ -\\\。] + $ /',$ domain))
{
//在域名中没有字符
$ isValid = false;
}
else if(preg_match('/ \\\\。/',$ domain))
{
//域部分有两个连续的点
$ isValid = false;
}
else if(!preg_match('/ ^(\\\\。| [A-Za-z0-9!#%&`_ = \\ / $ \'* +?^ {} |〜.-])+ $ /',
str_replace(\\\\\,,$ local)))
{
//在本地部分无效,除非
//本地部分引用
if(!preg_match('/ ^(\\\\ ])+$ /',
str_replace(\\\\\,,$ local)))
{
$ isValid = false;
}
}
if($ isValid&&!(checkdnsrr($ domain,MX)|| checkdnsrr($ domain,A)))
{
//在DNS中找不到域名
$ isValid = false;
}
}
return $ isValid;
}

?>


解决方案

尝试更改这部分代码:

  $ subject =Contact form:$ name; 
$ message =$ message;
$ headers ='From:myemail@my_domain.com'。 \\\\。
'回复:$ email'。 \\\\。
'X-Mailer:PHP /'。 phpversion();

为此:

  $ subject =Contact form:$ name; 
$ message =$ message;
$ headers ='From:myemail@my_domain.com'。 \\\\。
'Reply-To:'。 $电子邮件。 \\\\。
'X-Mailer:PHP /'。 phpversion();

基本上从单引号内取出 $ email ,并追加到该字符串


I bought a simple website template with a php contact form. Everything works great with the one small exception of actually receiving the messages sent via the form. That is, the contact form would show a success message, but the message would never arrive.

After a long back and forth with my hosting service, I found out that in order to avoid spoofing they won't allow emails to be sent where the FROM address they don't host. That is, if a visitor to the site writes down his gmail/yahoo etc. in the form, I won't get it.

They suggested using the email address hosted with them as the FROM address, and having the visitor's input email as the REPLY-TO address. This seems reasonable.

So I dug around (e.g. here: PHP reply-to error - comes with admin email not sender of contact form and php Contact Form on website and reply-to email )

and the answers suggest something adding a headers component:

$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

and also add it to

mail($to, $subject, $message, $headers);

so that's what I did. $email is defined in this template as what the visitor's email, so what I did was:

$subject = "Contact Form: $name";
$message = "$message";
$headers = 'From: myemail@my_domain.com' . "\r\n" .
    'Reply-To: $email' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

This is all nice and dandy but it still doesn't work well. Emails do go through now, but the details are:

from:    myemail@my_domain.com via servername.hosting_company.com 
**reply-to:  $email@servername.hosting_company.com**
to:  myemail@my_domain.com

so, the reply to address is still not what the visitor left.

Can you help me with this? Don't know what else I can do.

Many thanks!


if anyone is interested, here's the full php file:

<?php

// Clean up the input values
foreach($_POST as $key => $value) {
    if(ini_get('magic_quotes_gpc'))
        $_POST[$key] = stripslashes($_POST[$key]);

    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

// Test input values for errors
$errors = array();
if(strlen($name) < 2) {
    if(!$name) {
        $errors[] = "You must enter a name.";
    } else {
        $errors[] = "Name must be at least 2 characters.";
    }
}
if(!$email) {
    $errors[] = "You must enter an email.";
} else if(!validEmail($email)) {
    $errors[] = "You must enter a valid email.";
}
if(strlen($message) < 10) {
    if(!$message) {
        $errors[] = "You must enter a message.";
    } else {
        $errors[] = "Message must be at least 10 characters.";
    }
}

if($errors) {
    // Output errors and die with a failure message
    $errortext = "";
    foreach($errors as $error) {
        $errortext .= "<li>".$error."</li>";
    }
    die("<span class='failure'><h3>Sorry, The following errors occured:</h3><ol>". $errortext ."</ol><a href='contact.html' class='more'>Refresh Form</a></span>");
}


// --------------------------------------//
// Send the email // INSERT YOUR EMAIL HERE
$to = "myemail@my_domain.com";
// --------------------------------------//


$subject = "Contact Form: $name";
$message = "$message";
$headers = 'From: myemail@my_domain.com' . "\r\n" .
    'Reply-To: $email' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


mail($to, $subject, $message, $headers);

// Die with a success message
die("<span class='success'><h3>Successfully Sent!</h3> Your message is on its way, we will respond to you shortly.</span>");

// A function that checks to see if
// an email is valid
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

?>

解决方案

Try changing this part of your code :

$subject = "Contact Form: $name";
$message = "$message";
$headers = 'From: myemail@my_domain.com' . "\r\n" .
    'Reply-To: $email' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

To this :

$subject = "Contact Form: $name";
$message = "$message";
$headers = 'From: myemail@my_domain.com' . "\r\n" .
    'Reply-To: ' . $email . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

Basically take out the $email from inside the single quote and append it to that string

这篇关于以php联系表格回复地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆