加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP Mailer与HTML模板和发送变量

发布时间:2020-12-13 14:04:54 所属栏目:PHP教程 来源:网络整理
导读:基本上我试图这样做 http://www.xeweb.net/2009/12/31/sending-emails-the-right-way-using-phpmailer-and-email-templates/ 这是我的代码 的index.php ?php include('class.phpmailer.php'); // Retrieve the email template required $message = file_get_
基本上我试图这样做

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

这是我的代码

的index.php

<?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;
    } 
    ?>

mail_templates / sample_mail.html

<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文件中进行更改,而不用触摸代码部分.

其中两件事不像别人那样
$message = str_replace('%testusername%',$message); 
$message = str_replace('%testpassword%',$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问题…

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读