【问题标题】:How to create contact form and mail to the owner of website [duplicate]如何创建联系表格并邮寄给网站所有者[重复]
【发布时间】:2018-04-12 06:30:03
【问题描述】:

我想通过联系表格获取客户详细信息,并将其发送到我的邮件 ID。所以我正在尝试编码,但它不起作用。我通过观看 YouTube 视频尝试过

这是我的 HTML 代码

<!DOCTYPE html>
<html>
<head>
    <title>Form</title>
</head>
<body>
<form action="send.php" method="POST">
Name:   <input type="text" name="name" required=""></input><br><br>
Mobile: <input type="text" required="" name="mobile"></input><br><br>
Message: <textarea rows="10" cols="50" name="mes" required=""></textarea><br><br>
<input type="submit" value="Send Mail"></input>

</form>
</body>
</html>

这是我的 PHP 代码

<?php
$name =$_POST['name'];
$mobile =$_POST['mobile'];
$mes =$_POST['mes'];
mail("imjeevajk@gmail.com","Contact From Site",$mes,"From: $name\r\n","Mobile: $mobile\r\n");
echo "Thanks for Contacting Us";    
?>

【问题讨论】:

标签: php email sendmail contact-form


【解决方案1】:

这里描述的邮件功能http://php.net/manual/en/function.mail.php

例子:

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

因此,当您输入自定义名称时,标题“发件人”必须是电子邮件地址。 如何获取邮件功能投递邮件到'to'地址请阅读here

【讨论】:

    【解决方案2】:

    大部分是正确的。

    您不能将 Mobile 作为附加参数传递,这将无济于事。 我已更改脚本以将手机附加到消息正文中。

    要设置发件人地址,您需要有一个发件人电子邮件,仅名称本身是行不通的。

    另外,根据您的 php 配置和邮件服务器配置,您可能无法更改发件人地址,这将导致错误或被忽略。

    <?php
    if ($_POST) {
        $name = $_POST['name'];
        $mobile = $_POST['mobile'];
        $mes = $_POST['mes'];
    
        // append mobile to message
        $mes .= "\r\nMobile: $mobile\r\n";
    
        $from_email = "imjeevajk@gmail.com';
    
        mail("imjeevajk@gmail.com", "Contact From Site", $mes, "From: $name <$from_email>\r\n");
    
        // mail("imjeevajk@gmail.com", "Contact From Site", $mes, "From: $name\r\n", "Mobile: $mobile\r\n");
        echo "Thanks for Contacting Us";
        exit;
    }
    

    【讨论】:

      【解决方案3】:
      Please set submit name field and update the php mail function as mentioned below.
      <input type="submit" name="sendMail" value="Send Mail"></input>
      
      
          <?php
          if (isset($_POST['sendMail']))  {
          $to = 'admin@dummyemail.com';
      
          $subject = 'Contact From Site';
          $from = "From: ".$_POST["name"]."\r\n"; 
          $headers  = 'MIME-Version: 1.0' . "\r\n";
          $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
          $headers .= 'From: '.$from."\r\n".
      
              'Reply-To: '.$from."\r\n" .
      
              'X-Mailer: PHP/' . phpversion();
          $message = '<html><body>';
          $message = "Name: ".$_POST["name"].""; 
          $message .= "Mobile: ".$_POST["mobile"]."";   
          $message .= "Message: ".nl2br($_POST["mes"]).""; 
          $message .= '</body></html>';
          if(mail($to, $subject, $message, $headers)){
      
              echo 'Thanks for Contacting Us.';
      
          } else{
      
              echo 'Unable to send email. Please try again.';
      
          }
        }
          ?>
      

      【讨论】:

        【解决方案4】:

        更好地使用PHP Mailer 一些服务器提供商阻止了邮件功能,这可能是它不起作用的原因。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-01-31
          • 2014-01-02
          • 1970-01-01
          • 1970-01-01
          • 2020-04-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多