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.

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

  1. FIntan on November 25, 2012 at 10:35 PM:

    Thank you!

  2. Juan on June 14, 2013 at 1:51 PM:

    Great!! It worked! Thanks a lot!

  3. Nanyaks on August 8, 2013 at 11:28 PM:

    Works like a charm. Thanks.

  4. mc0e on August 12, 2013 at 4:05 AM:

    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 on October 28, 2013 at 1:04 PM:

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

  6. voiture on April 3, 2014 at 3:47 PM:

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

  7. balaji on August 28, 2014 at 6:40 AM:

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

  8. Sanjay Saha on July 6, 2015 at 11:04 AM:

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

  9. Virendra on August 20, 2015 at 7:26 AM:

    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 on November 2, 2015 at 10:30 AM:

    Thanks it worked for me.

  11. samad on April 4, 2016 at 12:03 PM:

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

  12. FX Bayu Anggara on November 1, 2016 at 11:58 PM:

    Thank you very much! It gives me a shot.

Leave a Reply

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