Different ways of sending e-mail from linux command line - email

For our web projects, we need a reliable e-mail distribution mechanism. Due to bad experiences in the past, I have written a bash script (executed hourly) which sends a notification e-mail if
the qmail-send process is not running
there are too many failures in the mail log
For sending the notification e-mail I obviously don't want to depend on qmail, since qmail will be unavailable if the qmail-send process is not running. However, the following command sends the notification e-mail via qmail:
echo "failure rate critical" | mail -s "qmail notification" my#email.com
What's the easiest way to send e-mail from the linux command line without qmail? Can I use sendmail?
If you guys have smarter alarm systems to monitor qmail, please let me know.

Invoke the /usr/sbin/sendmail binary. It is usually available no matter which MTA you use and you can be sure it supports the standard sendmail interface if it's named sendmail.
The easiest way to use it is invoking sendmail -t and then writing the email including a valid To header to its stdin. If you omit -t you'll have to pass the recipient address as a commandline argument.
Another solution would be using SMTP but if you need to send emails from a bash script this is clearly a bad solution as there are no standard libraries in Bash which contain functions to send an email over smtp (unlike in python where you cannot easily send mails using sendmail but over SMTP).

You have a mail utility in POSIX. If you're only for linux, sendmail is ok (but then you rely on the system's (mis)configuration, right?).
All in all, SMTP protocol is not that difficult. I'd say you can talk in pure SMTP. It's about four commands to issue for a trivial mail. And it's portable:)
But if there are complications it may result in PITA...

Related

What is the easiest mail server setup for piping incoming mails to scripts *only*?

