6

GOAL: Get a text file on my file server into my gmail inbox.

The file server is running Ubuntu Server 8.04 I currently SSH into it from time to time and to check logs, SMART stats for hard drives, and such. I'm looking into setting up a scheduled script that runs some commands and sends me the output via email.

The only issue I'm having is with actually getting the email sent. I have no experience with email config under linux and all guides pointing to sendmail seem to assume lots of things on behalf of myself and my current config. The servers are set up for file sharing with samba and NOT as mail, web, or dns servers. I don't want to run a mail server or route anything from the server or receive any mail on (or with) the server, unless any of that helps me accomplish the goal. The file server is on our normal network and has internet access.

I own several website domains (and have a hosting package). I've been able to set up apps like thunderbird to send mail using one of the accounts from my website by filling in smtp.mydotcom.com for the outgoing server to use and changing ports etc to match the information from my website's cpanel config page. Is there a way to do something similar so I can send an email from my file server to my external email?

EDIT: Here are the steps I took after reading Dennis Williamson's answer below. I was up and running in only a few minutes!

Install esmtp:

sudo apt-get install esmtp

I created a new email account on my website (called "alerts") and edited /etc/esmtprc like this:

hostname=mail.mydomain.com:26
username=alerts+mydomain.com

(These values were provided by the cpanel admin page)

Then I created a file called "eheader" with the default top of email:

To: Me <[email protected]>
From: Alerts <[email protected]>
Subject: subject

Message Body

So, in order to fire off the mail the following command can be used:

cat eheader <file1> <file2> ... <fileN> | /usr/bin/esmtp -t

So it's easy to vary which files are sent for the daily/weekly/etc jobs.

1
  • Are you sure it's not being blocked by a firewall or spam filter? The last Ubuntu server I set up was 8.04, and I received email out of the box from crond. Most of it went to spam in gmail, but I was able to filter it back out.
    – Jack M.
    Oct 30, 2009 at 20:14

7 Answers 7

4

I use esmtp, which is a send-only MTA, for that purpose. It is very simple to set up. It has sendmail-compatible command-line options (some ignored). It's in the repositories.

Here is a simple example:

echo -e "To: Recipient Name <[email protected]>\n\
    From: Me Myself and I <[email protected]>\n\
    Subject: Here is the example I promised\n\n\
    $(<somefile)" | /usr/bin/esmtp -t

This sends the contents of the file named "somefile".

