将SMTP凭据传递给PHPMailer配置,而不在heroku中公开它

时间:2018-01-05 04:38:19

标签: php github heroku smtp phpmailer

我正在使用 phpmailer 库。要发送电子邮件,我需要指定smtp服务器,smtp用户和smtp密码。问题是我正在使用云平台 Heroku ,我使用github存储库自动部署到heroku,我不希望我的smtp用户名和密码是公开的。有办法解决这个问题吗?

以下是代码段

  $mail = new PHPMailer(false); // Passing `true` enables exceptions

    //Server settings
    //$mail->SMTPDebug = 1;//Enable verbose debug output
    $mail->isSMTP();//Set mailer to use SMTP
    $mail->Host = 'smtp host';//Specify main and backup SMTP servers
    $mail->SMTPAuth = true;//Enable SMTP authentication
    $mail->Username = 'user';//SMTP username
    $mail->Password = 'password';//SMTP password
    $mail->SMTPSecure = 'tls';//Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;//TCP port to connect to


    //Recipients
    $mail->setFrom('example@example.com','myapp');
    $mail->addAddress('hi@examle.com');//Add a recipient
    //$mail->addAddress('ellen@example.com');//Name is optional
    $mail->addReplyTo('support@example.com','Contact');



    //Content
    $mail->isHTML(true);//Set email format to HTML
    $mail->Subject = 'test';

    $mail->Body    = 'this is a test';


    $mail->send();

2 个答案:

答案 0 :(得分:3)

一种不暴露但在代码中使用它的方法是在环境变量中设置SMTP用户名和密码

此链接中记录了为heroku应用程序设置环境变量的过程 - https://devcenter.heroku.com/articles/config-vars

在您的环境变量中设置了用户名和密码后,您可以使用以下代码

访问它们
    $mail->isSMTP();//Set mailer to use SMTP
    $mail->Host = 'smtp host';//Specify main and backup SMTP servers
    $mail->SMTPAuth = true;//Enable SMTP authentication
    //Assuming SMTP_USERNAME is your environment variable which holds username
    $mail->Username = getenv('SMTP_USERNAME');
    //Assuming SMTP_PASSWORD is your environment variable which holds password
    $mail->Password = getenv('SMTP_PASSWORD');
    $mail->SMTPSecure = 'tls';//Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;//TCP port to connect to

参考   - Use Heroku config vars with PHP?

答案 1 :(得分:1)

有很多可能的方法 -

  1. 使用Heroku CLI https://devcenter.heroku.com/categories/command-line

  2. 您可以在您放置smtp的网站中创建管理页面 提供将持久保存到文件的详细信息。这个文件将是 直到heroku dyno重启。

  3. 您可以使用 dropbox 进行部署
  4. 您可以使用 bitbucket 代替 Github 使用git repo也是免费的 https://confluence.atlassian.com/bitbucket/deploy-to-heroku-872013667.html
相关问题