PHP邮件功能:我必须停用防火墙吗?

时间:2019-04-18 13:13:01

标签: php xampp smtp phpmailer sendmail

我正在使用xampp sendmail在本地主机中使用php创建邮件发件人。

我按原样配置了sendmail.ini和php.ini文件,但在errol.log文件中仍然出现此错误:

  

19/04/18 13:51:58:套接字错误#10013访问被拒绝。

搜索时,我发现我必须停用防火墙,但是我正在使用受控防火墙进行管理。

还有其他解决方案吗?

我在sendmail.ini上进行的配置:

smtp_server= smtp.gmail.com
smtp_ssl=tls
auth_username= (my email)
auth_password= (my email's password)
force_sender= (my email)
hostname= localhost

我在php.ini上进行的配置:

;SMTP = localhost
sendmail_path = C:\xampp\sendmail\sendmail.exe

2 个答案:

答案 0 :(得分:0)

在PHPMailer中避免使用mail()(默认)和sendmail(带有isSendmail())传输;请改用SMTP到localhost,因为它既快捷又安全。这也意味着您可以忽略php.ini中的所有邮件和sendmail配置。试试这个:

$mail->isSMTP();
$mail->Host = 'localhost';

如果您没有可用的本地邮件服务器(这也将说明您为什么无法使用mail()),请更改Host属性,使其指向托管服务提供商的邮件服务器-他们应该能够提供有关要使用的设置的文档。

答案 1 :(得分:0)

https://github.com/Synchro/PHPMailer的示例:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // 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');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $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';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

启用opensslphp.ini-;extension=php_openssl.dll