【问题标题】:PHP Mailer with HTML template and sending variables带有 HTML 模板和发送变量的 PHP 邮件程序
【发布时间】:2014-09-12 04:36:49
【问题描述】:

基本上我正在尝试这样做。

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 文件进行更改。

【问题讨论】:

  • 在您的 sample_mail.html 中将测试放在用户名和密码之前。你只是忘记了测试部分!
  • 优秀。帮助很大
  • 感谢您用代码很好地提出这个问题。即使您的变量名称有错字,我还是从您的问题(模板中的 %fieldname%)中得到了答案,因为我不知道如何将表单字段从我的注册表单转移到电子邮件中。

标签: php phpmailer


【解决方案1】:

其中有两个与其他的不同......

$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 问题...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-12
    • 2020-08-16
    • 2015-11-11
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    相关资源
    最近更新 更多