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

Bec*_*cas 3 php email

我坚持尝试使用PHP mail()函数将SMTP身份验证添加到我的php脚本中.

该脚本目前有效,但因为它没有使用SMTP身份验证,所以php文件的路径和许多其他敏感信息都包含在标题中(帐户用户名等).

我目前正在使用"$ headers ="等指定一些标题信息,但我知道我需要使用SMTP身份验证来解决此问题.

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

非常感谢,

BEC

更新:这是代码:

            `code`$eol = PHP_EOL;
            $headers =  "From: Test <test@test.com>".$eol;
            $headers .= "Reply-To: test@test.com".$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`
Run Code Online (Sandbox Code Playgroud)

Moï*_*ïze 7

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

require_once "Mail.php";
$username = 'user@gmail.com';
$password = 'password';
$smtpHost = 'ssl://smtp.gmail.com';
$smtpPort = '465';
$to = 'mail@to.com';
$from =  'user@gmail.com';

$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);
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,访问https://goo.gl/HjffYA

编辑:

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

定义SMTP服务器

smtp_server=mail.mydomain.com
Run Code Online (Sandbox Code Playgroud)

如果需要更改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
Run Code Online (Sandbox Code Playgroud)

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

auth_username=username
auth_password=password
Run Code Online (Sandbox Code Playgroud)

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