When I moved my mail functionality (Qmail) from my FreeBSD-server to Google Apps I was left with no way to send email from my server (sendmail disabled and gmail removed) from shell. I have no longer the need to recieve email from my server, thanks to Google! However, I needed to send emails from my server to report server status and report software/hardware errors.

This is how I solved the problem on my FreeBSD 7.0 server. I have verified that it also works on 9.1

1. Freebsd has a guide for setting up send only with ssmtp. First you should install ssmtp.

cd /usr/ports/mail/ssmtp/ && make install replace clean

2. Then you need to configure ssmtp for Gmail. Create this file in /usr/local/etc/ssmtp/ssmtp.conf. Replace email and password with your Gmail-account settings.

root=youremail@gmail.com
mailhub=smtp.gmail.com:587
AuthUser=youremail@gmail.com
AuthPass=yourpassword
UseSTARTTLS=YES
rewriteDomain=gmail.com

3. Test ssmtp. First you have to create a textfile with the message that you are going to send. Copy the text between the lines below.

Save the text to ”testmessage.txt”.
————————–
To: youremail@gmail.com
From: youremail@gmail.com
Subject: Testmessage

This is a test
————————–

Then run this command to send the testmessage.

ssmtp -v youremail@gmail.com < testmessage.txt

You should see the following output:

[<-] 220 mx.google.com ESMTP 14sm2982172ewy.10 [->] EHLO mysql
[<-] 250 PIPELINING [->] STARTTLS
[<-] 220 2.0.0 Ready to start TLS [->] EHLO servername
[<-] 250 PIPELINING [->] AUTH LOGIN
[<-] 334 VXNlcm5hbWU6 [->] bWFyY3VzLm55YkBnbWFpbC5jb20=
[<-] 334 UGFzc3dvcmQ6 [<-] 235 2.7.0 Accepted [->] MAIL FROM:
[<-] 250 2.1.0 OK 14sm2982172ewy.10 [->] RCPT TO:
[<-] 250 2.1.5 OK 14sm2982172ewy.10 [->] DATA
[<-] 354 Go ahead 14sm2982172ewy.10 [->] Received: by Servername (sSMTP sendmail emulation); Tue, 09 Mar 2010 12:53:14 +0100
[->] From: "Username"
[->] Date: Tue, 09 Mar 2010 12:53:14 +0100
[->] To: youremail@gmail.com
[->] Subject: Testmessage
[->]
[->] This is a test
[->] .
[<-] 250 2.0.0 OK 1268135596 14sm2982172ewy.10 [->] QUIT
[<-] 221 2.0.0 closing connection 14sm2982172ewy.10 If everything seems ok you should now have a fresh email in your Gmail account. Check it out! If not, then you should probably check your ssmtp.conf file and verify that you can connect to smtp.google.com 4. Last, replace sendmail with ssmtp. This makes ssmtp the default mailer for your system. mv /usr/sbin/sendmail /usr/sbin/sendmail.org
ln -s /usr/local/sbin/ssmtp /usr/sbin/sendmail

5. Check that mail is working.

mail -v -s "TestSubject" youremail@gmail.com
Testing and
.

Output:
[<-] 220 mx.google.com ESMTP 16sm3004137ewy.7 [->] EHLO mysql
[<-] 250 PIPELINING [->] STARTTLS
[<-] 220 2.0.0 Ready to start TLS [->] EHLO servername
[<-] 250 PIPELINING [->] AUTH LOGIN
[<-] 334 VXNlcm5hbWU6 [->] bWFyY3VzLm55YkBnbWFpbC5jb20=
[<-] 334 UGFzc3dvcmQ6 [<-] 235 2.7.0 Accepted [->] MAIL FROM:
[<-] 250 2.1.0 OK 16sm3004137ewy.7 [->] RCPT TO:
[<-] 250 2.1.5 OK 16sm3004137ewy.7 [->] DATA
[<-] 354 Go ahead 16sm3004137ewy.7 [->] Received: by mysql (sSMTP sendmail emulation); Tue, 09 Mar 2010 13:11:41 +0100
[->] From: ”Username”
[->] Date: Tue, 09 Mar 2010 13:11:41 +0100
[->] To: youremail@gmail.com
[->] Subject: TestSubject
[->]
[->] testing
[->] .
[<-] 250 2.0.0 OK 1268136703 16sm3004137ewy.7 [->] QUIT
[<-] 221 2.0.0 closing connection 16sm3004137ewy.7 6. Check the log file. cat /var/log/maillog

Output:
SMTP[755]: Creating SSL connection to host
sSMTP[755]: SSL connection using RC4-MD5
sSMTP[755]: Sent mail for username@servername(221 2.0.0 closing connection 14sm3068161ewy.2) uid=0 username=username outbytes=696
sSMTP[1234]: Creating SSL connection to host
sSMTP[1234]: SSL connection using RC4-MD5
sSMTP[1234]: Sent mail for username@servername (221 2.0.0 closing connection 16sm3004137ewy.7) uid=0 username=username outbytes=349

Summary
This worked for me straight out of the box. I didn’t need to make any changes to make it work. I hope it works for you as well!

Share →

3 Responses to Sending email in FreeBSD with Gmail

  1. willer skriver:

    Thanks for this, Marcus!

  2. Brett skriver:

    To automate the process of replacing sendmail as the default mailer, simply execute:
    ”make install replace clean” in step 1 above.

Kommentera

E-postadressen publiceras inte. Obligatoriska fält är märkta *