【问题标题】:GMail SMTP avoid using direct password in PHP codeGMail SMTP 避免在 PHP 代码中使用直接密码
【发布时间】:2013-06-18 07:14:02
【问题描述】:

我将使用 PHP 发送电子邮件。我的 PHP 应该在共享主机上。我发现codes 可以通过 PHP 发送电子邮件,但我担心将直接密码写入 PHP 代码。 PHP 或 GMail SMTP 服务器是否提供任何方法来避免使用直接密码?也许类似于 API 身份验证?

【问题讨论】:

    标签: php authentication smtp gmail


    【解决方案1】:

    我从来没有真正尝试过仅通过 SMTP 服务器发送电子邮件,例如 google,因为所有这些服务都需要身份验证。 Github 中有一些很棒的项目,例如this,这允许使用安全端口、身份验证、SSL 等通过 SMTP 发送电子邮件。(请记住,php 文件是服务器端的,因此用户无法访问此信息。所以你的通行证它是安全的。)

    如果您访问他们的仓库,您会找到他们的示例:

    <?php
    require 'class.phpmailer.php';
    
    $mail = new PHPMailer;
    
    $mail->IsSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'jswan';                            // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
    
    $mail->From = 'from@example.com';
    $mail->FromName = 'Mailer';
    $mail->AddAddress('josh@example.net', 'Josh Adams');  // Add a recipient
    $mail->AddAddress('ellen@example.com');               // Name is optional
    $mail->AddReplyTo('info@example.com', 'Information');
    $mail->AddCC('cc@example.com');
    $mail->AddBCC('bcc@example.com');
    
    $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
    $mail->AddAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->AddAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    $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->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
    if(!$mail->Send()) {
       echo 'Message could not be sent.';
       echo 'Mailer Error: ' . $mail->ErrorInfo;
       exit;
    }
    
    echo 'Message has been sent';
    

    但是如果你不想在 php 控制器上硬编码密码或者你必须使用什么来处理电子邮件,你总是可以使用 php 上的mail() 函数来发送电子邮件。

    在文档中轻松找到示例:

    <?php
    $to      = 'nobody@example.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: webmaster@example.com' . "\r\n" .
        'Reply-To: webmaster@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    
    mail($to, $subject, $message, $headers);
    ?>
    

    此功能是一种发送电子邮件的 SMTP 方式,但始终需要验证,但在这种情况下,它是由服务器进行的,因此必须设置所有标头才能发送正确的电子邮件。

    请注意,有些服务器提供商不会大声发送电子邮件支持此功能,因此最终您需要创建或使用 php SMTP 电子邮件类来处理电子邮件,并且此文件必须具有密码以进行身份​​验证。

    【讨论】:

    • 似乎没有比制作虚假电子邮件更好的方法了!
    【解决方案2】:

    PHP 是在服务器端执行的,因此您的密码不应向公众公开。如果您所说的共享主机是指您实际上是在与某人共享服务器,那么您的 Gmail 帐户的密码可能会被盗,因为 Gmail smtp 需要密码。但是,如果您的服务器主机为您提供了一个传出 smtp 服务器,那么您当然不需要担心您的密码,因为您不需要密码。

    【讨论】:

    • 您可以针对该问题提出任何可能的解决方法。长段确实对问题提供了一些见解,但确实需要进一步澄清。
    猜你喜欢
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多