在PHPMailer中使用全局变量



而不是这个:

$mail->isSMTP();  
$mail->Host = 'smtp.demo-server.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 'demo-port';
$mail->Username = 'demo-email@gmail.com'; 
$mail->Password = 'demo-password';

我想将这些值保存在一个单独的文件中,并使用变量:

$mail->isSMTP();  
$mail->Host = echo $server; // also tried without echo
$mail->SMTPAuth = true;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = echo $port;
$mail->Username = echo $username; 
$mail->Password = echo $password;

我已经在使用输出缓冲,设置了全局范围,变量值包含引号。仍然不起作用。

首先,请阅读@Synchro的评论。

  • 如果您还没有创建一个php文件(例如mail_config.php(,并在其中声明您的变量
  • 要求/包括CCD_ 2文件在您的";主";文件(将发送邮件的文件,例如send_mail.php(
  • 从CCD_ 5文件中的CCD_

示例

mail_config.php

<?php
$server   = 'smtp.gmail.com';
$port     = 465;
$username = 'test@gmail.com';
$password = '123456';
?>

send_mail.php

<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
// require the settings file. This contains the settings
require 'path/to/mail_config.php';
// require the vendor/autoload.php file if you're using Composer
require 'path/to/vendor/autoload.php';
// require phpmailer files if you're not using Composer
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host          = $server;
$mail->SMTPAuth      = true;
$mail->SMTPSecure    = PHPMailer::ENCRYPTION_SMTPS; // 'ssl'
$mail->Port          = $port;
$mail->Username      = $username;
$mail->Password      = $password;
$mail->setFrom('from@example.com', "Sender's Name"); // sender's email(mostly the `$username`) and name.
$mail->addAddress('test@example.com', 'Stackoverflow'); // recipient email and optional name.
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->send();
echo 'Message has been sent';
} catch(Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

阅读更多https://github.com/PHPMailer/PHPMailer

相关内容

  • 没有找到相关文章

最新更新