要开启PHP SMTP功能,需要按照以下步骤进行操作:
-
确认服务器支持SMTP功能,查看php.ini中的SMTP设置。
-
在php.ini文件中找到[mail function]区域,修改以下几个参数:
SMTP = smtp.example.com
smtp_port = 25
sendmail_from = me@example.com
其中,SMTP参数指向你的SMTP服务器,smtp_port参数设置SMTP端口号(默认是25),sendmail_from参数设置发送邮件的发件人地址。
- 在php.ini文件末尾添加以下设置:
[mail function]
sendmail_path = "C:xamppsendmailsendmail.exe -t -i"
这里的sendmail_path参数需要指向sendmail.exe文件的路径。
- 在PHP代码中调用mail()函数,发送邮件。
示例代码:
$to = "recipient@example.com";
$subject = "Test email";
$message = "This is a test email.";
$headers = "From: me@example.com" . "
" .
"Reply-To: me@example.com" . "
" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
- 测试邮件发送功能,查看是否收到邮件。