11

How do I configure PHP to send mail using mail() via a remote SMTP server?

I've tried to do this using php.ini but it seems that you can only do that under Windows32 and I want to do this on my Unix server.

Also I've tried to change the configuration for sendmail so it would use a remote SMTP server but I'm not sure that's possible.

Magellan
  • 4,431
  • 3
  • 29
  • 53
Brayn
  • 281
  • 2
  • 3
  • 9

8 Answers8

9

My choice is setup a postfix server in the same web server that sent mail only for localhost and with this setting in main.cf to use a remote SMTP server:

relayhost = [a.b.c.d]

It works for me.

hdanniel
  • 4,253
  • 22
  • 25
  • HD, can you please give more details, or point me to a resource that explains this. I've a Google account (as well as Google Apps), and I beleive SMTP requires authentication. Google is using TLS for SMTP on port 587 (and not 25). Where do I specify all this information? – Uri Jun 01 '11 at 18:10
  • This is the best answer really cause you offload the smtp to postfix. If you send mail via your app and the remote server is down you are blocking the request. The best option is to pass off the mailing to a worker in a queue to do that work though but this is a good first step in the right direction. – Mike Oct 08 '12 at 03:47
3

It's not possible, AFAIK. I would either:

  1. Use a fuller functioning mail class such as PEAR::Mail.
  2. If all mail from that machine should be smart-relayed then consider configuring SSMTP as your sendmail binary.
Dan Carley
  • 25,189
  • 5
  • 52
  • 70
2

msmtp can be used as an alternative to sendmail which simply forwards traffic directly to an external SMTP server. It's pretty easy to set up and works very well.

Jonathan Prior
  • 257
  • 1
  • 5
  • 12
1

PHP by itself can't do this.

Simple forwarders such as ssmtp have some problems relaying php mails.

http://www.freehackers.org/thomas/2008/07/13/linux-a-second-class-citizen-in-the-php-world/

orzel
  • 131
  • 3
1

You can either use PHP's inbuilt IMAP tools or the very handy PHPMailer

This could be better answered on Stack Overflow I fear...

Andy
  • 5,190
  • 23
  • 34
  • 2
    +1 for the answer, I disagree with the later comment though. Usually the sysadmins get the "fun part" of configuring SMTP as not all programmers have any idea of what that is. They just want "email working". Of course, I'm generalizing, no pun intended. – Andrioid Jul 14 '09 at 15:50
1

PHP on Linux relies on sendmail. I would recommend using a different mail function from another library if you really want to do this. There are recommendations on the following StackOverFlow Post: https://stackoverflow.com/questions/966907/specify-smtp-server-for-php-mail-in-freebsd

Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
0

The internal mail() function can only use SMTP on a Windows machine like you have said.

The only option is using PHPMailer. You can also try the PEAR PHP package: http://pear.php.net/package/Mail

Mohammed Naser
  • 197
  • 2
  • 3
-1

https://docs.nexcess.net/article/how-to-configure-a-remote-mail-server.html From the SiteWorx main menu, click Administration > Mail Options > Remote Setup. - See more at: https://docs.nexcess.net/article/how-to-configure-a-remote-mail-server.html#sthash.JeroEsDU.dpuf

  • Welcome to Server Fault! Whilst this may theoretically answer the question, please [provide context around links](http://serverfault.com/help/how-to-answer) so others will have some idea what it is and why it’s there. If possible summarise or quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – HBruijn Dec 04 '15 at 13:07