Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Using an external SMTP server (Gmail) to send mail


Go to End


2 Posts   21362 Views

Avatar
dalesaurus

Community Member, 283 Posts

19 September 2009 at 4:29am

Edited: 19/09/2009 7:44am

For those that are having external SMTP mail issues, like the post titled "Using PHPMailer to send emails from external SMTP"
http://www.silverstripe.org/general-questions/show/259943

There is a better way to solve this! DON'T MODIFY SS CORE FILES! This is never a good idea for many reasons. Fix your php mail() function to send via a relay instead.

This is exclusive to linux servers. I'll be presenting No Brainer SMTP (nbsmtp) here, a good alternative is Simple SMTP (sSMTP). I will be using Gmail as an example here as it seems to be a common ask.

What you need here is a MTA to send via an external SMTP server that supports SMTP_AUTH. SS currently (as of 2.3.3) can only take advantage of the php mail() function which passes through to sendmail on linux. Instead of hacking the source code per above give this a shot if you have some kind of admin rights over your server:

Step 1: Fix your MTA
-Install nbsmtp ( http://www.ferdyx.org/ ) or ( http://packages.debian.org/unstable/mail/nbsmtp )
This is a lightweight MTA that simply passes sendmail through to a relay. It is available for Debian Ubuntu RHEL and CentOS. It will install a link to /usr/sbin/sendmail that will be used by all apps that default to sendmail (If this concerns you see the Edit: below)

Step 2: Configure
-Option 1: Configuration via php's sendmail_path, set with ini_set() or by editing your php.ini

sendmail_path = sendmail -N -n -f username@gmail.com -h smtp.gmail.com -d gmail.com -p 25 -U username@gmail.com -P myPassWord

-Option 2: Create a system-wide config for nbsmtp (usually /etc/nbsmtprc). This is a bit more secure as your password is in a root owned file that can be locked down.

relayhost = smtp.gmail.com
port = 25
domain = gmail.com
auth_user = username@gmail.com
auth_pass = myPassWord
auth_mech = login
# You may need to set these to false if your installed version of nbsmtp wasn't compiled with SSL/TLS support
use_tls = true
use_starttls = true

Step 3: Test
Give it a test from the cli with either sendmail alone or from php -r.

dalesaurus@hostname:~$ sendmail -f username@gmail.com
To: test@email.com
Subject: nbsmtp test
I hope all is well?
.

dalesaurus@hostname:~$ php -r 'mail( "test@email.com", "nbsmtp test", "I hope all is well?");'
dalesaurus@hostname:~$

If nothing happens tail -f your logfile (usually /var/log/mail.log) and try again. It may help to enable debugging with -D (Option 1) or debug = 1 (Option 2).

Step 4: Relax
Pop a cold one, you're done. A little bit of admin work beats hacky coding any day. Bonus! You didn't have to change up your SS core files and use custom functions, so you can upgrade SS without any worries in the future.

nbsmtp is a MUCH simpler option than configuring a SMARTHOST with standard sendmail MTAs. Trust me. I've done it both ways :)

EDIT: You may not want to change whatever default MTA your server is setup to use with sendmail. There is no telling what kind of hell you may unleash if you 'apt-get remove sendmail' or 'yum remove sendmail' before Step 1 above.

If this is the case you should manually compile/install nbsmtp and call it directly instead of having it hijack your default /usr/sbin/sendmail. Then replace all the above 'sendmail' commands with 'nbsmtp'.

EDIT 2: If you do end up use sSMTP here is a good resource that covers Gmail: http://www.nixtutor.com/linux/send-mail-with-gmail-and-ssmtp/

Avatar
dalesaurus

Community Member, 283 Posts

15 December 2009 at 9:00am

Also after using some other relay mailers I have settled on msmtp as it has better support/documentation and is being actively developed. The configuration is the same methods as above (global config file or on the CLI via the sendmail line in the php.ini).

http://msmtp.sourceforge.net/

Install Guide:
http://forum.slicehost.com/comments.php?DiscussionID=2338

For Gmail:
http://laurentbois.com/2007/10/20/activemailer-using-msmtp-and-gmail/