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

时间:2014-02-22 23:03:00

标签: php html email

我有通过php mail()功能发送邮件的问题。我不确定它是否与代码有问题因为我已经读过一些托管服务器不允许发送邮件但是当网站在localhost上时我也试图发送这封邮件但它仍然不起作用 - 点击后“发送“我看到的信息:”您的邮件已发送“,但当我检查我的邮箱时,没有邮件(也是垃圾邮件)。

对我而言,代码看起来不错,但也许我错过了一些东西。我正在考虑的第二个选项是我的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 conf:

PHP config PHP config PHP config

2 个答案:

答案 0 :(得分:7)

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

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

我过去曾使用过一个“就像它一样”作为测试配置的起点。如果这不起作用,则很可能是您的服务器配置。

答案 1 :(得分:2)

您可以使用SMTP(phpmailer)
例如:

$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

您可以在此处找到有关PHPMailer的更多信息:https://code.google.com/a/apache-extras.org/p/phpmailer/