【问题标题】:PHPMailer how to pass variablePHPMailer如何传递变量
【发布时间】:2016-08-09 05:22:58
【问题描述】:

我见过有人使用$mail->get_include_contents(),但我不知道它是如何工作的。

我想通过电子邮件激活一个帐户。我有一个模板 php、html 或 tpl,我想传递用户名、随机密钥、电子邮件等。考虑到我在所有项目中都使用 Smarty。

在我的 gmail.php 上,我有:

$mail->msgHTML(file_get_contents(dirname(__FILE__).'/MailActivation.php'), $variables);

有人可以向我解释一下我该怎么做吗?

【问题讨论】:

    标签: php parameter-passing phpmailer


    【解决方案1】:

    根据给定的文件路径包含文件,如果没有给出,则根据指定的 include_path。如果在 include_path 中找不到该文件,则 include 最终会在失败之前检查调用脚本自己的目录和当前工作目录。如果找不到文件,include 构造将发出警告;这与 require 的行为不同,后者会发出致命错误。

     $mail->Body = get_include_contents('sample.php', $variable); 
    

    sample.php

    <table width='600px' cellpadding='0' cellspacing='0'>
    <tr><td bgcolor='#eeeeee'><img src='logo.png' /></td></tr>
    <tr><td bgcolor='#ffffff'  bordercolor='#eeeeee'>
    <div style='border:1px solid #eeeeee;font-family:Segoe  UI,Tahoma,Verdana,Arial,sans-serif;padding:20px 10px;'>
    <p>Variable one is <?php echo $one; ?>.</p>
     <p>Variable two is <?php echo $two; ?>.</p>
    <p>Thanks</p>
    </div>
    </td></tr>
    </table>
    

    【讨论】:

      【解决方案2】:

      ¡终于!

      结果如下:

          <?php
      
          date_default_timezone_set('Etc/UTC');
      
          require(dirname(__FILE__).'/../PHPMailerAutoload.php');
          require_once (dirname(__FILE__).'/../../controller/SignUpController.php');
      
          $variables = [$username, $email, $randomkey];
      
          //Create a new PHPMailer instance
          $mail = new PHPMailer;
      
          //Tell PHPMailer to use SMTP
          $mail->isSMTP();
      
          //Enable SMTP debugging
          // 0 = off (for production use)
          // 1 = client messages
          // 2 = client and server messages
          $mail->SMTPDebug = 0;
      
          //Ask for HTML-friendly debug output
          $mail->Debugoutput = 'html';
      
          //Set the hostname of the mail server
          $mail->Host = 'smtp.gmail.com';
          // use
          // $mail->Host = gethostbyname('smtp.gmail.com');
          // if your network does not support SMTP over IPv6
      
          //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
          $mail->Port = 587;
      
          //Set the encryption system to use - ssl (deprecated) or tls
          $mail->SMTPSecure = 'tls';
      
          //Whether to use SMTP authentication
          $mail->SMTPAuth = true;
      
          //Username to use for SMTP authentication - use full email address for gmail
          $mail->Username = "your_email";
      
          //Password to use for SMTP authentication
          $mail->Password = "your_password_email";
      
          //Set who the message is to be sent from
          $mail->setFrom('user_email_from', 'title_to_display');
      
          //Set an alternative reply-to address: it's optional
          //$mail->addReplyTo('user_email_reply', 'user_email_name');
      
          //Set who the message is to be sent to
          $mail->addAddress($email, $firstname . " " . $lastname);
      
          //Set the subject line
          $mail->Subject = 'Subject';
      
      // Settin variables    
          $message = file_get_contents(dirname(__FILE__).'/MailActivation.html');
          $message = str_replace('%testusername%', $username, $message);
          $message = str_replace('%testemail%', $email, $message);
          $message = str_replace('%testrandomkey%', $randomkey, $message);
      
          $mail->msgHTML($message);
      
          //Replace the plain text body with one created manually
          //$mail->AltBody = 'This is a plain-text message body';
      
          //Attach an image file
          //$mail->addAttachment('images/phpmailer_mini.png');
      
      // For sending an image inline
          $mail->AddEmbeddedImage(dirname(__FILE__).'/images/frontimage.jpg', 'front', 'images/frontimage.jpg');
      
          //send the message, check for errors
          if (!$mail->send()) {
              echo "Mailer Error: " . $mail->ErrorInfo;
          } else {
      
      
                 echo "Message sent!";
              }
      ?>
      

      在我的 ActivationTemplate.html 中,我只是创建了一个 html 文件,并且我想在其中插入刚刚放入的变量:

      Lorem ipsum %testusername% dolor sit amet, consectetur adipiscing elit. Integer nulla elit, mattis non leo in, ornare interdum ante. Proin eget velit purus. Sed convallis lectus sed libero ornare, sit amet pharetra nisi facilisis. Cras fermentum nulla quis purus egestas, in accumsan nisl dictum. Donec in nisi vel enim pretium posuere at nec ante. Mauris tempus velit vel urna commodo accumsan. %testemail% 
      

      我觉得用php模板还是html没关系。

      【讨论】:

        猜你喜欢
        • 2015-09-01
        • 2015-08-30
        • 2019-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-07
        • 1970-01-01
        • 2020-12-20
        相关资源
        最近更新 更多