无法发送带有php邮件功能的邮件

时间:2014-11-28 12:33:16

标签: php function email

<?php
if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $contact = $_POST['num'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $ToEmail = 'info@kesems.com';
    $EmailSubject = 'School Enquiry';
    $mailheader = "From: ".$_POST["email"]."\r\n";
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
    $MESSAGE_BODY = "Phone: ".$_POST["num"]."\r\n";
    $MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
    $MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
    header('Location:contact-us.php');
}
?>

与-us.php

<form role="form" action="contact-us.php" id="main-contact-form" class="contact-form" name="contact-form" method="post">
    <div class="row ml0">
        <div class="form-group">
            <input type="text" class="form-control" name="name" required="required" placeholder="Name">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" name="num" required="required" placeholder="Contact number">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" name="email" required="required" placeholder="Email address">
        </div>
        <div class="form-group">
            <textarea name="message" id="message" required="required" name="message" class="form-control" rows="3" placeholder="Any Queries/suggestions" style="resize:none"></textarea>
        </div>
        <div class="form-group">
            <input type="submit" name="submit" value="Send Message" class="btn btn-primary btn-lg"/>
        </div>
    </div>
</form>

发送电子邮件标头的简单脚本......仍无效。

代码是否正确?

对此有什么特别的解决方案吗? 有什么特别的解决方案吗? 先感谢您。

2 个答案:

答案 0 :(得分:0)

试试这个对我有用

//要发送HTML邮件,必须设置Content-type标头

                $headers = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                $headers .= "From: yourmail@gmail.com" . "\r\n" .
                        "Reply-To: no-reply@gmail.com" . "\r\n" .
                        "X-Mailer: PHP/" . phpversion();
                $to = "tomail@gmail.com";
                $subject = "This is subject";
                $from = "yourmail@gmail.com";//optional
                $message = ' this is my message hello';
                if (mail($to, $subject, $message, $headers, 'ADMIN')) {
                 echo "mail sent"
                 }
              else{
               echo "error cannot send mail";
              }

答案 1 :(得分:0)

检查是否所有参数都已实际设置(使用ternary),因为设置了提交并不意味着所有参数都已设置:

$name = isset($_POST['name']) ? $_POST['name'] : "";
$contact = isset($_POST['num']) ? $_POST['num'] : "";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$message = isset($_POST['message']) ? $_POST['message'] : "";

检查这些值并指出如果未设置某些内容则显示错误的条件。

如果您确定已设置值,则需要更改

$ToEmail = 'info@example.com';

$ToEmail = $email;

否则什么都不会发生。

最后,我建议使用正确的ID识别您的html输入,而不是名称。

与错误无关但对您很重要

正如@Fred -ii-已经指出的那样,在$ MESSAGE_BODY的第二个标题分配中需要一个正确的连接(。=)而不是覆盖(=):

MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY = "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";

应该是:

$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";