is there a way to test with PHPUnit (or maybe other testing framework for PHP) if mail is sent correctly? I have to test a code which uses PHP function mail() . With custom mailer class i could always make a mock, but for mail() ... ? Maybe there is some plugin which is capable to use IMAP and verify if mail is received? (and it should be OS-agnostic if it is possible...)
如果邮件发送正确,是否有办法测试PHPUnit(或者PHP的其他测试框架)?我必须测试使用PHP函数mail()的代码。有了定制的mailer类,我总是可以做一个mock,但是对于mail()……吗?也许有一些插件可以使用IMAP来验证邮件是否收到?(如果可能的话,应该是os不可知的……)
3 个解决方案
#1
6
The solution here would be to wrap mail in a class that could be mocked and use that instead.
这里的解决方案是将邮件封装到可以被嘲笑的类中,然后使用它。
I don't see the point in testing mail()
itself, I'm sure it's been thoroughly tested already.
我不认为测试mail()本身有什么意义,我确信它已经经过了彻底的测试。
#2
0
There is a project called MailCatcher that can help assure you your email has (a) been sent and (b) is constructed as intended (it has the content you put into it). Note this program does not verify that your email is receivable (ie: not marked as spam or rejected by a mail server for other reasons)
有一个叫做MailCatcher的项目可以帮助您确保您的电子邮件(a)已经发送并且(b)按照预期构造(它包含您放入其中的内容)。注意此程序不验证您的电子邮件是可接收的(例如:没有标记为垃圾邮件或由于其他原因被邮件服务器拒绝)
Mailcatcher is a local SMTP service and web interface to help you verify emails sent by your code. In addition, emails can be programmatically verified by using the API: eg: /messages, /messages/:id.json, /messages/:id.html etc. To use the API you'll need something like Guzzle to make the HTTP calls. The project page is http://mailcatcher.me/
Mailcatcher是一个本地SMTP服务和web界面,用于帮助您验证代码发送的电子邮件。此外,可以通过使用API:例如:/messages/ message /:id以编程方式验证电子邮件。json /信息/:id。要使用API,您需要Guzzle之类的东西来进行HTTP调用。项目页面是http://mailcatcher.me/
A nice tutorial with links to example code: http://codeception.com/12-15-2013/testing-emails-in-php.html
一个很好的教程,其中有指向示例代码的链接:http://codeception.com/12-152013/testing-emails-inphp.html
#3
-4
is there a way to test with PHPUnit (or maybe other testing framework for PHP) if mail is sent correctly?
如果邮件发送正确,是否有办法使用PHPUnit(或者其他PHP测试框架)进行测试?
If you want to check whether or not mail was sent successfully, you don't need phpunit, you simply do:
如果要检查邮件是否发送成功,不需要phpunit,只需:
mail(.....) or die('Could not send the email !!');
Or
或
if (!mail(......)){
echo 'Could not send the email !!';
}
Note that this tells you whether or not mail was sent NOT whether or not it was received to whom the email was sent. So the better term here should be delivered.
注意,这告诉您邮件是否被发送,而不是邮件是否被发送给了发送者。所以这里更好的条件应该被提出。
#1
6
The solution here would be to wrap mail in a class that could be mocked and use that instead.
这里的解决方案是将邮件封装到可以被嘲笑的类中,然后使用它。
I don't see the point in testing mail()
itself, I'm sure it's been thoroughly tested already.
我不认为测试mail()本身有什么意义,我确信它已经经过了彻底的测试。
#2
0
There is a project called MailCatcher that can help assure you your email has (a) been sent and (b) is constructed as intended (it has the content you put into it). Note this program does not verify that your email is receivable (ie: not marked as spam or rejected by a mail server for other reasons)
有一个叫做MailCatcher的项目可以帮助您确保您的电子邮件(a)已经发送并且(b)按照预期构造(它包含您放入其中的内容)。注意此程序不验证您的电子邮件是可接收的(例如:没有标记为垃圾邮件或由于其他原因被邮件服务器拒绝)
Mailcatcher is a local SMTP service and web interface to help you verify emails sent by your code. In addition, emails can be programmatically verified by using the API: eg: /messages, /messages/:id.json, /messages/:id.html etc. To use the API you'll need something like Guzzle to make the HTTP calls. The project page is http://mailcatcher.me/
Mailcatcher是一个本地SMTP服务和web界面,用于帮助您验证代码发送的电子邮件。此外,可以通过使用API:例如:/messages/ message /:id以编程方式验证电子邮件。json /信息/:id。要使用API,您需要Guzzle之类的东西来进行HTTP调用。项目页面是http://mailcatcher.me/
A nice tutorial with links to example code: http://codeception.com/12-15-2013/testing-emails-in-php.html
一个很好的教程,其中有指向示例代码的链接:http://codeception.com/12-152013/testing-emails-inphp.html
#3
-4
is there a way to test with PHPUnit (or maybe other testing framework for PHP) if mail is sent correctly?
如果邮件发送正确,是否有办法使用PHPUnit(或者其他PHP测试框架)进行测试?
If you want to check whether or not mail was sent successfully, you don't need phpunit, you simply do:
如果要检查邮件是否发送成功,不需要phpunit,只需:
mail(.....) or die('Could not send the email !!');
Or
或
if (!mail(......)){
echo 'Could not send the email !!';
}
Note that this tells you whether or not mail was sent NOT whether or not it was received to whom the email was sent. So the better term here should be delivered.
注意,这告诉您邮件是否被发送,而不是邮件是否被发送给了发送者。所以这里更好的条件应该被提出。