問題描述
當使用者填寫了一個 HTML 表單,然後透過電子郵件傳送表單中的資訊時,我想傳送一封郵件。我想從顯示具有該表單的網頁的相同指令碼中執行此操作。
我發現這個程式碼,但郵件不傳送。
<?php
if (isset($_POST['submit'])) {
$to = $_POST['email'];
$subject = $_POST['name'];
$message = getRequestURI();
$from = "zenphoto@example.com";
$headers = "From:" . $from;
if (mail($to, $subject, $message, $headers)) {
echo "Mail Sent.";
}
else {
echo "failed";
}
}
?>
PHP 中傳送電子郵件的程式碼是什麼?
最佳解決方法
編輯 (#1)
如果我理解正確,您希望將所有內容都放在一個頁面中,並從同一頁面執行。
您可以使用以下程式碼從單個頁面傳送郵件,例如 index.php 或 contact.php
這個和我原來答案的唯一區別是<form action="" method="post">,其中動作已經留空。
最好在 PHP 處理程序中使用 header('Location: thank_you.php'); 代替 echo,然後再將使用者重定向到另一個頁面。
將下面的整個程式碼複製到一個檔案中。
<?php
if(isset($_POST['submit'])){
$to = "email@example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "nn" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "nn" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
原來的答案
我不太清楚這個問題是什麼,但是我的印象是,郵件的副本將被髮送給填寫表格的人。
這是 HTML 表單和 PHP 處理程序的測試/工作副本。這使用 PHP mail()功能。
PHP 處理程序還會將訊息的副本傳送給填寫表單的人員。
如果您不使用它,您可以在一行程式碼前面使用兩個正斜槓//。
例如:// $subject2 = "Copy of your form submission"; 不會執行。
HTML 表格:
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="mail_handler.php" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
PHP 處理程序 (mail_handler.php)
(使用 HTML 格式的資訊併傳送電子郵件)
<?php
if(isset($_POST['submit'])){
$to = "email@example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "nn" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "nn" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
}
?>
要作為 HTML 傳送:
如果您希望以 HTML 和兩個例項的形式傳送郵件,那麼您將需要建立兩個不同的變數名稱的 HTML 標頭檔案集。
閱讀 mail()上的手冊,瞭解如何以 HTML 形式傳送電子郵件:
腳註:
-
關於 HTML5
您必須使用 action 屬性指定將處理提交的資料的服務的 URL 。
如 https://www.w3.org/TR/html5/forms.html 4.10.1.3 所述。配置表單與伺服器進行通訊。有關完整資訊,請參閱該頁面。
因此,action=""將無法在 HTML5 中使用。
正確的語法是:
-
action="handler.xxx"或 -
action="http://www.example.com/handler.xxx"。
請注意,xxx 將是用於處理該程式的檔案型別的擴充套件。這可以是.php,.cgi,.pl,.jsp 副檔名等。
如果傳送郵件失敗,請諮詢堆疊上的以下 Q& A:
次佳解決方法
PHP 指令碼連線到 SMTP 伺服器並在 Windows 7 上傳送電子郵件
在 Windows 中傳送 PHP 的電子郵件是一個有雷人和頭痛的雷區。我會嘗試透過一個例項,讓我在 Windows 7 和 PHP 5.2.3(IIS)Internet Information Services Web 伺服器下工作。
我假設你不想使用任何包含電子郵件傳送功能的 CodeIgniter 或 Symfony 的 pre-built 框架。我們將從獨立的 PHP 檔案傳送電子郵件。我從 codeigniter hood(在系統/庫下) 獲取了這個程式碼,並修改了它,所以你可以放在這個 Email.php 檔案中,它應該只是工作。
這應該適用於較新版本的 PHP 。但你永遠不會知道
步驟 1,您需要使用 SMTP 伺服器的使用者名稱/密碼:
我正在使用 smtp.ihostexchange.net 的 smtp 伺服器,該伺服器已經為我建立並設定了。如果你沒有這個,你不能繼續。您應該可以使用 email client like thunderbird, evolution, Microsoft Outlook 來指定您的 smtp 伺服器,然後才能透過該郵件傳送電子郵件。
步驟 2,建立 Hello World 電子郵件檔案:
我假設你正在使用 IIS 。所以在 C:inetpubwwwroot 下建立一個名為 index.php 的檔案,並將這段程式碼放在那裡:
<?php
include("Email.php");
$c = new CI_Email();
$c->from("FromUserName@foobar.com");
$c->to("user_to_receive_email@gmail.com");
$c->subject("Celestial Temple");
$c->message("Dominion reinforcements on the way.");
$c->send();
echo "done";
?>
您應該能夠透過瀏覽瀏覽器到 localhost /index.php 來訪問此 index.php,因為 Email.php 丟失,所以會發出錯誤。但請確保您至少可以從瀏覽器執行它。
步驟 3,建立名為 Email.php 的檔案:
在 C:inetpubwwwroot 下建立一個名為 Email.php 的新檔案。
將此 PHP 程式碼複製/貼上到 Email.php 中:
https://github.com/sentientmachine/standalone_php_script_send_email/blob/master/Email.php
由於有多種 smtp 伺服器,您必須手動調整 Email.php 頂部的設定。我已經設定好它可以自動使用 smtp.ihostexchange.net,但您的 smtp 伺服器可能會有所不同。
例如:
-
將 smtp_port 設定設定為 smtp 伺服器的埠。
-
將 smtp_crypto 設定設定為 smtp 伺服器所需的設定。
-
設定 $ newline 和 $ crlf,因此它與 smtp 伺服器使用的相容。如果選擇錯誤,smtp 伺服器可能會忽略您的請求而沒有錯誤。我使用 r n,你可能需要
n。
連結程式碼太長,無法貼上為 stackoverflow 答案,如果要編輯它,請在這裡或透過 github 發表評論,我會更改它。
步驟 4,確保您的 php.ini 已啟用 ssl 副檔名:
找到您的 PHP.ini 檔案並取消註釋
;extension=php_openssl.dll
所以看起來像:
extension=php_openssl.dll
步驟 5,執行您剛剛在瀏覽器中建立的 index.php 檔案:
您應該得到以下輸出:
220 smtp.ihostexchange.net Microsoft ESMTP MAIL Service ready at
Wed, 16 Apr 2014 15:43:58 -0400 250 2.6.0
<534edd7c92761@summitbroadband.com> Queued mail for delivery
lang:email_sent
done
步驟 6,檢查你的電子郵件和垃圾郵件資料夾:
訪問 user_to_receive_email@gmail.com 的電子郵件帳戶,您應該已收到一封電子郵件。它應該在 5 或 10 秒內到達。如果沒有,請檢查頁面上返回的錯誤。如果這不行,請嘗試在谷歌上的鍵盤上揉搓你的臉,吟誦:「在雜貨店工作不是那麼糟糕。
第三種解決方法
如果還沒有,請檢視您的 php.ini,並確保 [mail function]設定下的引數設定正確以啟用電子郵件服務。您可以使用 PHPMailer 庫並按照說明進行操作。
第四種方法
您還可以使用 mandrill 應用程式在 php 中傳送郵件。您將從 https://mandrillapp.com/api/docs/index.php.html 獲取 API,您可以在其中找到有關傳送的電子郵件和其他詳細資訊的完整詳細資訊。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。