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

时间:2022-10-17 11:44:27

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!

我的公司发送了HTML电子邮件,我已经指示创建一个“发送给朋友”功能,因为“前进”按钮是不够的!

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:

因此,用户单击“发送给朋友”链接,该链接将他们带到网页。 url实际上是一个查询字符串,用于捕获用户名和电子邮件,以节省时间,以及HTML电子邮件的Web版本的URL:

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.

所以我有sendersName,sendersEmail和webVersionURL一起玩。大。

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

然后我有一个表单,用户可以在其中键入目标收件人的名称和电子邮件地址 - recipientName和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' . "\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>

1 个解决方案

#1


0  

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.

当您发布表单时,表单中的GET值将覆盖最初发送的值,因此您的sendersEmail和sendersName将为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??

您应该将电子邮件的html存储为变量,以便将其传递给邮件功能。不确定你要用iframe做什么?

#1


0  

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.

当您发布表单时,表单中的GET值将覆盖最初发送的值,因此您的sendersEmail和sendersName将为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??

您应该将电子邮件的html存储为变量,以便将其传递给邮件功能。不确定你要用iframe做什么?