带有 XAMPP 的 SMTP 服务器? [英] SMTP server with XAMPP?
问题描述
我是 php 的新手,在我的项目中我使用了 php 邮件功能,但是从数据库发送邮件时它显示如下错误:
I am new to php and in my project I have used php mail function, but while sending mail from database it shows an error like:
Failed to connect to mailserver at "localhost" port 25, verify your "SMTP"
通过在stackoverflow和google上搜索,我知道XAMPP不提供SMTP服务器,我必须安装一个SMTP服务器.
By searching on stackoverflow and google, I come to know that XAMPP does not provide SMTP server and I will have to install a SMTP server.
我真的很困惑.那么,我应该安装哪个 SMTP 服务器?
I am really confused.So, Which SMTP server I should install?
推荐答案
对于这个例子,我将使用 PHPMailer一>.
For this example, I will use PHPMailer.
所以首先,您必须下载 PHPMailer 的源代码.只需要 3 个文件:
So first, you have to download the source code of PHPMailer. Only 3 files are necessary :
将这三个文件放在同一个文件夹中.然后创建主脚本(我称之为'index.php').
Put these 3 files in the same folder. Then create the main script (I called it 'index.php').
index.php 的内容:
Content of index.php :
<?php
//Load PHPMailer dependencies
require_once 'PHPMailerAutoload.php';
require_once 'class.phpmailer.php';
require_once 'class.smtp.php';
/* CONFIGURATION */
$crendentials = array(
'email' => 'XXXXXXXX@gmail.com', //Your GMail adress
'password' => 'XXXXXXXX' //Your GMail password
);
/* SPECIFIC TO GMAIL SMTP */
$smtp = array(
'host' => 'smtp.gmail.com',
'port' => 587,
'username' => $crendentials['email'],
'password' => $crendentials['password'],
'secure' => 'tls' //SSL or TLS
);
/* TO, SUBJECT, CONTENT */
$to = ''; //The 'To' field
$subject = 'This is a test email sent with PHPMailer';
$content = 'This is the HTML message body <b>in bold!</b>';
$mailer = new PHPMailer();
//SMTP Configuration
$mailer->isSMTP();
$mailer->SMTPAuth = true; //We need to authenticate
$mailer->Host = $smtp['host'];
$mailer->Port = $smtp['port'];
$mailer->Username = $smtp['username'];
$mailer->Password = $smtp['password'];
$mailer->SMTPSecure = $smtp['secure'];
//Now, send mail :
//From - To :
$mailer->From = $crendentials['email'];
$mailer->FromName = 'Your Name'; //Optional
$mailer->addAddress($to); // Add a recipient
//Subject - Body :
$mailer->Subject = $subject;
$mailer->Body = $content;
$mailer->isHTML(true); //Mail body contains HTML tags
//Check if mail is sent :
if(!$mailer->send()) {
echo 'Error sending mail : ' . $mailer->ErrorInfo;
} else {
echo 'Message sent !';
}
您还可以添加抄送"、密件抄送"字段等...
You can also add 'CC', 'BCC' fields etc...
可以在 Github 上找到示例和文档.
Examples and documentation can be found on Github.
如果需要使用其他SMTP服务器,可以修改$smtp
中的值.
If you need to use another SMTP server, you can modify the values in $smtp
.
注意:您可能在发送邮件时遇到问题,例如警告:stream_socket_enable_crypto():此流不支持 SSL/加密".
Note : you may have a problem sending the mail, like 'Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto'.
在这种情况下,您必须启用 OpenSSL 扩展.检查您的 phpinfo()
,查找加载的配置文件"的值(在我的情况下:E:Program Fileswampinapacheapache2.4.2inphp.ini)在此文件中,取消注释 extension=php_openssl.dll
行.然后,重新启动您的 XAMPP 服务器.
In such case, you must enable the OpenSSL extension. Check your phpinfo()
, look for the value of 'Loaded Configuration File' (in my case : E:Program Fileswampinapacheapache2.4.2inphp.ini) and in this file, uncomment the line extension=php_openssl.dll
. Then, restart your XAMPP server.
这篇关于带有 XAMPP 的 SMTP 服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!