GMail SMTP避免在PHP代码中使用直接密码

GMail SMTP避免在PHP代码中使用直接密码,php,authentication,smtp,gmail,Php,Authentication,Smtp,Gmail,我将使用PHP发送电子邮件。我的PHP应该在共享主机上。我发现可以通过PHP发送电子邮件,但我担心将直接密码写入PHP代码。PHP或GMail SMTP服务器是否提供了避免使用直接密码的方法?可能类似于API身份验证吗?我从来没有尝试过在像谷歌这样的SMTP服务器上发送电子邮件,因为所有这些服务都需要身份验证。Github中有一些很棒的项目,例如,这允许使用安全端口、身份验证、SSL等通过SMTP发送电子邮件。。(请记住,php文件是服务器端的,因此用户无法访问此信息。因此,您的通行证是安全的。

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

我从来没有尝试过在像谷歌这样的SMTP服务器上发送电子邮件,因为所有这些服务都需要身份验证。Github中有一些很棒的项目,例如,这允许使用安全端口、身份验证、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';

此功能是发送电子邮件的SMTP方式,但身份验证始终是必需的,但在这种情况下,它是通过服务器进行的,因此必须将所有邮件头设置为能够发送正确的电子邮件


注意:有些服务器提供商不使用此函数大声发送电子邮件,因此最终需要创建或使用php SMTP电子邮件类来处理电子邮件,并且出于身份验证的原因,此文件必须具有密码

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

似乎没有比伪造电子邮件更好的方法了!您可以为这个问题提出任何可能的解决方法。这一长段确实让人对这个问题有所了解,但确实需要进一步澄清。
<?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);
?>