使用MailCatcher进行电子邮件调试

本文介绍了如何使用MailCatcher避免在测试应用程序时意外发送电子邮件给客户,从而导致混淆或不满。MailCatcher是一个简单的SMTP服务器,它可以捕获并显示在Web界面中的测试邮件。文章提供了设置MailCatcher的步骤,包括使用预配置的Vagrant box或手动安装,并通过PHP脚本展示了发送邮件的过程。使用MailCatcher可以确保在不离开开发环境的情况下进行有效的邮件测试。
摘要由CSDN通过智能技术生成

You use email in your applications, right? Ok, that’s a rhetorical question. Of course you do. Despite email being over 30 years old, it’s still the most popular application on the planet, by far. Here are some stats from Pingdom, for 2012:

您在应用程序中使用电子邮件,对吗? 好的,这是一个反问。 当然可以 尽管电子邮件已有30多年的历史了,但到目前为止,它仍然是地球上最受欢迎的应用程序。 以下是Pingdom在2012年的一些统计数据

  • 2.2 billion – Number of email users worldwide

    22亿 –全球电子邮件用户数量

  • 144 billion – Total email traffic per day worldwide

    1,440亿美元 –全球每天的电子邮件总流量

  • 4.3 billion – Number of email clients worldwide in 2012

    43亿 – 2012年全球电子邮件客户数量

Staggering!

agger!

但是,为什么还要在电子邮件上发表另一篇文章呢? (But why another article on email?)

For one simple reason, one which we’ve likely all been caught by at one point or another. Because we need to test, as close to production as we can, but without emailing our clients accidentally, and them consequently becoming confused or frustrated, or embarrassed that their clients have received test emails.

出于一个简单的原因,我们很可能都在某一点或另一点被抓住了。 因为我们需要尽可能地接近生产进行测试,但又不会意外地向客户发送电子邮件,因此他们会感到困惑或沮丧,或者为他们的客户收到测试电子邮件感到尴尬。

I’m sure you know what I mean. You think you’ve set your application in some kind of debug mode. Given that, you start your test, which sends a load of emails from your application. All the while, you feel comfortable in the knowledge that no one but you is ever going to see them.

我确定你知道我的意思。 您认为您已将应用程序设置为某种调试模式。 鉴于此,您开始测试,该测试从您的应用程序发送大量电子邮件。 一直以来,您会感到无所适从,因为除了一个人,您永远都不会看到它们。

Your tests pass, you congratulate yourself, and move on. A short time later, you receive a rather, let’s just say terse, Skype call from your client. She’s rather annoyed as her customers have been ringing up, asking why they’ve been receiving odd emails from her company. She’s not happy and wants answers?

您的测试通过了,您祝贺自己,然后继续前进。 不久之后,您收到来自客户的相当简短的 Skype电话。 由于客户一直在打电话,她感到很生气,问他们为什么收到来自她公司的奇怪电子邮件。 她不快乐,想要答案吗?

Been there? Don’t want to be there again? I’m guessing you don’t. Here’s the solution – MailCatcher. If you’re not familiar with it, MailCatcher

到过那里? 不想再在那里? 我猜你没有。 这是解决方案– MailCatcher 。 如果您不熟悉,可以使用MailCatcher

…runs a super simple SMTP server which catches any message sent to it to display in a web interface. Run mailcatcher, set your favourite app to deliver to smtp://127.0.0.1:1025 instead of your default SMTP server, then check out http://127.0.0.1:1080 to see the mail that’s arrived so far.

…运行一个超级简单的SMTP服务器,该服务器捕获发送给它的任何邮件以在Web界面中显示。 运行mailcatcher,将您喜欢的应用程序设置为传递到smtp://127.0.0.1:1025,而不是默认的SMTP服务器,然后查看http://127.0.0.1:1080,以查看到目前为止已收到的邮件。

Sound like a good solution? No matter if you’re tired, under pressure, new on the team, or just need to run tests – MailCatcher will ensure that no email ever gets sent outside of your network, or even outside of your development virtual machine.

听起来是个不错的解决方案? 无论您是疲倦,承受压力,团队中的新手,还是只需要运行测试,MailCatcher都能确保您不会在网络之外甚至在开发虚拟机之外发送电子邮件。

In this post, I’m going to show you how to set it up and run through the interface when emails have been captured by it.

在本文中,我将向您展示如何设置它并在捕获电子邮件后通过界面运行它。

预先准备的虚拟机 (A Pre-Prepared Virtual Machine)

