如何创建联系表单并邮寄给网站所有者

时间:2017-10-31 06:52:26

标签: php email sendmail contact-form

我希望通过联系表单获取客户详细信息,并将其发送到我的邮件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";    
?>

4 个答案:

答案 0 :(得分:0)

此处所述的邮件功能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);
?>

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

答案 1 :(得分:0)

这大多是正确的。

您无法通过移动设备作为附加参数,它将不执行任何操作。 我已经更改了脚本以将移动设备附加到邮件正文中。

要设置发件人地址,您需要有一封来自电子邮件,只需一个名称本身就无法使用。

另外,根据您的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;
}

答案 2 :(得分:0)

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.';

    }
  }
    ?>

答案 3 :(得分:0)

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

相关问题