我是php的初学者。现在我想在发送电子邮件中设置$ contact_name显示?在我的代码下方,请修复我的代码。电子邮件接收始终显示“ $ contact_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';
}
?>
答案 0 :(得分:0)
更改
$mail->Body = 'Dear All, ($contact_name) with $contact_ext has been addeds.';
收件人
$mail->Body = "Dear All, ($contact_name) with $contact_ext has been addeds.";
查看双引号而不是单引号。
使用双引号使php用其值替换变量名。单引号使php将引号之间的字符串视为文字。
答案 1 :(得分:0)
更改:
$mail->body
使用
$mail->MsgHTML($message);
现在可以使用。谢谢