To save you a lot of trouble of getting MailCatcher set up, I’ve created a custom Vagrant box which does it all for you. Make sure you have VirtualBox and Vagrant installed, then clone a copy of the article’s repository using the following command:

为了省去设置MailCatcher的麻烦,我创建了一个自定义的Vagrant框,可以为您完成所有操作。 确保已安装VirtualBox和Vagrant,然后使用以下命令克隆文章存储库的副本:

git clone git@github.com:sitepoint-examples/mailcatcher-article.git

Then, in the cloned project directory, run the following command:

然后,在克隆的项目目录中,运行以下命令:

vagrant up

This will launch the virtual machine provisioning process, displaying quite a lot of output as the provisioning process runs. The configured virtual machine is quite minimalist. It has MailCatcher, Sendmail and Nginx installed.

这将启动虚拟机置备过程,在置备过程运行时显示大量输出。 配置的虚拟机非常简单。 它具有MailCatcherSendmailNginx安装。

Nginx has a simple VHost setup, which maps the project directory to /var/www/ on the virtual machine. If you’d like to know exactly what happens during provisioning, checkout provision.sh.

Nginx有一个简单的VHost设置,它将项目目录映射到虚拟机上的/var/www/ 。 如果您想确切了解配置过程中发生的情况,请签出provision.sh

手动安装MailCatcher (Manually Installing MailCatcher)

If you want to install MailCatcher yourself, and you have a virtual machine (or a Linux machine available), here are the steps to run:

如果要自己安装MailCatcher,并且有虚拟机(或可用的Linux机器),请执行以下步骤:

sudo apt-get install -y vim curl python-software-properties lynx nginx
sudo apt-get install -y php5-fpm php5-memcache memcached php-apc
sudo apt-get install -y build-essential libsqlite3-dev ruby1.9.3
sudo gem install mailcatcher
sudo mailcatcher --http-ip 0.0.0.0

You may or may not have to run the third command. I did as I’m using a very minimalist Ubuntu Precise 64 Vagrant image, which needs the packages to build MailCatcher. Please be aware that if you’re on a different Linux distribution or version, the individual package names may be different.

您可能必须运行第三条命令,也可以不必运行。 我这样做的原因是我使用的是极简的Ubuntu Precise 64 Vagrant映像,该映像需要软件包来构建MailCatcher。 请注意,如果您使用的是其他Linux发行版或版本,则各个程序包名称可能会有所不同。

Note: if you follow the standard MailCatcher startup process, it will only listen on IP 127.0.0.1, and port 1025. With this setup we couldn’t see it from the host machine. So I’ve added a public IP on the virtual machine, 192.168.56.111 and changed MailCatcher’s configuration so that it listens on all IPs.

注意:如果遵循标准MailCatcher启动过程,它将仅侦听IP 127.0.0.1和端口1025 。 通过此设置,我们无法在主机上看到它。 因此,我在虚拟机192.168.56.111上添加了一个公用IP,并更改了MailCatcher的配置,以便它可以侦听所有IP。

MailCatcher Web UI (The MailCatcher Web UI)

Now you’ll be able to see the MailCatcher web UI at http://192.168.56.111:1080. It will look like the image below:

现在,您可以在http://192.168.56.111:1080上看到MailCatcher Web UI。 它将如下图所示:

MailCatcher UI

It’s a pretty simple interface, listing the emails in the MailCatcher queue, when available, at the top. When there are emails in the list, the bottom pane does a good job of showing you information about them, which we’ll see later.

这是一个非常简单的界面,顶部列出了MailCatcher队列中的电子邮件(如果有)。 当列表中有电子邮件时,底部窗格会很好地向您显示有关电子邮件的信息,我们将在以后看到。

样例代码 (Sample Code)

For this article, I’ve created a simple PHP script, index.php, available in the project repository, and accessible at http://192.168.56.111. It uses SwiftMailer to connect to MailCatcher and send an email on load. You can see in the code below a fairly standard HTML page.

对于本文,我创建了一个简单PHP脚本index.php ,该脚本可在项目存储库中找到,并可以从http://192.168.56.111访问。 它使用SwiftMailer连接到MailCatcher并在加载时发送电子邮件。 您可以在下面的代码中看到一个相当标准HTML页面。

<?php require_once('mail-loader.php'); ?>
<html>
<head>
    <title>Simple MailCatcher PHP Example</title>
</head>
<body>
    <h1>Simple MailCatcher PHP Example</h1>
    <p>This application sends a number of emails which will be caught by MailCatcher. To check them, view them in <a href="http://192.168.56.111:1080/" target="_blank">the local MailCatcher installation</a></p>
