Postfix 不会从表单发送 PHP mail() 函数

Postfix 不会从表单发送 PHP mail() 函数

我让 postfix 使用 通过控制台从我的服务器向我发送电子邮件echo "body example" | mail -s "subject example" my@email,但当我尝试通过mail()php 函数从我的网站发送电子邮件时,我没有收到电子邮件。我对网络服务器和邮件服务器还不熟悉,据我所知,我需要运行 postfix mts 才能使用该mail()功能。

我不知道为什么我的表单提交没有通过,文件中也没有显示任何内容/var/log/mail.log来帮助我找出问题所在。我检查了我的联系表单和 php 本身是否有任何错误(我从 bootstrap 模板中获取了这些错误,但后来对其进行了篡改),但没有发现任何问题。

我可以使用一些帮助来找到下一步可以采取的步骤来找出电子邮件无法发送的原因,以及其他地方是否有任何日志可以表明潜在的错误。

为了更好的衡量,我已经包含了我的 php 函数和表单:

<?php
    function send(){
        if(empty($_POST['name'])        ||
           empty($_POST['email'])       ||
           empty($_POST['phone'])       ||
           empty($_POST['message']) ||
           empty($_POST['company'])   ||
           !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
           {
            echo "No arguments Provided!";
            return false;
           }

        $name = $_POST['name'];
        $email_address = $_POST['email'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];
        $company = $_POST['company'];

        // Create the email and send the message
        $to = 'my@email';
        $email_subject = "Website Contact Form:  $name";
        $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nCompany: $company\n\nPhone: $phone\n\nMessage:\n$message";
        $headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
        $headers .= "Reply-To: $email_address"; 
        mail($to,$email_subject,$email_body,$headers);
        return true;
    }
    if(isset($_POST['send'])){
        send();
    }        
?>


<!-- Some html -->


<form name="sentMessage" method="post" id="contactForm" novalidate>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Name</label>
                            <input type="text" class="form-control" placeholder="Name" name="name" id="name" required data-validation-required-message="Please enter your name.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Email Address</label>
                            <input type="email" class="form-control" placeholder="Email Address" name="email" id="email" required data-validation-required-message="Please enter your email address.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Phone Number</label>
                            <input type="tel" class="form-control" placeholder="Phone Number" name="phone" id="phone" required data-validation-required-message="Please enter your phone number.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Company</label>
                            <input type="text" class="form-control" placeholder="Company" name="company" id="company">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Message</label>
                            <textarea rows="5" class="form-control" placeholder="Message" name="message" id="message" required data-validation-required-message="Please enter a message."></textarea>
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <br>
                    <div id="success"></div>
                    <div class="row">
                        <div class="form-group col-xs-12">
                            <button type="submit" name="send" class="btn btn-default">Send</button>
                        </div>
                    </div>
                </form>

我的 postfix 配置文件如下/etc/postfix/main.cf

smtpd_banner = ESMTP $mail_name (Ubuntu)
biff = no

append_dot_mydomain = no

readme_directory = no

smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = nickborisenko.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = nickborisenko.com, ubuntu-512mb-nyc3-01, localhost.localdomain, localhost
relayhost = 
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
mydomain = nickborisenko.com
smtp_generic_maps = hash:/etc/postfix/generic

编辑:这些日志来自我的 apache2/access.log:

136.167.247.240 - - [28/Mar/2016:17:35:15 -0400] "GET /contact.php HTTP/1.1" 200 2592 "http://nickborisenko.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
::1 - - [28/Mar/2016:17:35:23 -0400] "OPTIONS * HTTP/1.0" 200 125 "-" "Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.14 (internal dummy connection)"
136.167.247.240 - - [28/Mar/2016:17:35:28 -0400] "POST /mail/contact_me.php HTTP/1.1" 200 247 "http://nickborisenko.com/contact.php" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"

这些是我尝试发送联系表单时发现的唯一发生变化的日志,它们似乎参与其中,尽管我没有看到任何错误......

答案1

问题出在我处理 php 的方式上。我只需要修复一下,将上面的函数分成一个验证函数来验证输入,另一个函数在验证返回 true 时实际发送表单。这个修复并没有完全满足我的验证要求,但电子邮件已经发送,这是我唯一担心的事情。

<?php
    function validate(){
        if(empty($_POST['name'])        ||
           empty($_POST['email'])       ||
           empty($_POST['phone'])       ||
           empty($_POST['message']) ||
           empty($_POST['company'])   ||
           !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
           {
            echo "No arguments Provided!";
            return false;
           }else{
            return true;
           }
    }

    function send(){
        $name = $_POST['name'];
        $email_address = $_POST['email'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];
        $company = $_POST['company'];

        // Create the email and send the message
        $to = 'my@email';
        $email_subject = "Website Contact Form:  $name";
        $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nCompany: $company\n\nPhone: $phone\n\nMessage:\n$message";
        $headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
        $headers .= "Reply-To: $email_address"; 
        mail($to,$email_subject,$email_body,$headers);
        return true;
    }
    if(isset($_POST['send'])){
        send();
    }        
?>

<form name="sentMessage" onsubmit="return validate()" method="post" id="contactForm" novalidate>

相关内容