問題描述

當用户填寫了一個 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.phpcontact.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 服務器可能會有所不同。

例如:

  1. 將 smtp_port 設置設置為 smtp 服務器的端口。

  2. 將 smtp_crypto 設置設置為 smtp 服務器所需的設置。

  3. 設置 $ 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