Mailing with PHP

I wrote this article because I recently had the problem to send mails from PHP. Normally this can be done with only some lines of code, because you only need one function that comes with PHP

mail()

simple

With theĀ mail() function it is very easy to send mails. You only have to put in some params:

  • to (a list of recipients)
  • subject
  • message

The fourth optional parameter is to extend the functionality with header informations.

advanced

With some background information you can extend the functionality of your mail() function. If you place your header right, you can do some things like:

  • enoding (for example UTF-8)
  • HTML mails instead of pure text mails

Here you can find an example on how to send a HTML mail.

And now the difficult part. Sometimes it is very hard to send HTML mails. On the one hand there can be problems with mail clients that can not display HTML code. On the other hand there is a much bigger problem. Some servers don’t like HTML code and they do not send these mails. Other server send this mails, but they destroy the HTML code by changing backslashes and line ends. I have tested on different servers and the mail always looked a bit different or was not displayed at all.

If you are looking for a good solution, you may need a mail class for you application.

PHPMailer

TheĀ PHPMailer library is very good and safed me for many hours of work. Today I only use this library when I need to send mails. Here you can find a good tutorial.

Who else had problems with sending mails? Are there other good solutions? Are there disadvantages with PHPMailer? What do you think about sending mails from PHP?

(Visited 170 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *