首页 > 解决方案 > 如何在 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'}
*/

?>

有什么办法可以解决吗?

标签: phpemailsmtpcontact-formsendinblue

解决方案


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}";

推荐阅读