基本上我试图这样做。
http://www.xeweb.net/2009/12/31/sending-emails-the-right-way-using-phpmailer-and-email-templates/
这是我的代码
<?php
include('class.phpmailer.php'); // Retrieve the email template required
$message = file_get_contents('mail_templates/sample_mail.html');
$message = str_replace('%testusername%', $username, $message);
$message = str_replace('%testpassword%', $password, $message);
$mail = new PHPMailer(); $mail->IsSMTP(); // This is the SMTP mail server
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'mygmailid@gmail.com';
$mail->Password = 'mypassword';
$mail->SetFrom('fromgmail@gmail.com', 'Pricol Technologies');
$mail->AddAddress('addaddress@gmail.com');
$mail->Subject = 'Your account information';
$mail->MsgHTML($message);
$mail->IsHTML(true);
$mail->CharSet="utf-8";
//$mail->AltBody(strip_tags($message));
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
?>
<html>
<body>
<h1>Account Details</h1>
<p>Thank you for registering on our site, your account details are as follows:<br>
Username: %username%<br>
Password: %password% </p>
</body>
</html>
我收到的邮件如下:
Account Details
Thank you for registering on our site, your account details are as follows:
Username: %testusername%
Password: %testpassword%
Account Details
Thank you for registering on our site, your account details are as follows:
Username: testusername
Password: testpassword
我哪里出错了。我查了一些论坛。但没用。
我之前曾问过一些问题。但我的项目要求是使用%变量名为%的html模板,这样任何人都可以在不触及代码部分的情况下对html文件进行更改。
答案 0 :(得分:22)
其中两件事与其他事情不同......
$message = str_replace('%testusername%', $username, $message);
$message = str_replace('%testpassword%', $password, $message);
^^^^---note "test"
<p>Thank you for registering on our site, your account details are as follows:<br>
Username: %username%<br>
Password: %password% </p>
^---note the LACK of "test"
您的脚本工作正常,它是PEBKAC问题......