通过php mail()函数发送邮件 [英] Send mail by php mail() function

查看:151
本文介绍了通过php mail()函数发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过php发送邮件的邮件(邮件()功能的问题。我不知道是否是代码coz的问题我已经看到,一些托管服务器不允许发送邮件,但我也想发送这个邮件,当网站在本地主机,它仍然不工作 - 点击发送我看到的信息:你的邮件被发送,但是当我检查我的邮箱时,没有邮件(也是垃圾邮件)。

I have problem with sending mail message by php mail() function. I'm not sure if it's problem with code coz I have read that some hosting servers are not allowing to sends mail but I'm trying to send this mail also when website is on localhost and it still doesn't work - after click "Send" I see the information: "Your mail is sent", but when I'm checking on my postbox there is no mails (also in spam).

对我来说,代码看起来不错,但也许我错过了一些东西。我正在考虑的第二个选项是我的本地主机也不允许发送邮件。

For me code looks good but maybe I'm missing something. The second option which I'm considering is that also my localhost is not allowing to send mails.

<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 conf:

Pictures with my php conf:



推荐答案

要测试发送电子邮件的工作原理,请尝试这个简单的程序:

To test that sending of email works, try this really short program:

<?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!"
?>

过去我已经使用了就像它,作为测试配置的起点。如果这不起作用,很可能是您的服务器配置。

I have used one "just like it" in the past as the starting point for testing a configuration. If this doesn't work it's most likely your server configuration.

这篇关于通过php mail()函数发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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