PHPMailer如何传递变量

Kaa*_*ene -1 php parameter-passing phpmailer

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

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

在我的 gmail.php 上我有:

$mail->msgHTML(file_get_contents(dirname(__FILE__).'/MailActivation.php'), $variables);
Run Code Online (Sandbox Code Playgroud)

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

Kaa*_*ene 5

\xc2\xa1终于!

\n\n

结果是这样的:

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

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

\n\n
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% \n
Run Code Online (Sandbox Code Playgroud)\n\n

我认为使用 php 模板或 html 并不重要。

\n