簡體   English   中英

如何將基本的 PHP function 注入 PHPmailer Z0C83F57C786A0B4A39EFAB2377EBC 的 HTML 主體

[英]How to inject basic PHP function into HTML body of a PHPmailer email?

I am pretty new to backend PHP developement... I know how to capture posted data into PHP variables and inject those variables into the body of my HTML email. 但是,我不知道如何在我的 HTML 中實現 PHP “foreach” function。

目標如下:

Use PHPmailer to send an HTML email using a simple PHP "foreach" function to capture input data and paste them into the body of the HTML email.

我已經簡化了我的問題:我想為每個 function 使用一個來捕獲所有具有名稱屬性“FRUIT”的輸入,並將它們各自的數據屬性和值注入我的 HTML 的主體中。

預期的行為將是接收一個 email ,其主體如下所示:

蘋果:1

香蕉:2

這是我簡化的 HTML表格:

   <form id="form" action="(action.php) method="POST">
      <input type="text" name="FRUIT[]" data-attribute="apple" value="1">
      <input type="text" name="FRUIT[]" data-attribute="banana" value="2">
      <input type="submit" value="submit">
    </form>

這是我簡化的 PHP動作腳本:

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require '/home/furtbswy/public_html/PHPMailer-master/src/Exception.php';
require '/home/furtbswy/public_html//PHPMailer-master/src/PHPMailer.php';
require '/home/furtbswy/public_html/PHPMailer-master/src/SMTP.php';


$mail = new PHPMailer(false);

try {

    $from = 'xxxxxxh@gmail.com';
    $to = $_POST['EMAIL'];
    $fruits= '';
    foreach ($_POST['FRUIT'] as $key => $value) {
      $fruits.= "$key: $value <br>";
    }
    $message = '
        <html>
            <body>
            Fruits: ' . $fruits. '
            </body>
        </html>
        ';

    // PHPmailer setup
    $mail = new PHPMailer();
    $mail->CharSet = 'UTF-8';
    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->Username = $from;
    $mail->Password = 'xxxxxx';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

    // recipients
    $mail->setFrom($from);
    $mail->addAddress($to);

    // content
    $mail->Subject = 'confirmation';
    $mail->isHTML(true);
    $mail->Body = $message;
    $mail->send();

} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

您不能將foreach用作表達式。 Append 到循環中的變量:

$result = '';
foreach ($_POST['NAME'] as $key => $value) {
    $result .= "$key: $value <br>";
}

暫無
暫無

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

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