php - 如何在 php Sendinblue 电子邮件中添加 php 变量?
问题描述
我想使用 Sendinblue 交易电子邮件在 php 中发送电子邮件。问题是我需要在我的电子邮件中添加 php 变量,但是在我收到它之后,php 变量并没有变成文本!
这是我收到的:
https://drive.google.com/file/d/1--c84eZcSJpp9icfsNZeXxj048Y-d3f9/view?usp=drivesdk
这是我的 php 代码:
<?php
// Check for empty fields
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone']) || empty($_POST['message']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
http_response_code(500);
exit();
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$subject = "HMP Reseller - New Message";
$body = "You have received a new message from HMP Reseller contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email\n\nPhone: $phone\n\nMessage:\n$message";
include 'Mailin.php';
$mailin = new Mailin('hosteymega@gmail.com', 'API-KEY');
$mailin->
addTo('hosteymega@gmail.com', 'HosteyMega Hosting')->
setFrom('admin@hosteyme.ga', 'HosteyMega Admin')->
setReplyTo('$email','HMP Reseller Client')->
setSubject('$subject')->
setText('$body')->
setHtml('<h2>$body</h2>');
$res = $mailin->send();
/**
Successful send message will be returned in this format:
{'result' => true, 'message' => 'Email sent'}
*/
?>
有什么办法可以解决吗?
解决方案
在setText('$body')
您使用单引号而不是双引号时,因此$body
变量不会自动扩展-它将其作为文字字符串传递。您应该使用双引号,或者只是将其作为变量本身传递,因为它应该已经是一个字符串(所以只使用setText($body)
不带任何引号)。
此外,为了避免任何可能的转义问题,您可能希望切换到字符串连接来构建主体变量,或者通过将它们包装在大括号中来使用更明确的变量扩展,如下所示:
$body = "You have received a new message from HMP Reseller contact form.\n\n"."Here are the details:\n\nName: ${name}\n\nEmail: ${email}\n\nPhone: ${phone}\n\nMessage:\n${message}";
推荐阅读
- tomcat - 为什么我的tomcat控制台在启动后就死机了?
- raspberry-pi - Raspberry Pi Pico - Thonny 解释器的问题
- javascript - 隐藏的音频文件不播放
- python - 如何将向量的每个元素提升到另一个向量的每个元素的幂?
- php - 使用 php 仅在桌面设备上显示 div
- javascript - Postman - 循环遍历嵌套对象数组以创建变量
- sql - 将 2 个不同表中的值与包含进行比较
- reactjs - 如果发生错误,在 redux-saga 中的 api 调用后调度一个动作
- html - 如何使用 CSS 去除 html 表格中的白线
- sql - 如何以 10 为增量更新所有行的列