在Php中正确配置SMTP Gmail

问题描述 投票:0回答:1

我需要连接SMTP,以便我可以从php发送电子邮件。我有的是这个:

$smtp = Mail::factory('smtp', array(
    'host' => 'ssl://smtp.gmail.com',
    'SMTPSecure' => 'tls',
    'port' => '587',
    'auth' => true,
    'username' => '[email protected]',
    'password' => 'xxxx'
));
$subject = 'Nuevo correo';


$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= 'From: domain <[email protected]>'. "\r\n" .

我有自己的域设置与Gmail。需要修复或错误的是什么?提前致谢。

php smtp gmail
1个回答
0
投票

您需要在此link中包含邮件库,而不是使用TLS如果您的域名在G-Suite帐户中正确配置,则此代码不会出现任何问题。我已对其进行了测试,并且工作正常!

// Pear Mail Library
require_once "Mail.php";

$from = '<[email protected]>';
$to = '<[email protected]>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => '[email protected]',
        'password' => 'passwordxxx'
    ));

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

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}
© www.soinside.com 2019 - 2024. All rights reserved.