How I configured sendmail for PHP on Ubuntu Server 12.04

Preventing sendmail from been very slow

The first thing that I did after installing sendmail with

aptitude install sendmail

is I put “gate.localhost” (gate is my server name) to /etc/hosts so it looks like this:

127.0.0.1       localhost.localdomain localhost
127.0.1.1       gate.localhost gate

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

With default /etc/hosts containing only “gate” and “localhostsendmail hangs up for a while and writes to /var/log/mail.log the following message:

My unqualified host name (gate) unknown; sleeping for retry.

Testing

To test how sendmail works I used the following command:

/usr/sbin/sendmail -t -f myaddress@gmail.com -v -i < body.txt

Where body.txt is a text file containing the message body:

Subject: test mail to Some Address!
From: Me <fromaddress@somemail.com>
To: Vasya<toaddress@somemail.com>
first line of my message

Please note, that without –f  option provided in command line sendmail would not work, saying that there are some problems with the sender address.

Configuring SMTP server settings

To make sendmail work via my SMTP server I added SMART_HOST directive to sendmail.mc:

define(`SMART_HOST',`smtp.spbtlg.ru')dnl

Then I executed the following commands to changes take effect:

cd /etc/mail
m4 sendmail.mc > sendmail.cf
make
/etc/init.d/sendmail restart

Fortunately, my SMTP server does not require any authorization so I do not need to use AuthInfo directive.

Configuring PHP mail() function

At last I put the following to /etc/php5/apache2/php.ini:

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "/usr/sbin/sendmail -t -f myaddress@gmail.com -i"

Conclusion

I feel that there is another way, but looks like my php mail() function now works fine, at least I can receive messages from my websites.

13 Responses to How I configured sendmail for PHP on Ubuntu Server 12.04

  1. FIntan says:

    Thank you!

  2. Juan says:

    Great!! It worked! Thanks a lot!

  3. Nanyaks says:

    Works like a charm. Thanks.

  4. mc0e says:

    While php expects to find a sendmail binary, that does not mean you should install the sendmail package. It’s a historical relic really, but part of its legacy is that there’s a standard path for a binary called sendmail with a standard interface which most mail software provides.

    If it works for you, and since you’ve already installed sendmail, you might just leave it alone, but at the first hint of trouble (like your messing about with the sender address), you should switch to a modern mail program. My choice is postfix, but some people like exim or qmail, and of course there are many others less used.

    sendmail has a configuration option to set which system users are allowed to set the sender address to something other than their system username at sendmail’s configured domain name. It’s been 10 years since I’ve needed this setting, so I can’t give it to you off the top of my head, but if you look for it you’ll find it.

  5. stef says:

    The /etc/hosts settings are exactly what I needed for my dev-machine 🙂

  6. voiture says:

    Thank you for this article,
    can you give more details about the use of “SMART_HOST” ?

  7. balaji says:

    Thanks for the post…
    It helped me a lot…………
    nice work

  8. Sanjay Saha says:

    Thank you for this article. I do that, But not working in my server.
    Please tell me what i do.

  9. Virendra says:

    i get an error *** ERROR: FEATURE() should be before MAILER()
    *** MAILER(`local’) must appear after FEATURE(`always_add_domain’)*** ERROR: FEATURE() should be before MAILER()
    when i run
    m4 sendmail.mc > sendmail.cf

  10. Sajjad says:

    Thanks it worked for me.

  11. samad says:

    thank you this is worked…..nice job..!!

  12. Thank you very much! It gives me a shot.

  13. hameed says:

    i dont know how to do this. can anyone explain to me step by step please?
    Thank you

Leave a Reply

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