簡體   English   中英

使用相同的腳本提交時,從 html 表單使用 PHP 發送電子郵件

[英]Send email with PHP from html form on submit with the same script

我想在用戶填寫完 HTML 表單然后通過電子郵件從表單發送信息時使用 PHP 發送電子郵件。 我想從顯示具有表單的網頁的同一個腳本中執行此操作。

我找到了這個代碼,但是郵件沒有發送。

<?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"> ,其中操作被留空。

最好使用header('Location: thank_you.php'); 而不是echo在PHP處理程序后,用戶到另一個頁面重定向。

將下面的整個代碼復制到一個文件中。

<?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:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_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:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_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.html4.10.1.3 配置表單以與服務器通信 中所述 有關完整信息,請參閱頁面。

因此, action=""在 HTML5 中不起作用。

正確的語法是:

  • action="handler.xxx"
  • action="http://www.example.com/handler.xxx"

請注意, xxx將是用於處理該過程的文件類型的擴展名。 這可能是.php.cgi.pl.jsp文件擴展名等。


如果發送郵件失敗,請參閱 Stack 上的以下問答:

如果您還沒有,請查看您的php.ini並確保[mail function]設置下的參數設置正確以激活電子郵件服務。 之后您可以使用PHPMailer庫並按照說明進行操作。

用於連接到 SMTP 服務器並在 Windows 7 上發送電子郵件的 PHP 腳本

在 Windows 中從 PHP 發送電子郵件有點像雷區,有陷阱和頭疼。 我將嘗試向您介紹一個實例,在該實例中,我讓它在 (IIS) Internet 信息服務網絡服務器下的 Windows 7 和 PHP 5.2.3 上運行。

我假設您不想使用任何包含電子郵件發送功能的預構建框架,如 CodeIgniter 或 Symfony。 我們將從獨立的 PHP 文件發送電子郵件。 我從 codeigniter 引擎蓋下(在系統/庫下)獲取了這段代碼並修改了它,這樣你就可以放入這個 Email.php 文件,它應該可以正常工作。

這應該適用於較新版本的 PHP。 但你永遠不知道。

第 1 步,您需要使用 SMTP 服務器的用戶名/密碼:

我正在使用來自smtp.ihostexchange.net的 smtp 服務器,它已經為我創建和設置。 如果你沒有這個,你就不能繼續。 您應該能夠使用諸如 Thunderbird、evolution、Microsoft Outlook 之類電子郵件客戶端來指定您的 smtp 服務器,然后能夠通過那里發送電子郵件。

第 2 步,創建您的 Hello World 電子郵件文件:

我假設您正在使用 IIS。 因此,在C:\\inetpub\\wwwroot下創建一個名為 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:\\inetpub\\wwwroot下創建一個名為 Email.php 的新文件。

將此 PHP 代碼復制/粘貼到 Email.php 中:

https://github.com/sentientmachine/standalone_php_script_send_email/blob/master/Email.php

由於有多種 smtp 服務器,您將不得不手動Email.php頂部的Email.php 我已經對其進行了設置,因此它可以自動與smtp.ihostexchange.net一起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

第五步,在瀏覽器中運行剛才創建的 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 秒內到達。 如果沒有,請檢查頁面上返回的錯誤。 如果這不起作用,請嘗試在谷歌鍵盤上搗碎你的臉,同時高呼:“在雜貨店工作還不錯。”

您也可以使用 mandrill 應用程序在 php 中發送郵件。 您將從https://mandrillapp.com/api/docs/index.php.html獲取 API,您可以在其中找到有關已發送電子郵件的完整詳細信息和其他詳細信息。

您需要在表單中添加一個action例如:

<form name='form1' method='post' action='<?php echo($_SERVER['PHP_SELF']);'>
    <!-- All your input for the form here -->
</form>

然后將您的代碼段放在文檔的頂部,然后發送郵件。 什么echo($_SERVER['PHP_SELF']); 確實是它將您的信息發送到腳本的頂部,以便您可以使用它。

您需要一個 SMPT 服務器才能

... mail($to,$subject,$message,$headers);

上班。

您可以嘗試像 xmailer 這樣的輕量級 SMTP 服務器

以下是我使用的 PHP 郵件設置:

//Mail sending function
$subject = $_POST['name'];
$to = $_POST['email'];
$from = "zenphoto@example.com";

//data
$msg = "Your MSG <br>\n";       

//Headers
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;

mail($to,$subject,$msg,$headers);
echo "Mail Sent.";

我認為原始代碼中的一個錯誤可能是它有:

$message = echo getRequestURI();

代替:

$message = getRequestURI();

(盡管此代碼已被編輯。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM