使用 PHP mail() 函数进行 SMTP 身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32413576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:57:29  来源:igfitidea点击:

SMTP Authentication with PHP mail() function

phpemail

提问by Beccas

I'm stuck on trying to add SMTP Authentication to my php script using the PHP mail() function.

我一直在尝试使用 PHP mail() 函数将 SMTP 身份验证添加到我的 php 脚本中。

The script currently works but because it isn't using SMTP Authentication the path of the php file and many other sensitive details are being included in the header (account username, etc.).

该脚本目前可以工作,但由于它没有使用 SMTP 身份验证,所以 php 文件的路径和许多其他敏感细节都包含在标头中(帐户用户名等)。

I'm currently specifying some of the header information using "$headers = ", etc., but I understand I need to use SMTP Authentication to fix this.

我目前正在使用“$headers =”等指定一些标头信息,但我知道我需要使用 SMTP 身份验证来解决这个问题。

Is there a simple way to make my script use SMTP Authentication without having to use phpmailer, etc? Can I simply specify the port, authentication, username, password?

有没有一种简单的方法可以让我的脚本使用 SMTP 身份验证而不必使用 phpmailer 等?我可以简单地指定端口、身份验证、用户名、密码吗?

Update: Here is come code:

更新:这里是代码:

            `code`$eol = PHP_EOL;
            $headers =  "From: Test <[email protected]>".$eol;
            $headers .= "Reply-To: [email protected]".$eol;
            $headers .= "MIME-Version: 1.0".$eol;
            $headers .= "Content-Type: multipart/mixed; boundary=\"$random_hash\"".$eol.$eol;
            $subject = 'Subject Goes Here';
            $message="--".$random_hash.$eol;
            $message.="Content-Type: text/plain; charset=UTF-8".$eol;
            $message.="Content-Transfer-Encoding: 8bit".$eol.$eol;
            $message.="Hello,".$eol;
            $message.="Body content goes here.".$eol.$eol;
            $message.="Thank you,".$eol.$eol;
            $message.="--".$random_hash.$eol;
            @mail(to, subject, message, headers);`code`

回答by Mo?ze

Why don't you try the Pear Mail interface something like this:

你为什么不试试 Pear Mail 界面是这样的:

require_once "Mail.php";
$username = '[email protected]';
$password = 'password';
$smtpHost = 'ssl://smtp.gmail.com';
$smtpPort = '465';
$to = '[email protected]';
$from =  '[email protected]';

$subject = 'Contact Form';
$successMessage = 'Message successfully sent!';


$replyTo = '';
$name = '';
$body = '';


$headers = array(
    'From' => $name . " <" . $from . ">",
    'To' => $to,
    'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
            'host' => $smtpHost,
            'port' => $smtpPort,
            'auth' => true,
            'username' => $username,
            'password' => $password
        ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo($mail->getMessage());
} else {
    echo($successMessage);
}

More information at https://goo.gl/HjffYA

更多信息请访问https://goo.gl/HjffYA

EDIT:

编辑:

The only way to this without more coding or using external library, is to update sendmail:

无需更多编码或使用外部库的唯一方法是更新 sendmail:

Define SMTP Server

定义 SMTP 服务器

smtp_server=mail.mydomain.com

If you need to change the smtp and SSL ports ; smtp port (normally 25)

如果您需要更改 smtp 和 SSL 端口;smtp 端口(通常为 25)

smtp_port=25

; SMTPS (SSL) support
;   auto = use SSL for port 465, otherwise try to use TLS
;   ssl  = alway use SSL
;   tls  = always use TLS
;   none = never try to use SSL

smtp_ssl=auto

And finally your authentication credentials for SMTP server:

最后是 SMTP 服务器的身份验证凭据:

auth_username=username
auth_password=password

Ref: http://php.net/manual/en/ref.mail.php

参考:http: //php.net/manual/en/ref.mail.php