There is a very simple configuration file, /etc/esmtprc, that contains the hostname, username and password for your upstream email provider (I'm assuming yours is gmail).

Instructions for setting it up for Gmail are here.

2

Look into ssmtp, which is a sendmail replacement that just passes on email to an SMTP server that you configure it with. It's probably one of the simplest ways you can get the capability to send emails programmatically. A web search for "ssmtp" should come up with plenty of tutorials and instructions on how to configure it.

Also, most programming/scripting languages have SMTP libraries available, so if you're familiar with, say, Perl or Python, you could write a little script that connects to an SMTP server to send the mail - basically you'd be writing your own, simpler version of ssmtp.

2

What you need to do is setup a SMTP server on the Ubuntu box that is configured to forward to the real SMTP server (on your network or at your ISP) such as ssmtp or esmtp. Here's a list of lightweight ones (mutt docs):

http://wiki.mutt.org/?LightSMTPagents

Update:

Since you're running Ubuntu, you'll have Exim installed.

You can configure it with sudo dpkg-reconfigure exim4-config. It's quite easy to setup to relay mail, once you read some of the docs.

/Update

The following are the Gmail SMTP server settings for sending mail through Gmail from any email client program:

  • set Gmail SMTP server address: smtp.gmail.com
  • Configure Gmail SMTP user name as: your full Gmail address (including @gmail.com) Google Apps users may have to enter username@your_domain.com
  • Configure Gmail SMTP password as: Your Gmail password
  • Configure Gmail SMTP port as: 465 or 587
  • Configure Gmail SMTP TLS/SSL required as: yes

Finally, you will write a script that calls a command-line mail client like mutt (I don't believe mail or mailx do attachments). You could also use Perl's or Python's mail APIs. The script creates an email and attaches the files you want.

Finally you would put a crontab entry in for the script run daily (or whatever interval you want):

01 * * * * root echo "This command is run at one min past every hour"
17 8 * * * root echo "This command is run daily at 8:17 am"
17 20 * * * root echo "This command is run daily at 8:17 pm"
00 4 * * 0 root echo "This command is run at 4 am every Sunday"
* 4 * * Sun root echo "So is this"
42 4 1 * * root echo "This command is run 4:42 am every 1st of the month"
01 * 19 07 * root echo "This command is run hourly on the 19th of July"

See man crontab and man cron

ANOTHER UPDATE:

You can send email from the commandline with SendEmail

2
  • Ubuntu Server may have a mail server installed, it depends on which boxes you checked off in the tasksel part of the install. Oct 30, 2009 at 19:00
  • Thanks. I thought it was a required package like in Debian (an MTA is required, exim is the default).
    – Swoogan
    Oct 30, 2009 at 19:08
1

You want to send mail, but not configure a mail server. Not a tough order but strange. Postfix can be set up to send out email in a few seconds, and it would often be useful to get normal emails sent to root, such as notices of MD raid components in failure.

If you cannot or will not set up email on that system, what do we have left?

If one of your servers or web hosting sites does have ssh, and probably has mail services already configured, how about a cron which scp's the log files from the server and mails it from the system having mail? Mail is often sent from unix with the mail or mailx command.

mailx -s "the subject line" [email protected] < filetomail.txt

Prior to this you could put the stuff you want reported in that file with whatever scripts or cp commands you would need to do. If it works better, you could place the cron on the Ubuntu server and have it issue ssh commands to the second server to pull the data down and mail it.

7
  • 4
    I don't think wanting to send email and not wanting to set up a mail server is strange. Every desktop computer I've ever used had an application that could send email. Not a single one of them had a mail server installed on it.
    – Swoogan
    Oct 30, 2009 at 16:16
  • In my view it is strange because 100% of the Linux installs I've seen include an MTA (pretty sure Ubuntu does as well) and it is far less effort to configure that than take this approach. If I was doing it, the MTA on the Ubuntu file server would be configured in less time than I've taken describing an alternate approach. The only reason I can see for avoiding the MTA is to disable the possibility of web site hacking to send out email via php weaknesses, and such, if it was running a web site as well.
    – labradort
    Oct 30, 2009 at 18:00
  • Here's another reason to avoid an MTA: "I have no experience with email config under linux and all guides pointing to sendmail seem to assume lots of things on behalf of myself and my current config." Your alternate approach my be more effort for YOU than setting up an MTA, but you're not setting up the MTA and your alternate approach isn't a very good one.
    – Swoogan
    Oct 30, 2009 at 19:22
  • 1
    I have no experience flying an aircraft but I need to get the plane to Detroit. Is the answer to learn to fly or hire someone who flies, or to say: "we'll take it apart, drive it there in a truck I am already capable of driving". Sure, you can drive a plane somewhere, just don't ask me to hold off comment that it could be easier! Every single other solution on this page involves installing or configuring an MTA, which is what he asked not to do. I'm trying to supply an answer in the way the OP asked.
    – labradort
    Oct 30, 2009 at 20:41
  • 1
    If I wanted to send email from my Windows desktop would you tell me to install Exchange? If I told you I didn't want to learn how to configure Exchange, would you say that was strange?
    – Swoogan
    Oct 30, 2009 at 22:12
1

I use a perl script with Mail::Sendmail to email reports from my servers (and Net::Twitter for status updates). Of course, that means dealing with CPAN which may not be worth it as you have to make the packages.

!#/usr/bin/perl 
use Mail::Sendmail;

%mail = ( To      => '[email protected]',
        From    => '[email protected]',
        Message => "This is a very short message"
       );

sendmail(%mail) or die $Mail::Sendmail::error;

print "OK. Log says:\n", $Mail::Sendmail::log;
1
  • 1
    Perl with MIME::Lite also works very well. Oct 31, 2009 at 7:40
-1
cat /var/log/file.log | mail -s "File logs" [email protected]

Try this. It will work on most Linux distributions out of box.

1
  • 1
    Does Ubuntu Server include a MTA "out of the box"? (Also, useless use of cat) Oct 30, 2009 at 15:16
-1

Try mutt. It is a very versatile. Allows command line usage incuding the ability to send attachments. Mutt is installed in Ubuntu server by default. So you should have it. The typical command line look like:

mutt -s 'The subject' -i message.txt [email protected] &

See 'man mutt' for full usage options. It also has an interactive mode if you just enter 'mutt' in a terminal.

4
  • "How do I configure Mutt to use a remote SMTP server to send mail? Mutt can't do so directly... You must specify a local MTA to do it." From wiki.mutt.org/?MuttFaq/Sendmail
    – Swoogan
    Oct 30, 2009 at 16:19
  • Have you tried it without setting up a remote SMTP server? In my case it works "out of the box" but YMMV. If you do need to set up a remote SMTP server you can install postfix.
    – simplr
    Oct 30, 2009 at 16:57
  • The whole point of his question is how to send email to a remote server.
    – Swoogan
    Oct 30, 2009 at 19:18
  • As I read it, the O.P. wants the remote server to send email to himself. That is exactly what I use mutt for. After reading the other answers to the question I don't understand why it works since I have not installed any smtp server on the computer that I tested my post on. I checked and none of the following are installed: exim, postfix, esmtp, mailx.
    – simplr
    Oct 30, 2009 at 22:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .