📜  mailhog 的邮件发送设置磁电机 (1)

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

Mailhog 的邮件发送设置

如果你正在开发一个应用程序,并需要测试邮件发送功能,那么 Mailhog 可能是一个好的选择。Mailhog 是一个专门用于开发和测试邮件发送的工具。本文将讲解如何在 Mailhog 中设置邮件发送。

安装 Mailhog

首先,你需要安装 Mailhog。你可以从 Mailhog 的官网(https://github.com/mailhog/MailHog)下载最新版本的 Mailhog。

你可以使用如下的命令来下载和安装 Mailhog:

wget https://github.com/mailhog/MailHog/releases/download/v1.0.0/MailHog_linux_amd64
chmod +x MailHog_linux_amd64
sudo mv MailHog_linux_amd64 /usr/local/bin/mailhog

完成安装后,你可以使用以下命令启动 Mailhog:

mailhog

这将启动一个 Mailhog 服务器,并在默认端口8080上提供Web UI。

配置 Mailhog

接下来,你需要配置 Mailhog,以便邮件发送时将邮件发送到 Mailhog 服务器。

对于 Python 应用程序,你可以使用以下的配置代码:

import smtplib

# MailHog SMTP server address and port
SMTP_SERVER = 'localhost'
SMTP_PORT = 1025

# SendGrid credentials
SENDGRID_USERNAME = ''
SENDGRID_PASSWORD = ''

# Set up a connection to the SMTP server
mailserver = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)

# Send the email
from_addr = 'sender@example.com'
to_addr = 'recipient@example.com'
msg = 'Hello, world!'
mailserver.sendmail(from_addr, to_addr, msg)

# Clean up the connection to the SMTP server
mailserver.quit()

这里的 SMTP_SERVER 是 Mailhog 的地址,端口则为1025。这里假设你的应用程序中使用的是 SendGrid 凭据。实际上,你需要使用你自己的凭据。

查看发送的邮件

当你的应用程序将邮件发送到 Mailhog 服务器时,你可以登录到 Mailhog 的 web UI(默认地址为 http://localhost:8025)来查看发送的邮件。

可以看到,Mailhog 的 web UI 显示了邮件的主题,发件人、收件人等邮件的详细信息。你还可以在这里查看邮件的原始代码。

结论

Mailhog 是一个非常有用的工具,可以帮助你开发和测试邮件发送功能。通过本文的介绍,你应该已经知道了如何在 Mailhog 中设置邮件发送。现在就开始使用 Mailhog 吧!