【问题标题】:Send Echo Messages in PHP mail()在 PHP mail() 中发送 Echo 消息
【发布时间】:2016-12-08 21:10:33
【问题描述】:

我有以下 php 代码

$sql = "SELECT * FROM Requests ORDER BY RequestID DESC LIMIT 1";
$result = $conn->query($sql);
while ($row1 = $result->fetch_assoc()){
echo "Name: " . $row1['FirstName'] . " " . $row1['LastName'];
echo "<br> Date Needed: " . $row1['DateNeeded'];
echo "<br> Company: " . $row1['Company'] . "<br> Account: " . $row1['Account'] . "<br> Brand: " . $row1['Brand'] . "<br> Wants: ";
    $requestID = $row1['RequestID'];
    $sql = "SELECT * FROM ItemsPerRequest, Items WHERE Items.ItemID = ItemsPerRequest.ItemID AND RequestID = '$requestID'";
    $result = $conn->query($sql);
        while ($row = $result->fetch_assoc()) {
        echo $row ['Quantity'] . " ";
        echo $row['ItemDescription'];
            if ($row['Quantity'] > 1) {
            echo "s";
            }
        echo " <br> ";
        }
echo "Comments: " . $row1['Comments'];
echo "<br> File Name: " . $row1['FileLink'];
$file = $row1['FileLink'];

}

效果很好,例如给我这个输出
名称:Sharkn8do
所需日期:2016-12-28
公司:饮料
帐户:布法罗酒吧 品牌: Grand
想要:4 传单 - 大号 11"x17"
18 桌帐篷 - 框架样式
评论:现在在草稿大峡谷咖啡豆世涛
文件名称:2016-12-08-000000015-Me.jpg
现在,我希望在电子邮件正文中输出。我尝试将其复制并粘贴到

$message = "Form submission";

我的 mail() 代码的一部分.. 但这确实行不通。我想要的只是代码中的回声部分。但!因为 Wants: (4 flyer, 18 table tent) 部分是基于数组生成的,所以我不能真正分配变量并发送主题,对吧?
这是邮件代码:

$to = "test@email.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$message = //WHAT DO;

$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.

没有 PHPMailer 或类似的建议。我试过了,不喜欢,不起作用……所以请只使用 mail()。

【问题讨论】:

  • 显示邮件代码
  • 创建一个类似$output=''; 的变量,而不是回显,并将每个回显替换为$output .= [text to be echo'ed here];。这将创建一个带有回显的字符串。然后您可以在末尾回显该值以显示或将其添加到电子邮件中。
  • "没有 PHPMailer 或类似的建议。"真的,如果你想建议你应该对所有选项持开放态度,phpmailer 比 php 内置的 mail() 好 100 倍。但这不是问题,只是观察。
  • @JonathanKuhn 好的,但我在回应 $row['ItemDescription'];可以是多个项目,在这个例子中就是这样。项目传单和桌帐篷都是以这种方式呼应的。如果我说 $var = $row['ItemDescription'];每次while循环运行时,那个var不会改变吗?
  • 注意.= 点表示添加到现有变量

标签: php email


【解决方案1】:

创建一个类似$output=''; 的变量,而不是回显,并将每个回显替换为$output .= [text to be echo'ed here];。这将创建一个带有回显的字符串。然后您可以在末尾回显该值以显示或将其添加到电子邮件中。

例如:

$output = "";

//just a simple loop
for($i=0; $i<10; $i++){
    //basically what you have echo'ing a string in a loop.
    //echo "The loop is on number: {$i}\n";

    //what you should do with concatenation
    $output .= "The loop is on number: {$i}\n";
}

//now you have a variable with the same text that
//would have been echo'ed. You can echo it here to
//get the same output or use it somewhere else.
echo $output;

//or email it
mail($to, $subject, "This is the output: {$output}", $headers);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    相关资源
    最近更新 更多