How to check mail () using PHPUnit

Is there a way to test PHPUnit (or perhaps a different testing framework for PHP) if the mail was sent correctly? I have to check the code that uses the PHP function mail () function. With a custom postal class, I can always do the layout, but for mail () ...? Maybe there is a plugin that can use IMAP and check if mail is received? (and this should be an OS agnostic, if possible ...)

+6
php email unit-testing testing phpunit
source share
3 answers

The solution here would be to wrap the mail in a class that could be mocked and used instead.

I do not see the point in testing mail() , I am sure that it has been fully tested already.

+6
source share

There is a project called MailCatcher that can help you notify you that your letter was sent (a) and (b) built for the purpose (it contains the content that you put into it). Please note that this program does not check that your e-mail is receivable (i.e. it is not marked as spam or rejected by the mail server for other reasons).

Mailcatcher is a local SMTP service and web interface that will help you check emails sent by your code. In addition, emails can be programmatically checked using the API: for example: / messages, / messages /: id.json, / messages /: id.html, etc. To use the API, you will need something like Guzzle to make HTTP calls. Project Page http://mailcatcher.me/

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

0
source share

Is there a way to check with PHPUnit (or perhaps another testing framework for PHP) if the mail was sent correctly?

If you want to check if mail was sent successfully, you do not need phpunit, you just do:

 mail(.....) or die('Could not send the email !!'); 

or

 if (!mail(......)){ echo 'Could not send the email !!'; } 

Note that this tells you whether the mail was sent, whether it was received, to whom the email was sent. Therefore, the best term here should be delivered .

-4
source share

All Articles