I want to set up a ticketing system (osTicket) on a centOS server that generates tickets from incoming e-mails.
osTicket can query mailboxes, but it also provides an API / scripts for piping. Is there a recommended way to setup a (lightweight) mailserver to pipe incoming emails to the script? I do not need actual mailboxes for users.
It's been a while since I did any work on a mail server, but it seems to me that I would only need to set up an MTA for this, and no MDA, correct?
My fallback is to set up POP3/SMTP inboxes elsewhere and query from osTicket. Easy as that would be, the local MTA setup seems cleaner to me.
Consider using remote mailbox accessible via IMAP with IMAP IDLE command support.
It will allow you to get "near real time" delivery to pipe without burden of configuring properly your own SMTP server.
[AFAIR IMAP IDLE is supported e.g. by gmail]
You may use fetchmail with custom procmail script as mda (no need for local SMTP/MTA server).
Using procmail (as "man in the middle) is not strictly necessary but your it will allow you to easily run filtering before delivery to the ticket system (e.g. anti-spam + anti-virus).

How to securely send emails in command line?

I would like to be able to send emails in command line and in bash scripts (e.g., to get notifications about running simulations on my computer). There are apparently different options to do that, but all seem to rely on postfix. However, as far as I understand it, postfix is a full email client to send and receive emails. In my case, I am just interested in sending emails.
I am particularly worrying about security issues and I don't want to open any doors on my system by installing one of these programs.
Any advice on how to configure postfix to only allow outgoing emails and to block anything else to avoid any threats? Or any other ideas to send emails securely?
Many thanks for your help!
PS: Running Ubuntu 14.04.01 LTS.

Exim: read local mail with simple mail client like unix `mail`

EDIT: I have not used the wrong mail reader, but my exim was no configured correctly. So I go and check that first. I used exim quite out-of-the-box in gentoo linux.
Original Question:
It's a bit hard to google, since the word mail means much more than just the unix command mail.
I want to run a local exim that spools most of the mail just locally. I want to read that mail with something like mail from commandline.
Now, exim does not use /var/spool/mail and not the same spooling format, so mail just finds no new mails.
So my question: how should I combine a simple mail client with exim, and which simple mail client is able to do so?
Check your exim.conf file to see which transports are delivering local messages. It will look something like this (from an Ubuntu machine):
mail_spool:
debug_print = "T: appendfile for $local_part#$domain"
driver = appendfile
file = /var/mail/$local_part
delivery_date_add
envelope_to_add
return_path_add
group = mail
mode = 0660
mode_fail_narrower = false
Also go look in your mail logs, somewhere under /var/log. Find where it's actually delivering the messages by finding what transport it's using to deliver them. It will be in the delivery line (the one containing "=>" to the local user) and will be of the format T=transport_name. You can look at that transport definition in exim.conf to determine where it's delivering them if you can't figure out where the emails are being delivered from the log messages.
Alternative: Every distro has the mutt MUA available as well (CentOS installs it by default). You might find it easier to install and configure mutt to read the mail spool wherever it is being delivered, than to try and re-work whatever custom changes your distro made to exim and its local mail spool delivery. Common delivery locations are:
/var/mail/$USER
$HOME/Maildir/
$HOME/.maildir/
Note that the presence of a trailing slash when defining mail spools usually indicates that the mailbox is in Maildir++ format (i.e. one file per message). The absence of that slash usually indicates that the mailbix is in mbox format (i.e. one big file).

How can I quickly bulk-send files to an email?

I regularly send files in Gmail to exchange files between different computers. It is cumbersome. I want quickly to send my files, such as error logs. I found a promising article [1] about Mutt, but it apparently is not what I want:
Please note that mutt is a pure MUA
and cannot send e-mail without proper
email server .
I want no email server, since I cannot install it to my clients' computers. I don't need to know the sender. I want quickly to send many error logs to my Gmail. I am looking something like:
mutt -s "Many error logs" -a
./log1 -a ./log2 -a ./log3 mygmail#gmail.com
I would really love to do the job in terminal. It is too time-consuming to bulk-send files in Gmail. How can I quickly send a great amount of files?
[1] http://www.cyberciti.biz/tips/sending-mail-with-attachment.html
Mutt can be configured to use a different MTA than the default sendmail. You could install SSMTP (described here and available for download here) or ESMTP and configure it to send directly to Gmail's web server, then configure Mutt to use SSMTP or ESMTP. This is nicely described in this Ubuntu tutorial. If installing systemwide binaries isn't an option, you ought to be able to install SSMTP or ESMTP to a local folder.
If you prefer scripting, mimesend, which is one of the example programs in Perl's MIME-tools package, does the same thing as your mutt command and could be hacked to connect to Gmail's web server.
Why are you using email for this? Other options:
FTP/SCP (run a server yourself, or purchase a hosted service)
Internet storage service like Dropbox.
assuming that you don't want to use ftp/scp as suggested above, here is how i solved a similar problem ... emailing logs from a client:
i created a cgi script on my server that had the ability to mail things to myself. a sort of email proxy webservice.
the client posted the files it needed to send (along with authentication, obviously) to that cgi script.
that way the client only needs to talk to my server instead of talking to a mail server.
perhaps this helps.
-don

automation: email yourself a file

I have a computer at home which I can't access from work. I'd like to be able to view results from work that my home computer produces. The best idea I've come up with is an automated script running on my home computer that emails myself the results (from a text file or stderr/out) when complete.
I'm decent with bash (I have a linux machine) and java, so an answer using either or both of those would be ideal, but if there's something easier that's fine too.
I typically use gmail, but also have yahoo mail.
My question is this: what would be the basic steps in solving this problem? I can do the nitty gritty stuff, but can't really get the big picture of how something like this would work.
Please help.
jbu
Howto set up ssmtp to send through a Gmail account
Some of the steps here might seem strange at first, but the rationale is put
in footnotes that should hopefully explain why.
First create a spare account on gmail which you will only use for
sending email. For instance, if your normal account is user#gmail.com,
create an account user.noreply#gmail.com with a newly created password
which you only will use for this account [1].
Set up the new account to forward all email to the normal account [2]
and under account settings you should add all other email adresses you
use [3].
Then install ssmtp (On Debian: aptitude install ssmtp) and edit ssmtp's configuration file /etc/ssmtp/ssmtp.conf:
root=user#gmail.com
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
AuthUser=user.noreply
AuthPass=passwdusedonlyforthisaccount
FromLineOverride=YES
and configure the local mail delivery by editing /etc/ssmtp/revaliases
assuming that your local login is localuser:
root:user#gmail.com:smtp.gmail.com:587
localuser:user#gmail.com:smtp.gmail.com:587
Make sure the two configuration files are readable to all users who
should be able to send email [4].
Test the setup by e.g. mailx (On Debian: aptitude install bsd-mailx):
echo 'testing, one, two' | mailx -s 'test 1' user#gmail.com
Hope this helps.
[1] The new gmail user name and password will be visible to everyone who
can log onto your machine, so you do not want this account to be
critical in any way, meaning you can close it down immediately if
someone should get access to it.
[2] If some email you sent bounces back to you, you might want to know
about it, and there actually exists people who will happily reply to an
email from johnsmith.noreply.
[3] Gmail will rewrite the From header on the email if it does not recognise the address.
[4] Ssmtp runs as the local user who sends the email, so that user needs
read access to the configuration files.
On any Linux I have used the mail sending from command-line is simple:
mail -s "My subject here" recipient#wherever.com <message_body.txt
AFAIK this acts as a front-end to sendmail, and you have to have sendmail configured to forward the messages to your ISP mail server.
You can't access your home computer from work which rules out a "remote support" option.
Can you access other computers on the Internet? If so, you could simply set up one of the online storage options and then ftp the results from your home computer. That's a lot simpler then trying to write scripts or code to generate emails with attachments or whatever.
You could then view the external computer from work.
If you have netcat, this command will send you an e-mail:
Given a file in this format (from Wikipedia):
HELO relay.example.org
MAIL FROM:<bob#example.org>
RCPT TO:<alice#example.com>
RCPT TO:<theboss#example.com>
DATA
From: "Bob Example" <bob#example.org>
To: Alice Example <alice#example.com>
Cc: theboss#example.com
Date: Tue, 15 Jan 2008 16:02:43 -0500
Subject: Test message
Hello Alice.
This is a test message with 5 headers and 4 lines in the body.
Your friend,
Bob
.
QUIT
Then netcat it to an SMTP server you have access to:
nc mail.somewhere.com 25 < file.txt
This will then send the e-mail. You can see how you can create a Java program to do this for you (just execute the commands).
Traditionaly, with unix systems like Linux, you'd have an MTA, a mail transfer agent, on the computer that deals with sending e-mail.
This could be a full blown e-mail server like exim, or something simple like ssmtp that just sends messages on to a relaying SMTP server such as would be provided by your ISP.
This isn't neccessarily the case anymore, since mail clients like Thunderbird include their own MTA, much like mail clients on Windows do.
However, it is likely that your distro will install some MTA or other by default, if for no other reason than the fact that other things on your system, like cron, want to be able to send e-mail. Generally there will be a command line tool called sendmail (sendmail being the original MTA [citation needed], other MTAs maintain compatability with its interface and it has sort of become the standard) that can be used from a shell script to send an e-mail.
My solution assumes that you have a SMTP server available which allows you to send an email programmatically. Alternatively, you can use a local install of sendmail which generally is available with most linux distros.
Create a standalone java program which watches the directory your home computer saves the file to. Use the JavaMail API to attach and send the file to any email you wish.
If you're also familiar with the Spring Framework, it has a nice abstraction layer for working with JavaMail and makes this sort of thing trivial.
Of course, your home ISP probably has the common SMTP port blocked as well.