从数据库中填充 $contact_name
filled $contact_name from database
我是 php 的新手。现在我想在发送电子邮件中设置 $contact_name 显示?在我的代码下方,请修复我的代码。收到的电子邮件总是显示“$contact_name”而不是 "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';
}
?>
改变
$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 将引号之间的字符串视为文字。
变化:
$mail->body
和
$mail->MsgHTML($message);
现在可以使用了。谢谢
我是 php 的新手。现在我想在发送电子邮件中设置 $contact_name 显示?在我的代码下方,请修复我的代码。收到的电子邮件总是显示“$contact_name”而不是 "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';
}
?>
改变
$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 将引号之间的字符串视为文字。
变化:
$mail->body
和
$mail->MsgHTML($message);
现在可以使用了。谢谢