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

将SMTP凭据传递给PHPMailer配置,而不在heroku中公开它,php,github,heroku,smtp,phpmailer,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

我正在使用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();

有很多可能的方法-

  • 使用Heroku CLI

  • 您可以在您的网站中创建一个管理页面,从中放置smtp 提供将保留到文件中的详细信息。此文件将被删除 在heroku dyno重新启动之前可用

  • 您可以使用dropbox进行部署
  • 您可以使用bitbucket来代替Github git回购协议也免费提供

  • 有很多可能的方法-

  • 使用Heroku CLI

  • 您可以在您的网站中创建一个管理页面,从中放置smtp 提供将保留到文件中的详细信息。此文件将被删除 在heroku dyno重新启动之前可用

  • 您可以使用dropbox进行部署
  • 您可以使用bitbucket来代替Github git回购协议也免费提供

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

    为heroku应用程序设置环境变量的过程记录在以下链接中-

    在环境变量中设置用户名和密码后,可以使用以下代码访问它们

        $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
    
    参考资料
    -

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

    为heroku应用程序设置环境变量的过程记录在以下链接中-

    在环境变量中设置用户名和密码后,可以使用以下代码访问它们

        $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
    
    参考资料 -