簡體   English   中英

使用PHP發送HTML電子郵件和表單數據

[英]Send HTML email using PHP with form data

編輯嘗試使此閱讀更容易!

我的公司發送HTML電子郵件,並已被指示創建“發送給朋友”功能,因為顯然“轉發”按鈕還不夠!

因此,用戶單擊“發送給朋友”鏈接,將其帶到網頁。 該url實際上是一個查詢字符串,它捕獲用戶名和電子郵件,以節省他們的時間,以及HTML電子郵件的網絡版本的URL:

send-to-a-friend.php/?sendersName=Drew&sendersEmail=name@gmail.com&webVersionURL=http://website.address/page.html

因此,我有sendersName,sendersEmail和webVersionURL可以使用。 大。

然后,我有了一個表單,該用戶可以在其中輸入所需收件人的名稱和電子郵件地址-收件人姓名和收件人電子郵件。

我需要做的是在填寫表單后發送電子郵件。此刻,我下面的代碼在頁面加載后立即發送。這是不正確的。

我不知道如何將發送電子郵件包裝為僅在填寫表單后才發送的功能,有人可以特別幫助這部分嗎?

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>test send to a friend</title>
    </head>

    <body>

     <p>Who do you want to send this email to?</p>
    <form method="get" action="">
         <input type="text" id="recipientName" value="" name="recipientName" />
         <input type="text" id="recipientEmail" value="Email" name="recipientEmail" />
         <?php
             $_POST['recipientName'];
             $_POST['recipientEmail'];
         ?>
         <input type="submit" />
     </form>

     <hr />

     <p>This is what your message will look like:</p>
     <hr />
     <p>Hi <strong>RECIPIENT</strong>, your friend <strong><?php echo $_GET["sendersName"]; ?></strong> (<?php echo $_GET["sendersEmail"]; ?>) thought you might be interested in this email from shelter!</p>
     <p>------</p>
    <p>
    From: <?php echo $_GET["sendersName"]; ?> <br />
    Sent: <?php date_default_timezone_set('Europe/London'); $date = date('d/m/Y h:i a', time()); echo $date; ?><br />
    To: <strong>RECIPIENT</strong><br />
    Subject: FWD: <strong>Subject Line</strong><br />
    </p>

    <iframe src="<?php echo $_GET["webVersionURL"]; ?>" width="700" height="400"></iframe>


    <?php

    $to = $recipientEmail;

    // subject
    $subject = "Test mail";

    // message
    $message = file_get_contents($_GET["webVersionURL"]);

    // from
    $from = "".$_GET["sendersName"]."<".$_GET["sendersEmail"].">";

    // To send HTML mail, the Content-type header must be set
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    $headers .= "From:" . $from;

    // Mail it
    mail($to,$subject,$message,$headers);

    echo "Mail Sent.";
    ?>
    </body>
    </html>

當您發布表單時,表單中的GET值將覆蓋最初發送的值,因此sendersEmail和sendersName將為NULL。

您應該將電子郵件的html作為變量存儲,以便可以將其傳遞到mail函數。 不確定要使用iframe做什么?

暫無
暫無

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

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