Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过php mail()函数发送邮件_Php_Html_Email - Fatal编程技术网

通过php mail()函数发送邮件

通过php mail()函数发送邮件,php,html,email,Php,Html,Email,我无法通过php mail()函数发送邮件消息。我不确定这是否是代码的问题,因为我读到一些主机服务器不允许发送邮件,但我也尝试在网站位于localhost时发送此邮件,但仍然无法工作-单击“发送”后,我会看到信息:“您的邮件已发送”,但当我检查邮箱时,没有邮件(也是垃圾邮件) 对我来说,代码看起来不错,但也许我遗漏了什么。我正在考虑的第二个选择是,我的本地主机也不允许发送邮件 <form id="contact" action="mail.php" method="POST">

我无法通过php mail()函数发送邮件消息。我不确定这是否是代码的问题,因为我读到一些主机服务器不允许发送邮件,但我也尝试在网站位于localhost时发送此邮件,但仍然无法工作-单击“发送”后,我会看到信息:“您的邮件已发送”,但当我检查邮箱时,没有邮件(也是垃圾邮件)

对我来说,代码看起来不错,但也许我遗漏了什么。我正在考虑的第二个选择是,我的本地主机也不允许发送邮件

<form id="contact" action="mail.php" method="POST">
    <div class="field">
        <label class="fixed_width" for="name">Name:</label><input id="name" name="name" value="Name"/>
    </div>
    <div class="field">
        <label class="fixed_width" for="surname">Surname:</label><input id="surname" name="surname" value="Surname"/>
    </div>
    <div class="field">
        <label class="fixed_width" for="mail">E-mail:</label><input id="mail" name="mail" value="E-mail"/>
    </div>
    <div class="field" id="message">
        <label class="fixed_width" id="message_width" for="mail">Message:</label>
        <textarea id="message" name="message" />Type your message...</textarea>
    </div>
    <div>
        <input class="width" type="submit" value="Send" />
    </div>
</form>

<?php

    srand((double)microtime()*1000000);
    $marker = md5(uniqid(rand()));

    $receiver  = "address@gmail.com";
    $title = "Mail";
    $sender  = $_POST['name'];
    $sender .= $_POST['surname'];
    $sender_mail = $_POST['mail'];

    $message = $_POST['message'];

    $headers  = "From: $sender <$sender_mail>\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed;\n";
    $headers .= "\tboundary=\"___$marker==\"";

    $content ="--___$marker==\n";
    $content .="Content-Type: text/plain; charset=\"iso-8859-2\"\n";
    $content .="Content-Transfer-Encoding: 8bit\n";
    $content .="\n$message\n";

    if (mail($receiver,$title,$content,$headers))
    {
        print "Your message is sent.";
    } else {
        print "Your message is not sent.
        <br>Please go <a href=\"javascript:history.back();\">back</a> and send again.";
    }
?>

姓名:
姓:
电邮:
信息:
键入您的消息。。。
我的php配置的图片:


要测试发送电子邮件是否有效,请尝试以下非常简短的程序:

<?php
$email_to="address@gmail.com";
$email_subject="It works";
$email_message="Hello. I can send mail!";
$headers = "From: Beacze\r\n".
"Reply-To: address@gmail.com\r\n'" .
"X-Mailer: PHP/" . phpversion();
mail($email_to, $email_subject, $email_message, $headers);  
echo "mail sent!"
?>
您可以使用SMTP(phpmailer)
例如:


您可以在此处找到有关PHPMailer的更多信息:

是否已启用错误报告功能?您是否尝试过gmail以外的其他电子邮件?是否有使用边界的原因(不必要)?您似乎没有“-f”参数,这有助于交付……如果我是您,我会将输入的“值”属性更改为“占位符”。这将使您无需依赖JS来检查这些字段是否将其值设置为默认值,如果是,则在接收到焦点时清除这些字段,并在模糊时将其设置为空。鉴于您已经为这些字段指定了标签,设置值是多余的。此外,我还将strip\u tags()或filter\u var()调用放在$\u POST变量周围。(永远不要相信没有某种形式的卫生设施的用户输入。)@cale_b OP已经指定这是在localhost上。我从未幸运地从localhost安装发送过电子邮件……这是localhost,我还尝试过gmail以外的其他电子邮件。同样的情况。我收到了“已发送邮件”的信息,但我的邮箱上没有邮件。您是否尝试刷新收件箱?有时可能需要一段时间。另外,您发送的服务器(例如,如果使用谷歌的邮件服务器)是否需要身份验证?是的,我已经刷新了几次。不,我没有任何身份验证。嗯。。。我只想写简单的联系表单,所以我不知道从哪里可以获取用户名和密码。如果邮件服务器需要身份验证,用户名和密码用于发送电子邮件的smtp邮件服务器。
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';

$mail->Host       = "mail.example.com"; // SMTP server example
$mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 25;                    // set the SMTP port for the GMAIL server
$mail->Username   = "username"; // SMTP account username example
$mail->Password   = "password";        // SMTP account password example