</body>
</html>

It includes mail-loader.php, which is available below.

它包括mail-loader.php ,可从下面获得。

<?php
require('vendor/autoload.php');
$email = 'matthew@example.com';
$subject = 'testing';
$message = 'test message';

$transport = Swift_SmtpTransport::newInstance(
    "localhost", 1025
);

$message = Swift_Message::newInstance();
$message->setTo(array(
    "matthew@maltblue.com" => "Matthew Setter",
));
$message->setSubject(
    "This email is sent using Swift Mailer"
);
$message->setBody("You're our best client ever.");
$message->setFrom("matthew@localhost", "Your bank");

$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message, $failedRecipients);
print_r($failedRecipients);

If you’re not familiar with SwiftMailer Aurelio’s article here on SitePoint provides an excellent introduction. Incidentally, it’s his code I’ve used for this example. Thanks Aurelio.

如果您对SwiftMailer Aurelio的文章不熟悉,可以在SitePoint上获得很好的介绍。 顺便说一句,这是我在此示例中使用的他的代码。 谢谢奥雷利奥。

Basically, there’s only one line that’s important for us to note, which is the following:

基本上,只有一行需要我们注意,以下是:

$transport = Swift_SmtpTransport::newInstance(
    "localhost", 1025
);

This creates a connection to the MailCatcher server which we’ve just set up. That’s it, nothing fancy! I’ve now reloaded the page three times, which has sent three emails. You can see that they’re listed in MailCatcher in the image below.

这将创建到我们刚刚设置的MailCatcher服务器的连接。 就是这样,没什么花哨的! 我现在已经将页面重新加载了三次,已经发送了三封电子邮件。 您可以在下图中看到MailCatcher中列出了它们。

MailCatcher UI Showing Mail Content

I’ve clicked on the first entry, which shows the received, from, to and subject details, as well as the body of the email. If we switch to the Source tab, as in the image below, we can see the raw email details:

我单击了第一个条目,该条目显示了收到的,从,到和主题的详细信息,以及电子邮件的正文。 如果切换到“源”选项卡,如下图所示,我们可以看到原始电子邮件详细信息:

MailCatcher UI Showing Raw Mail Content

Via the third tab, we can use Fractal to analyse the content of our email. I’ll skip that today as it’s beyond the scope of this article.

通过第三个选项卡,我们可以使用Fractal分析电子邮件的内容。 我今天将跳过它,因为它已超出了本文的范围。

我们都准备好了 (We’re All Set)

With that done, we can now create emails as we otherwise would, setting recipients, subjects, attachments and so on, like I have above, and know that they will never actually go to the real addresses.

完成此操作后,我们现在可以像以前那样创建电子邮件,设置收件人,主题,附件等,就像我上面提到的那样,并且知道它们永远不会真正到达真实地址。

We can write tests to verify that the code works as it should. We can check the mail sending workflow, check message contents, recipients, headers and so forth. It’s as practical as it gets, without actually sending anything outside of our environment.

我们可以编写测试来验证代码是否可以正常工作。 我们可以检查邮件发送工作流程,检查邮件内容,收件人,标头等。 它尽可能地实用,而无需在环境之外实际发送任何东西。

Now we can rest assured that our clients (and their clients) will never receive any emails that were never intended for them. What a relief.

现在,我们可以放心,我们的客户(和他们的客户)将永远不会收到任何不适合他们的电子邮件。 终于解脱了。

结语 (Wrapping Up)

Now, this is a rather trivial example, only using a simple SwiftMailer code snippet for the example. I’m sure that your codebases are much more complex and sophisticated than this example.

现在,这是一个相当琐碎的示例,仅使用简单的SwiftMailer代码片段作为示例。 我确信您的代码库比本示例要复杂得多。

But I’m sure that in your applications, you’ll have debug and test configurations available, where you can set the host and port accordingly, differentiating it from live. By doing so, no other code will ever need to change, and you can reap the benefits of the peace of mind MailCatcher brings.

但是我敢肯定,在您的应用程序中,您将拥有可用的调试和测试配置,您可以在其中相应地设置主机和端口,从而将其与实时版本区分开。 这样,就无需更改其他代码,您就可以从MailCatcher带来的安心中获得收益。

Do you already use MailCatcher? What’s your experience of integrating it? Were there any issues which caught you unaware? Share your thoughts in the comments.

您已经使用MailCatcher吗? 您如何整合它? 有没有让您不知所措的问题? 在评论区分享你的观点。

翻译自: https://www.sitepoint.com/email-debugging-mailcatcher/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值