PHP Mail Relay via Remote smtp Server

8,545

Solution 1

I would install nullmailer and configure it to forward emails to your relay server. The linux distribution that you are using most likely has nullmailer packages, so that a sendmail binary (that is command line compatible) is installed in the proper place.

If you do not want to install nullmailer, it is possible to do with sendmail too. If you want, I will update the answer.

UPDATE:

If you want to do this under sendmail, you have a number of choices:

(1) In sendmail.mc define the SMART_HOST to be your mail relay server:

define(SMART_HOST, `smtp:[relay.server]')dnl

(2) You can use FEATURE(nullclient).

(3) Modify rule set 0 to do this. In sendmail.mc add the following lines:

LOCAL_RULE_0
R$* < @ $* . > $*       $#esmtp $@ [relay.server] $: $1 < @ $2 . > $3

Do not copy-paste the code snippet above since the left hand side is separated from the right hand side with tabs and not spaces.

After you are done editing sendmail.mc you need to compile sendmail.cf and restart sendmail. In Debian this is done by running sendmailconfig. In CentOS this is done by running /etc/mail/make followed by service sendmail restart.

Solution 2

Please search first, this was asked before (e.g. in How to configure php.ini with remote SMTP?).

The PHP mail() function always uses sendmail (on Linux/Unix). There is no way to change that by php.ini. That leaves two other means:

  • For a single application you can replace all calls to the mail() function with calls to an alternative library (e.g. PHPMailer).
  • As a more general solution you can configure your local sendmail (or whatever program provides the sendmail command) to do the right thing, i.e. set the domain's mail server as a smarthost.
Share:
8,545
Toqeer
Author by

Toqeer

I am Linux system administrator and also can perform programming like automation stuff.

Updated on September 18, 2022

Comments

  • Toqeer
    Toqeer over 1 year

    Possible Duplicate:
    How to configure php.ini with remote SMTP?

    We have a php application running on Linux which sends emails to there users. Currently its setup like php.ini is configured to send via local sendmail server but we have separate mail server for our organization for this domain. I want to send the php application emails via that remote smtp server so these emails can have the correct SPF records and sign via DKIM.

    But I could not see such option in php.ini to specify the remote host address to forward emails to that, its for windows only.

    I saw some post which suggest phpMailer but I could not find how to configure that so all our php application could send via our remote SMTP.

  • Magellan
    Magellan over 11 years
    Another nice point, under Ubuntu I prefer to remove Postfix and install mailx and configure it in 'smarthost' mode that forwards mail to the network's mail server. Very little fiddling at install time when configured with 'dpkg-reconfigure'.
  • Toqeer
    Toqeer over 11 years
    Thanks for the answer, it will be better if you could put the sendmail configuration as well. I have sendmail already installed so I will just configure to forward to the relay host.
  • adamo
    adamo over 11 years
    Answer updated.