dongwei1895 2014-08-20 10:18
浏览 70

使用带表单数据的PHP发送HTML电子邮件

Edited to try and make this read easier!

My company sends out HTML Emails, and I've ben instructed to create a "send to a friend" feature, because aparantly the "Forward" button isn't enough!

So, the user clicks the "send to a friend" link, which takes them to a web page. The url is actually a query string that captures the users name and email, to save them time, and also the URL of the web version of the HTML email:

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

So I've got sendersName, sendersEmail and also webVersionURL to play with. Great.

I then have a form where this user can type in a name and email address of the intended recipient - recipientName and recipientEmail.

What I need to do is send the email when the form has been filled in. At the moment the code I have below sends it as soon as the page is loaded, which is not correct.

I do not know how to wrap the send email in a function that only sends once the form has been filled in, can anyone help with this part in particular?

    <!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' . "
";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";

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

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

    echo "Mail Sent.";
    ?>
    </body>
    </html>
  • 写回答

1条回答 默认 最新

  • dongtaihui5131 2014-08-20 10:22
    关注

    When you post your form the GET values from the form will overwrite those that were originally sent through so your sendersEmail and sendersName will be NULL.

    You should store the html for your email as a variable so you can pass it into the mail function. Not sure what you are trying to do with the iframe??

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算