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

5
我卡在了尝试使用PHP mail()函数将SMTP身份验证添加到我的php脚本上。目前的脚本可以正常工作,但由于它没有使用SMTP身份验证,所以php文件的路径和许多其他敏感细节都包含在头部中(帐户用户名等)。
我目前正在使用“$headers =”,等来指定一些头部信息,但我知道我需要使用SMTP身份验证来解决这个问题。
有没有简单的方法可以使我的脚本使用SMTP身份验证而不必使用phpmailer等?我是否可以简单地指定端口、身份验证、用户名、密码?
更新:以下是一些代码:
            `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`

哪个平台?哪个MTA?mail()函数本身不执行任何授权。 - mario
嗨,Mario,使用EXIM的CENTOS 6.7。 - Beccas
1个回答

10

为什么不尝试一下梨邮件界面,就像这样:

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);
}

更多信息请参见https://goo.gl/HjffYA

编辑:

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

定义SMTP服务器

smtp_server=mail.mydomain.com

如果您需要更改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

最后,这是您用于 SMTP 服务器的身份验证凭据:

auth_username=username
auth_password=password

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


1
你好Enzo,感谢你的帮助。是否有可能在不依赖于PEAR的情况下,使用SMTP身份验证与mail()函数一起使用,以便我不必重写所有的php代码? - Beccas
嗨Enzo,感谢你的帮助。这可能是一个愚蠢的问题,但我需要将它添加到php文件中还是需要编辑php.ini和sendmail.ini文件? - Beccas
1
你没有提供更多关于修改这些配置的解释。 - Renan Coelho
Mail.php里面有什么内容? - Laurence Cope

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接