Php
Postfix 不會從表單發送 PHP mail() 函式
我得到了後綴,可以使用 控制台從我的伺服器向我發送電子郵件
echo "body example" | mail -s "subject example" my@email
,但是當我嘗試通過mail()
php 函式從我的網站發送電子郵件時,我沒有收到電子郵件。我是 Web 伺服器和郵件伺服器的新手,據我了解,我需要執行 postfix mts 才能使用該mail()
功能。我不知道為什麼我的表單送出沒有通過,並且
/var/log/mail.log
文件中沒有任何內容可以幫助我找出問題所在。我檢查了我的聯繫表格和 php 本身是否有任何錯誤(我從引導模板中得到,但後來被篡改了),在那裡找不到任何問題。我可以使用一些幫助來查找可以採取的後續步驟,以找出電子郵件未發送的原因,以及其他地方是否有任何日誌表明我可能存在錯誤。
為了更好地衡量,我已經包含了我的 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: noreply@nickborisenko.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com. $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>
和我的後綴配置文件
/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"
這些是我在嘗試發送聯繫表時發現的唯一變化的日誌,它們似乎參與其中,儘管我沒有看到任何錯誤……
問題在於我處理 php.ini 的方式。我所要做的就是將上面的函式分成一個驗證函式來驗證輸入,另一個在驗證返回 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: noreply@nickborisenko.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com. $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>