【问题标题】:Using PHP to require and modify html content to send as html-email使用 PHP 要求和修改 html 内容以作为 html-email 发送
【发布时间】:2017-08-03 05:09:05
【问题描述】:

我正在使用 PHPmailer 发送我的电子邮件。我创建了一个表单,您可以在其中输入一些详细信息(包括电子邮件地址)。单击提交/生成后,我想使用表单详细信息修改电子邮件的内容。电子邮件消息应显示为 html 电子邮件。但是,我只是无法让我的表单正确显示或让我的 html 电子邮件正确发送。

表单代码(index.php): `

<?php
    //PHP mailer code
    require 'PHPmailer/PHPMailerAutoload.php';
    $emailmessage = require 'mail.php';

    $mail = new PHPMailer;

    //$mail->SMTPDebug = 3;                // Enable verbose debug output
    if (isset($_POST['submit'])) {
        $mail->isSMTP();                   // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';    // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;            // Enable SMTP authentication
        $mail->Username = 'email@email.com';    // SMTP username
        $mail->Password = 'password';      // SMTP password
        $mail->SMTPSecure = 'tls';   // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                 // TCP port to connect to

        $mail->setFrom('email@email.com', 'Me');
        $mail->addAddress($_POST['emailrecipients'], $_POST['client']);  // Add a recipient

        $mail->isHTML(true);               // Set email format to HTML

        $mail->Subject = $_POST['callid'].' | Customer Feedback';
        $mail->Body    = $emailmessage;
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        if(!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message has been sent';
        }
    }
?>
<!DOCTYPE html>
    <html>
        <body>
        <form>
            <input type="text" id="firstname" name="firstname" placeholder="John" />

            <input type="text" id="lastname" name="lastname" placeholder="Doe" />

            <input type="email" id="email" name="email" placeholder="email@email.com" />

            <button type="submit" name="submit">Generate</button>
        </form>
        </body>
    </html>`

电子邮件代码(mail.php):

<!DOCTYPE html>
<html>
<body>
<p>First Name: <?php $_POST['firstname']; ?></p>
<p>Last Name: <?php $_POST['lastname']; ?></p>
</body>
</html>

我曾设想,在提交时,所需的电子邮件将根据输入字段进行更改,然后发送到在电子邮件输入字段中键入的地址。

【问题讨论】:

  • 您的 HTML 中没有 form

标签: php email phpmailer html-email


【解决方案1】:

我不确定这条线是否按预期工作:

$emailmessage = require 'mail.php';

您的 mail.php 应该如下所示:

    $emailmessage = "
        <!DOCTYPE html>
        <html>
        <body>
        <p>First Name:".$_POST['firstname']."</p>
        <p>Last Name:  ".$_POST['lastname']."</p>
        </body>
        </html>";

然后只用

require "mail.php" 

在代码的第 2 行。您还应该将“表单”标签添加到您的 HTML 表单中。

【讨论】:

  • 感谢您的回答。我试过了。但现在看来我必须将 emailmessage 变量中包含的所有双引号更改为单引号。例如: $emailmessage = " ...

    First Name: ".$_POST['firstname']."

    ..." 现在变成 $emailmessage = "...

    First Name: '.$_POST['firstname'].'

    ..."。这看起来对吗?还是 XAMP 服务器要求的?
  • 您可以使用反引号来保存您的双引号,例如\"
猜你喜欢
  • 2011-08-23
  • 2021-06-22
  • 1970-01-01
  • 2017-03-19
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
相关资源
最近更新 更多