PHP Mailer与HTML模板和发送变量

前端之家收集整理的这篇文章主要介绍了PHP Mailer与HTML模板和发送变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
基本上我试图这样做

http://www.xeweb.net/2009/12/31/sending-emails-the-right-way-using-phpmailer-and-email-templates/

这是我的代码

的index.PHP

  1. <?PHP
  2. include('class.PHPmailer.PHP'); // Retrieve the email template required
  3. $message = file_get_contents('mail_templates/sample_mail.html');
  4. $message = str_replace('%testusername%',$username,$message);
  5. $message = str_replace('%testpassword%',$password,$message);
  6. $mail = new PHPMailer(); $mail->IsSMTP(); // This is the SMTP mail server
  7.  
  8. $mail->SMTPSecure = 'tls';
  9. $mail->Host = "smtp.gmail.com";
  10. $mail->Port = 587;
  11. $mail->SMTPAuth = true;
  12. $mail->Username = 'mygmailid@gmail.com';
  13. $mail->Password = 'mypassword';
  14. $mail->SetFrom('fromgmail@gmail.com','Pricol Technologies');
  15. $mail->AddAddress('addaddress@gmail.com');
  16. $mail->Subject = 'Your account information';
  17. $mail->MsgHTML($message);
  18. $mail->IsHTML(true);
  19. $mail->CharSet="utf-8";
  20. //$mail->AltBody(strip_tags($message));
  21. if(!$mail->Send()) {
  22. echo "Mailer Error: " . $mail->ErrorInfo;
  23. }
  24. ?>

mail_templates / sample_mail.html

  1. <html>
  2. <body>
  3. <h1>Account Details</h1>
  4. <p>Thank you for registering on our site,your account details are as follows:<br>
  5. Username: %username%<br>
  6. Password: %password% </p>
  7. </body>
  8. </html>

我收到邮件如下:

  1. Account Details
  2.  
  3. Thank you for registering on our site,your account details are as follows:
  4. Username: %testusername%
  5. Password: %testpassword%

预期产量

  1. Account Details
  2.  
  3. Thank you for registering on our site,your account details are as follows:
  4. Username: testusername
  5. Password: testpassword

我哪里错了我已经检查过一些论坛.但没有用.

我以前问过一些问题.但是我的项目要求是使用%变量名%的HTML模板,以便任何人都可以在html文件中进行更改,而不用触摸代码部分.

其中两件事不像别人那样
  1. $message = str_replace('%testusername%',$message);
  2. $message = str_replace('%testpassword%',$message);
  3. ^^^^---note "test"
  4.  
  5.  
  6. <p>Thank you for registering on our site,your account details are as follows:<br>
  7. Username: %username%<br>
  8. Password: %password% </p>
  9. ^---note the LACK of "test"

您的脚本正常工作,这是一个PEBKAC问题…

猜你在找的PHP相关文章