phpemailsmtpcontact-formsendinblue

How to add php variables in php Sendinblue emails?


I want to send emails in php using Sendinblue transactional email. The problem is I need to add php variables in my email, but after I received it, the php variables didn't change to texts!

This is what I received:

https://drive.google.com/file/d/1--c84eZcSJpp9icfsNZeXxj048Y-d3f9/view?usp=drivesdk

This is my php code:

<?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'}
*/

?>

Is there any way to solve it?


Solution

  • In setText('$body') you used single quotes, not double, so the $body variable does not get auto-expanded - it passes it as literal string. You should either use double quotes, or simply pass it in as the variable itself, since it should already be a string (so just use setText($body) without any quotes).

    Furthermore, to avoid any possible escaping issues, you might want to switch to string concatenation for building the body variable, or use more explicit variable expansion by wrapping them in braces, like so:

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