📜  发送电子邮件 xampp (1)

📅  最后修改于: 2023-12-03 15:37:08.044000             🧑  作者: Mango

发送电子邮件 XAMPP

如果你正在进行本地开发,那么一个问题就是如何测试邮箱功能,以确保你在应用程序中正确地发送电子邮件。在此情况下,XAMPP是一种方便可靠的解决方案,它可以让你轻松地在你的本地计算机上测试邮件系统。

XAMPP简介

XAMPP是一个软件套装,它提供了一套完整的web开发环境,其中包括Apache web服务器、MySQL数据库、PHP以及Perl编程语言。拥有该套软件可以帮助开发人员更快地搭建web应用程序。

如何使用XAMPP来测试邮件系统

要测试XAMPP中的邮件系统,你需要使用一个基于PHP的邮件发送库。让我们来使用PHPMailer这个库,这是一个流行的PHP库,可以方便地发送电子邮件。

首先,你需要在XAMPP上配置SMTP设置。进入php.ini文件并搜索以下行:

[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = localhost
; smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
; sendmail_from = postmaster@localhost

修改上面的SMTPsmtp_port,分别将其设为你的SMTP服务器和端口。例如,如果你使用Gmail作为SMTP服务器,配置如下:

[mail function]
SMTP = smtp.gmail.com
smtp_port = 587

然后,你需要在你的项目中下载PHPMailer并包含以下代码:

// Require the PHPMailer class
require 'PHPMailerAutoload.php';

// Create a new PHPMailer instance
$mail = new PHPMailer;

// Tell PHPMailer to use SMTP
$mail->isSMTP();

// Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

// Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

// Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

// Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;

// Whether to use SMTP authentication
$mail->SMTPAuth = true;

// Username to use for SMTP authentication
$mail->Username = 'your@gmail.com';

// Password to use for SMTP authentication
$mail->Password = 'your-password';

// Set who the message is to be sent from
$mail->setFrom('from@example.com', 'John Doe');

// Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'John Doe');

// Set who the message is to be sent to
$mail->addAddress('recipient@example.com', 'Recipient Name');

// Set the subject line
$mail->Subject = 'Testing XAMPP Email Functionality';

// Read an HTML message body from an external file, convert
// referenced images to embedded,
$mail->msgHTML(file_get_contents('message.html'), __DIR__);

// Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

// Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');

// Send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

这段代码将启动一个SMTP邮件客户端并发送测试消息。如果你在浏览器中查看你的脚本,你应该看到一个“Message sent!”提示。

结论

现在你知道了如何使用XAMPP和PHPMailer测试SMTP设置,并在没有发送实际电子邮件的情况下测试电子邮件功能。现在,你可以安心地开发你的电子邮件应用程序,并将其部署到你的实际SMTP服务器上。

# 发送电子邮件 XAMPP

如果你正在进行本地开发,那么一个问题就是如何测试邮箱功能,以确保你在应用程序中正确地发送电子邮件。在此情况下,XAMPP是一种方便可靠的解决方案,它可以让你轻松地在你的本地计算机上测试邮件系统。

## XAMPP简介

[XAMPP](https://www.apachefriends.org/zh_cn/index.html)是一个软件套装,它提供了一套完整的web开发环境,其中包括Apache web服务器、MySQL数据库、PHP以及Perl编程语言。拥有该套软件可以帮助开发人员更快地搭建web应用程序。

## 如何使用XAMPP来测试邮件系统

要测试XAMPP中的邮件系统,你需要使用一个基于PHP的邮件发送库。让我们来使用[PHPMailer](https://github.com/PHPMailer/PHPMailer)这个库,这是一个流行的PHP库,可以方便地发送电子邮件。

首先,你需要在XAMPP上配置SMTP设置。进入`php.ini`文件并搜索以下行:

```ini
[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = localhost
; smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
; sendmail_from = postmaster@localhost

修改上面的SMTPsmtp_port,分别将其设为你的SMTP服务器和端口。例如,如果你使用Gmail作为SMTP服务器,配置如下:

[mail function]
SMTP = smtp.gmail.com
smtp_port = 587

然后,你需要在你的项目中下载PHPMailer并包含以下代码:

// Require the PHPMailer class
require 'PHPMailerAutoload.php';

// Create a new PHPMailer instance
$mail = new PHPMailer;

// Tell PHPMailer to use SMTP
$mail->isSMTP();

// Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

// Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

// Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

// Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;

// Whether to use SMTP authentication
$mail->SMTPAuth = true;

// Username to use for SMTP authentication
$mail->Username = 'your@gmail.com';

// Password to use for SMTP authentication
$mail->Password = 'your-password';

// Set who the message is to be sent from
$mail->setFrom('from@example.com', 'John Doe');

// Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'John Doe');

// Set who the message is to be sent to
$mail->addAddress('recipient@example.com', 'Recipient Name');

// Set the subject line
$mail->Subject = 'Testing XAMPP Email Functionality';

// Read an HTML message body from an external file, convert
// referenced images to embedded,
$mail->msgHTML(file_get_contents('message.html'), __DIR__);

// Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

// Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');

// Send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

这段代码将启动一个SMTP邮件客户端并发送测试消息。如果你在浏览器中查看你的脚本,你应该看到一个“Message sent!”提示。

结论

现在你知道了如何使用XAMPP和PHPMailer测试SMTP设置,并在没有发送实际电子邮件的情况下测试电子邮件功能。现在,你可以安心地开发你的电子邮件应用程序,并将其部署到你的实际SMTP服务器上。