开发者

how to test mail() using PHPUnit

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 plugi开发者_Python百科n which is capable to use IMAP and verify if mail is received? (and it should be OS-agnostic if it is possible...)


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.


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 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/

A nice tutorial with links to example code: http://codeception.com/12-15-2013/testing-emails-in-php.html


is there a way to test with PHPUnit (or maybe other testing framework for PHP) if mail is sent correctly?

If you want to check whether or not mail was sent successfully, you don't need phpunit, you simply do:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