Configure Postfix/Sendmail for PHP mail() in Ubuntu

Actually configure Postfix or Sendmail for PHP mail() is the same. Both of them run the /usr/sbin/sendmail binary. It is quite confusing if we just look at the name of the binary but this is what Postfix did.

For more information, take a look at the Postfix Manual – sendmail.

For the PHP setting, open the /etc/php5/apache2/php.ini and configure the sendmail_path.
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 -i"

 

Save the file and restart the Apache server.

Try testing the PHP mail() function by the following command. Thanks amitbhatia1. =)

echo testing | mail -s Bla myemail@somewhere.com

 

If you need to send with attachment. you can refer to this post.
PHP – Send Attachment with PHP mail()

Done =)

Reference: Using the mail() function in php with postfix on linux

30 thoughts on “Configure Postfix/Sendmail for PHP mail() in Ubuntu”

      1. I didn’t set up the relay, because I hate gmail and want to close my account. I uninstalled postfix completely and installed it again and configed php.ini. Now mail() function is working. I have a question though.

        It works with $headers = ‘From: me@hushmail.me‘; But once I change it to a different address, such as me@gmail.com, (ie., ‘From: me@gmail.com‘;) the mail doesn’t get sent. In the mail.log file I see “Sender address rejected: Domain not found (in reply to MAIL FROM command)).” How can I fix this problem? When I save up enough money I want to build my own website, and I want to use the email address of my site to send and receive mails instead of me@hushmai.me.

        Like

      2. i am sorry that i couldn’t help.

        actually some hosting providers have email service and what you need to do is just setting up the domain for the email service. i think maybe you can seek help from you web hosting service provider.

        Like

  1. I made a simple php script to test and is giving this error on my server
    server name: Demeter
    OS: Linux RedHat

    installed: apache + php + Sendmail

    sendmail works mada normal email is ok
    the problem is to have php that will not and this error:
    n 25 12:20:09 demeter sendmail[13202]: NOQUEUE: SYSERR(apache): can not chdir(/var/spool/clientmqueue/): Permission denied

    Like

  2. I can’t seem to get it to work, I just want to send mail, my server has internet access but my site is lan only as my isp blocks port-forwarding, but I don’t think that sould matter much to send mail any help appriciated.

    Like

    1. The error log
      Feb 11 07:46:31 h4x0r-Linux postfix/smtp[13557]: connect to alt1.gmail-smtp-in.l.google.com[74.125.137.27]:25: Connection timed out
      Feb 11 07:46:31 h4x0r-Linux postfix/smtp[13557]: connect to alt1.gmail-smtp-in.l.google.com[2607:f8b0:400e:c01::1a]:25: Network is unreachable
      Feb 11 07:46:31 h4x0r-Linux postfix/smtp[13557]: connect to alt2.gmail-smtp-in.l.google.com[2607:f8b0:4002:c04::1a]:25: Network is unreachable
      Feb 11 07:46:32 h4x0r-Linux postfix/smtp[13557]: 821ADAB1BA: to=, relay=none, delay=21372, delays=21311/0.08/60/0,

      Like

  3. It’s not working for me either.
    Using mail to send like in you example, I only receive a waring message.

    WARNING: gnome-keyring:: couldn’t connect to: /tmp/keyring-rgI6w9/pkcs11: file or directory not found

    but I don’t think that’s the problem.

    My mail.log file:

    Sep  9 11:05:50 caco-laptop postfix/pickup[27589]: 8B219E0F57: uid=1000 from=
    Sep  9 11:05:50 caco-laptop postfix/cleanup[28075]: 8B219E0F57: message-id=
    Sep  9 11:05:50 caco-laptop postfix/qmgr[27590]: 8B219E0F57: from=, size=325, nrcpt=1 (queue active)
    Sep  9 11:05:50 caco-laptop postfix/smtp[28077]: connect to smtp.gmail.com[2800:3f0:4003:c00::6c]:587: Network is unreachable
    Sep  9 11:05:51 caco-laptop postfix/smtp[28077]: 8B219E0F57: to=, relay=smtp.gmail.com[64.233.186.109]:587, delay=1.1, delays=0.14/0.02/0.71/0.2, dsn=5.7.0, status=bounced (host smtp.gmail.com[64.233.186.109] said: 530 5.7.0 Must issue a STARTTLS command first. z32sm9888357qgd.40 - gsmtp (in reply to MAIL FROM command))
    Sep  9 11:05:51 caco-laptop postfix/cleanup[28075]: C9DBEE0FBA: message-id=
    Sep  9 11:05:51 caco-laptop postfix/qmgr[27590]: C9DBEE0FBA: from=, size=2211, nrcpt=1 (queue active)
    Sep  9 11:05:51 caco-laptop postfix/bounce[28078]: 8B219E0F57: sender non-delivery notification: C9DBEE0FBA
    Sep  9 11:05:51 caco-laptop postfix/qmgr[27590]: 8B219E0F57: removed
    Sep  9 11:05:52 caco-laptop postfix/smtp[28077]: C9DBEE0FBA: to=, relay=smtp.gmail.com[64.233.186.108]:587, delay=0.96, delays=0.09/0/0.67/0.2, dsn=5.7.0, status=bounced (host smtp.gmail.com[64.233.186.108] said: 530 5.7.0 Must issue a STARTTLS command first. l7sm10385441qaf.16 - gsmtp (in reply to MAIL FROM command))
    Sep  9 11:05:53 caco-laptop postfix/qmgr[27590]: C9DBEE0FBA: removed
    

    If you can help me?

    Like

  4. Hey Hi,
    postfix mail is working from past few years and suddenly it get stopped. dont know why.i tried to reinstall it. now i can able to send mails over command. But not with php code. but i did try to add this line to php.ini file.(sendmail_path = “/usr/sbin/sendmail -t -i”) and restart the postfix service. Here is the error log. please check and suggest.

    Jun 25 07:21:53 j78921 postfix/sendmail[29750]: fatal: Recipient addresses must be specified on the command line or via the -t option

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s