2016-12-30 57 views
0

我試圖運行一個電子郵件表單,它從用戶的輸入表單中獲取文本並將它們發送到我想要的電子郵件(在下面的php中被看作$ myemail)。爲什麼此電子郵件表格代碼無效?

<form method="post" name="contact_form" action="Contact.php"> 
     Your Name: <input type="text" required name="name"> 
     Email Address: <input type="text" required name="email"> 
     Message: <textarea required name="message"></textarea> 
     <input type="submit" value="Submit"> 
</form> 

<?php 
$errors = ''; 
$myemail = 'enterDesiredEmail'; 

if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) { 
     $errors .= "\n Error: all fields are required"; 
} 

$name = null; 
$email_address = null; 
$message = null; 

$name = $_POST["name"]; 
$email_address = $_POST["email"]; 
$message = $_POST["message"]; 

if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",$email_address)) { 
     $errors .= "\n Error: Invalid email address"; 
} 

if(empty($errors)) { 
    $to = $myemail; 
    $email_subject = "Contact form submission: $name"; 
    $email_body = "You have received a new message. ". 
    " Here are the details:\n Name: $name \n ". 
    "Email: $email_address\n Message \n $message"; 
    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email_address"; 
    mail($to,$email_subject,$email_body,$headers); 
} 
?> 

我把上面的代碼吐出到網頁上,然後把文件上傳到我的服務器。在網上訪問網頁,填寫表單內容並點擊sbmit,頁面刷新後輸入文本和textarea會按預期清除。當我查看了我設置的電子郵件時,沒有收到任何消息。我能做些什麼來糾正這個代碼,以便所需的電子郵件地址實際上收到一條消息?

非常感謝。

+2

郵件命令需要設置sendmail或其他郵件服務器。 –

+0

嘗試並向您的標題添加類型,以便服務器知道如何處理腳本。按類型我的意思是如果它是一個HTML電子郵件或只是文本。其次,爲郵件功能實例化一個變量。如果郵件實際發送則返回true,否則返回false。例如$ send = mail(....):if($ sent)做東西....希望這會有所幫助 –

+1

您需要在標題字段之間加上'\ r \ n',而不是'\ n'。 – Barmar

回答

1

一些指針可能會很好地解決您的問題沒有明確告訴你是什麼原因這個問題實際上是:

1)Use Error Logging

2)檢查您的服務器上是否有有效的郵件服務器設置。如Jason K.的評論中所述

3)使用郵寄圖書館,如PHPMailer。它迴避了大量的頭痛。

4)檢查您的電子郵件地址是否有效the correct code,主要是使用filter_var而不是使用鈍的正則表達式。

也檢查電子郵件是消毒FILTER_SANITIZE_EMAIL)以及。

5)如果不使用或者建立一個庫,如PHPMailer的或SwiftMailer那麼你需要檢查您mail headers are exactly correct

BONUS
似乎有什麼用電子郵件行尾[包括標題]一些conflictingaccounts,但我會建議PHP_EOL\r\n(在Linux服務器上同樣的事情)。 \n不適合。正如Barmar的評論所述。

祝你好運

1

您需要從內插雙引號內的實際$電子郵件變量:如果您指定其他標題,如收件人,抄送和密件抄送
$headers = "From: $myemail\n";$headers = "From: ${myemail}"."\r\n";

額外的標題應該用CRLF(\ r \ n)分隔。

這是從PHP文檔的例子:

`<?php 
    $to  = '[email protected]'; 
    $subject = 'the subject'; 
    $message = 'hello'; 
    $headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
    mail($to, $subject, $message, $headers); 
?> 

編碼愉快!

+1

''From:$ myemail \ n「'和'」From:$ {myemail} \ n「'沒有區別。你只需要花括號來處理複雜的變量。 – Barmar

1

您應該嘗試使用PHPMailer等庫。在大多數情況下,mail()函數有時不夠靈活以達到您所需要的。 mail()功能還需要本地郵件服務器。另外,PHPMailer提供了很多插件,例如設置附件的能力或向用戶發送HTML電子郵件的能力。使用這樣的圖書館也意味着你的電子郵件將會在很多人測試和使用之後幾乎所有的時間被髮送出去。

你可以找到這裏使用的PHPMailer教程:使用PHPMailer的從網站採取https://www.sitepoint.com/sending-emails-php-phpmailer/

示例代碼:

<?php 

require_once "vendor/autoload.php"; 

//PHPMailer Object 
$mail = new PHPMailer; 

//From email address and name 
$mail->From = "[email protected]"; 
$mail->FromName = "Full Name"; 

//To address and name 
$mail->addAddress("[email protected]", "Recepient Name"); 
$mail->addAddress("[email protected]"); //Recipient name is optional 

//Address to which recipient will reply 
$mail->addReplyTo("[email protected]", "Reply"); 

//CC and BCC 
$mail->addCC("[email protected]"); 
$mail->addBCC("[email protected]"); 

//Send HTML or Plain Text email 
$mail->isHTML(true); 

$mail->Subject = "Subject Text"; 
$mail->Body = "<i>Mail body in HTML</i>"; 
$mail->AltBody = "This is the plain text version of the email content"; 

if(!$mail->send()) 
{ 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} 
else 
{ 
    echo "Message has been sent successfully"; 
} 

?> 
相關問題