通过邮件功能发送的电子邮件未发送

时间:2018-07-10 02:15:57

标签: php email

我正在尝试制作一个PHP脚本,该脚本从html联系人表单发送邮件,该脚本不会引发任何错误,但是不会发送邮件。 代码如下。

mail.php

<?php
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];
    $email_from = 'stronka@obabie.com'; //<== update the email address
    $email_subject = "New Form submission";
    $email_body = "You have received a new message from the user $name.\n" .
    "Here is the message:\n $message" .
    $to = "bruno.kedzierski@wp.pl";
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    mail($to, $email_subject, $email_body, $headers);
    header('Location: index.html');
?>

我的HTML文件

<div class="container" style="width: 200px; float: left; margin-left: 300px; margin-top: 25px">
    <form action="mail.php" method="post">
        <div class="form-group" style="margin-left: auto;margin-right: auto;">
            <label>Imie i nazwisko</label>
            <input type="text" class="form-control" placeholder="Imie i nazwisko " name="name">
            </label>
        </div>

        <div class="form-group" style="margin-left: auto;margin-right: auto;">
            <label>E-mail</label>
            <input type="email" class="form-control" placeholder="E-mail" name="email" required>
            </label>
        </div>

        <div class="form-group" style="margin-left: auto;margin-right: auto;">
            <label>Numer telefonu</label>
            <input class="form-control" placeholder="Twoj numer" type="tel">
            </label>
        </div>

        <div class="form-group" style="margin-left: auto;margin-right: auto; width: 400px;">
            <label>Wiadomosc</label>
            <textarea class="form-control" placeholder="Twoja wiadomosc" style="height: 100px" name="message"> </textarea>
            </label>
        </div>

        <div class="form-group" style="margin-left: auto;margin-right: auto;">
            <label>Plec
                <select style="form-control">
                    <option value="chlop">Chlop</option>
                    <option value="chlop">Baba</option>
                    <option value="inny">Inna</option>
                </select>
            </label>

        </div>
        <div class="radio">
            <label style="display: block;">
                <input type="radio" name="optradio"> kradne</label>
            <label style="display: block;">
                <input type="radio" name="optradio"> nie kradne</label>
        </div>
        <button type="submit" class="btn btn-default" value="submit">Wyslij</button>

    </form>

因此,我添加了属性action="mail.php"method="post",以便在我按Submit时启动PHP。谁能告诉我为什么它不起作用?

2 个答案:

答案 0 :(得分:0)

首先将此属性添加到表单标签中 enctype =“ multipart / form-data”

然后在php代码中尝试发送不带标头的邮件

mail($ to,$ email_subject,$ email_body);

如果仍然出现错误,请尝试在发送邮件功能后打印此类错误

print_r(error_get_last())

答案 1 :(得分:-1)

发送邮件不是火箭科学。 Just Works的一个简单示例-

<?php

    $to_address = "someone@example.com";
    $from_name = "From Name";
    $from_address =  "no.reply@example.com";
    $reply_to_name="Reply to name";
    $reply_to_address="reply-to@example.com";
    $subject = "Hello!";
    $headers = "Content-type: text/plain; charset=iso-8859-1\r\n";
    $headers .= "From: $from_name <$from_address>\r\n";
    $headers .= "Date: " . date("Ymd H:i:s") . "\r\n";
    $headers .= "Reply-To: $reply_to_name <$reply_to_address>r\n";
    $headers .= "X-Priority: 1\r\n";
    $headers .= "X-MSMail-Priority: High\r\n";
    $headers .= "X-Mailer: Some PHP Script\r\n";
    $message_body = "Message goes here. Be polite and wrap it every 70 lines
    or so, otherwise some mail clients will display very long annoying
    lines of text.  There are functions that can do this automagically
    for you ... or write you own.";
    mail($to_address, $subject, $message_body, $headers);

?>

但是 ...过去的美好时光已经过去了,任何人都可以从任何在interwebz上运行脚本的计算机上的任何地方发送邮件。在不想被视为垃圾邮件的来源与不想接收垃圾邮件之间,处理SPF记录和DKIM……之间的工作量很大。

要真正发送邮件并知道您的代码对电子邮件系统不起作用,您需要正确配置PHP并正确配置任何与PHP合作的邮件服务器。

我看到经常出现邮件问题,没有答案与服务器配置无关。我想我可能会建立一个可以发送邮件的虚拟机,将其发布到某个地方,并在需要使用mail() ...

时发布有关为开发工作设置内容的自我解答问题。