How to send and receive emails in PHP. Tutorial with example code.

Why emails?

In many modern PHP applications you will at some point need to send (or receive) email. This could be for a newsletter, transactional emails, user sign-up and more. There are lots of ways to send emails in PHP. Here are 3 ways with examples:

1) Sending emails with SMTP

If you have access to a MailServer you can configure an SMTP instance. Then you can use the inbuilt PHP function. Note this requires considerable setup on the server side.

2) Email APIs

Most people don't want to mess around maintaining a mail server. For them an easier way to use php mail is with an email API. Email APIs let you send and receive emails in PHP code. They let you create inboxes and addresses dyanmically too.

With the PHP MailSlurp library you can send and receive emails in PHP. Here is an example:

For more information see the MailSlurp documentation. It's free to sign-up!

3) Serverless MailServers

A final way to send emails in PHP is by using serverless email forms. These are HTML forms that you can submit directly to MailSlurp. You'll receive the results as emails. No sign-up is required.

When submitted the form will be sent directly to the email you provided - no PHP code required!

Conclusion

Those are three easy ways to use emails in PHP: function, MailSlurp library, or serverless forms.

Have fun!