I'm beginner on php. and now I want to set up $contact_name display in send email ? Below my code and please fix my code. email receive always show "$contact_name" not "my name"
<?php
if(isset($_POST['send_email']))
{
require_once('../src/PHPMailer.php');
require_once('../src/SMTP.php');
require_once('../src/Exception.php');
require_once('../vendor/autoload.php');
$mail = new phpmailer\phpmailer\PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2; // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->Port = 587; // Set the SMTP port
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mygmail@gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$contact_name = $_POST['contact_name']; // required
$mail->From = 'mygmail@gmail.com';
$mail->FromName = 'No Reply';
$mail->AddAddress('togmail@gmail.com'); // Add a recipient
$mail->AddAddress; // Name is optional
$mail->IsHTML(false); // Set email format to HTML
$mail->Subject = 'Add Contact';
$mail->Body = 'Dear All, ($contact_name) with $contact_ext has been addeds.';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
}
?>
Change:
$mail->body
with
$mail->MsgHTML($message);
It Works Now. Thanks
Change
$mail->Body = 'Dear All, ($contact_name) with $contact_ext has been addeds.';
To
$mail->Body = "Dear All, ($contact_name) with $contact_ext has been addeds.";
See the double quotes instead of single quotes.
Using double quotes makes php replace the variable name by its value. Single quotes makes php treat the string between the quotes as literal.