Mailtrap.io 为开发者提供了一个假的电子邮件服务器。使用它在开发阶段发送报告或电子邮件到实际地址,但在邮件陷阱中拦截它们。io(它们永远不会被发送到最终的收件人),并立即看到你的HTML邮件是如何呈现的,或者一个新的CTO按钮是如何脱颖而出的。
通过集成sSMTP和Mailtrap,您的Ubuntu开发VPS可以使用相同的邮件发送服务器进行一般的邮件发送,比如cron作业完成报告。
通过这种方式,您可以使用真实的电子邮件地址来测试您的应用程序,并了解它在客户端是如何运行的,所有这些都不会淹没您的个人或同事的邮件帐户。
先决条件
Vultr VPS与Ubuntu 16.04安装和更新。
Mailtrap。io帐户。
非根用户sudo。
您选择的文本编辑器,我们将使用nano。
备份或当前安装的快照。
安装过程
以常规用户的身份登录到VPS中。通过发布以下内容,让自己成为管理员。
sudo su
然后用你的密码确认。为服务器端安装sSMTP包,以mutt作为邮件客户端进行测试。
apt install ssmtp mutt
配置
现在你必须让它与你的邮箱帐户一起工作。
您将需要您的凭证和服务器名。在我们的例子中,服务器是smtp.mailtrap。io端口2525。
我们正在编辑两个文件。首先,当仍然以root身份登录时,编辑配置文件。
nano /etc/ssmtp/ssmtp.conf
默认情况下,它看起来像下面的文本。
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster
# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=mail
# Where will the mail seem to come from?
#rewriteDomain=
# The full hostname
hostname=example.com
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
#FromLineOverride=YES
此时,您可以编辑条目以匹配您自己的值,或者只是用您的特定信息替换整个内容。大写的条目包含您的实际数据。USERNAME@EXAMPLE。COM是您希望发送电子邮件的邮件。MAILTRAP_USERNAME和MAILTRAP_PASSWORD在Mailtrap.io中为您生成。两者都是很长的随机字符串。在SMTP设置下的Mailtrap收件箱中查找。
root=USERNAME@EXAMPLE.COM
mailhub=smtp.mailtrap.io:2525
AuthUser=MAILTRAP_USERNAME
AuthPass=MAILRTRAP_PASSWORD
FromLineOverride=Yes
保存文件。用nano按下“Ctrl-X”并与“Y”确认。
需要编辑的第二个文件是将本地用户帐户映射到外部电子邮件地址,反向混叠。把文件。
nano /etc/ssmtp/revaliases
将您的条目添加到最后,这样它看起来就像这样的文本。
# sSMTP aliases
#
# Format: local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.
root:USERNAME@EXAMPLE.COM:smtp.mailtrap.io:2525
username:USERNAME@EXAMPLE.COM:smtp.mailtrap.io:2525
从本质上说,系统应该将本地帐户根的所有电子邮件作为电子邮件用户USERNAME@EXAMPLE发送。通过mailtrap服务器。输入您需要的任意数量的用户,每个用户都在自己的行中。如果您只需要根来发送消息,那么就省略其他行。您可以稍后返回,并根据需要添加用户。
保存文件。用nano按下“Ctrl-X”并与“Y”确认。
你就完成了。你可能想通过给自己发一封电子邮件来了解它。为此,创建您的消息并发送它。
echo "This is the body" | mutt -s "Hello World" SEND_TO_USERNAME@EXAMPLE.COM
登录您的邮箱帐户并检查您的演示收件箱。邮件应该已经在那里等候了。
结论
最后,将终端会话从根节点切换到普通用户。
exit
可选地,将MAILTO=SEND_TO_USERNAME@EXAMPLE.COM
添加到您的crontab中。要做一个快速测试,比方说,每分钟检查一下/home文件夹中的子文件夹,编辑crontab。
crontab -e
如果需要,选择您的编辑器,并在结尾添加/编辑这些行,这样它就类似于下面的文本。
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
mailto=SEND_TO_USERNAME@EXAMPLE.COM
* * * * * ls /home
这将每分钟运行ls /home,因为有一个MAILTO条目添加了,发送响应到该地址。您可以在这里了解更多关于crontab表的信息。
保存退出。