Installing php mail() function

0

I have recently set up a CentOS 6.4 server using the minimalistic installation. I have a working version of PHP up and running, but I want to be able to use the mail() function in PHP.

  • What do I need to make it work (mailservers, scripts, what is needed?)
  • How do I proceed installing whats needed?

Simon Carlson

Posted 2013-08-05T22:13:20.473

Reputation: 103

You probably will need to start with installing sendmail – kobaltz – 2013-08-05T22:38:54.200

sendmail is installed, where do I go next? I am clueless. – Simon Carlson – 2013-08-06T00:00:40.433

You really shouldn't be interfacing with mail() directly. You would be better off with Zend_mail, PHPMailer, or some other library. Composing messages correctly, so you get get flagged as SPAM is not trivial. Ideally you will be using one of these libraries through a properly configured mail server, instead of just relying on poorly configured local sendmail. – Zoredache – 2013-08-06T00:49:09.863

Thanks for your input. How do I achieve this though? I'm a complete beginner to all of this so I'm very, very clueless... – Simon Carlson – 2013-08-06T01:10:38.290

If you do not have problems using it, are you receiving any kind of errors? – kobaltz – 2013-08-06T01:51:18.083

I've installed sendmail properly but I dont even know what it does or how to use it, hence I have no errors. Could you show me some kind of configuration guide? – Simon Carlson – 2013-08-06T16:39:02.900

Answers

0

Sending E-Mails is a complicated topic, because anti-spam measurements are getting more and more aggressive. I recommend to send e-mails using SMTP and a 3rd party provider. Most of the time, you already have a way of sending and receiving emails for your project (i.e. included with your domain or webhosting). I will explain the "how" and "why" below.

How to send mails using SSMTP?

Multiple choices:

  1. ssmtp.

This is a small tool that exposes a "sendmail" binary to PHP's mail() function. Install it and configure it using the /etc/ssmtp/ssmtp.conf file. Here is an example for sending mails using SSMTP and SSL:

mailhub=smtp.yourprovider.com:465
hostname=yourdomain.com
AuthUser=yourusername
AuthPass=yourpassword
FromLineOverride=NO
UseTLS=yes

When sending a mail using the mail() function, php will call the sendmail binary and ssmtp will deliver the e-mail transparently using SMTP.

  1. Postfix/Exim/Sendmail

You can configure a standard mailserver for you system and configure it to relay your emails over a 3rd party provider. This is a litte more complex than ssmtp but allows accepting emails on localhost SMTP port 25. Just google for smtp relay and your mailserver name.

  1. PHPMailer

When you create your own project and you can modify your code, you should use PHPMailer class, because sending an e-mail with html content, attachment or to multiple senders is complicated using the plain mail function and absolutely not recommended for beginners.

PHPMailer can send mails using SMTP itself, or it can be used together with the ssmtp tool.

Why is sending e-mails complicated?

As i said before, anti-spam measurements are getting more and more aggressive. to do it right, you have to setup a mailer daemon like postfix, exim or sendmail and configure it correctly. i.e. you need to make sure that no one can use your mailserver for sending mails without authentification. you have to configure your domain's MX record to point to your servers ip, because other mailservers verify your sender ip using the MX record. You may have to setup DKIM (DomainKeys Identified Mail) to keep you mail from being classified as Spam.

If you have setup your server and domain correctly, you are not done. The fun just starts.

If you start sending emails to different mail providers, some recipients may tag your emails as spam. This happens not only to newsletters and advertisings, some ppl are also tagging automated registration confirmations as spam. If a number of your emails got spam-tagged on a large e-mail-provider like microsoft, google or GMX they will block your IP address or classify it as spam. Then you have to sign up with microsoft and others and investigate the removal of your ip-address. This can be a time-consuming task.

it may also happen that your project gets hacked and your ip is sending millions of emails. if that happend, you may have a hard time clearing your bad ip reputation. you should have configured sending limits to limit the impact of hackers abusing you mail() function.

a 3rd party provider will do all this for you. they will configure the mailserver correctly, they will keep the ip reputation up and they will also block you from sending spam.

bhelm

Posted 2013-08-05T22:13:20.473

Reputation: 651