带XAMPP的SMTP服务器? [英] SMTP server with XAMPP?

查看:104
本文介绍了带XAMPP的SMTP服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢php,在我的项目中我使用了php邮件功能,但是从数据库发送邮件时,会显示如下错误:

 无法在localhost端口25连接到mailserver,验证您的SMTP

通过搜索stackoverflow和google,我来了解,XAMPP不提供SMTP服务器,我将不得不安装一个SMTP服务器。



我真的很困惑。 ,我应该安装哪个SMTP服务器?

解决方案

对于这个例子,我将使用 PHPMailer



首先,您必须下载PHPMailer的源代码。只需要3个文件:





将这3个文件放在同一个文件夹。
然后创建主脚本(我称之为'index.php')。



index.php的内容:

 <?php 

//加载PHPMailer依赖项
require_once'PHPMailerAutoload.php';
require_once'class.phpmailer.php';
require_once'class.smtp.php';

/ * CONFIGURATION * /
$ crendentials = array(
'email'=>'XXXXXXXX@gmail.com',//您的GMail地址
'密码'=>'XXXXXXXX'//您的GMail密码
);

/ *特殊邮件SMTP * /
$ smtp = array(

'host'=>'smtp.gmail.com',
'port'=> 587,
'username'=> $ crendentials ['email'],
'password'=> $ crendentials ['password'],
' secure'=>'tls'// SSL或TLS

);

/ * TO,SUBJECT,CONTENT * /
$ to =''; //To字段
$ subject ='这是使用PHPMailer发送的测试电子邮件;
$ content ='这是黑体中的HTML消息正文< b>< / b>';



$ mailer = new PHPMailer();

// SMTP配置
$ mailer-> isSMTP();
$ mailer-> SMTPAuth = true; //我们需要验证
$ mailer-> Host = $ smtp ['host'];
$ mailer-> Port = $ smtp ['port'];
$ mailer-> Username = $ smtp ['username'];
$ mailer-> Password = $ smtp ['password'];
$ mailer-> SMTPSecure = $ smtp ['secure'];

//现在发送邮件:
//从 - 到:
$ mailer-> From = $ crendentials ['email'];
$ mailer-> FromName ='您的姓名'; //可选
$ mailer-> addAddress($ to); //添加收件人

//主题 - 正文:
$ mailer-> Subject = $ subject;
$ mailer-> Body = $ content;
$ mailer-> isHTML(true); //邮件正文包含HTML标签

//检查邮件是否发送:
如果(!$ mailer-> send()){
echo'发送邮件时出错: '。 $ mailer-> ERRORINFO;
} else {
echo'Message sent!';
}

您还可以添加CC,BCC字段等...



示例和文档可以在 Github 找到。



如果您需要使用其他SMTP服务器,您可以修改 $ smtp 中的值。



注意:您可能在发送邮件时遇到问题,如Warning:stream_socket_enable_crypto():此流不支持SSL / crypto。



在这种情况下,您必须启用OpenSSL扩展。检查您的 phpinfo(),查找加载配置文件的值(在我的情况下:E:\Program Files\wamp\bin\apache\\ \\apache2.4.2\bin\php.ini),在此文件中,取消注释行 extension = php_openssl.dll 。然后,重新启动您的XAMPP服务器。


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"

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.

I am really confused.So, Which SMTP server I should install?

解决方案

For this example, I will use PHPMailer.

So first, you have to download the source code of PHPMailer. Only 3 files are necessary :

Put these 3 files in the same folder. Then create the main script (I called it '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...

Examples and documentation can be found on Github.

If you need to use another SMTP server, you can modify the values in $smtp.

Note : you may have a problem sending the mail, like 'Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto'.

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 Files\wamp\bin\apache\apache2.4.2\bin\php.ini) and in this file, uncomment the line extension=php_openssl.dll. Then, restart your XAMPP server.

这篇关于带XAMPP的SMTP服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