Leanest way to send an email from bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Leanest way to send an email from bash
# 1  
Old 07-17-2014
Leanest way to send an email from bash

Is there a quick and dirty utility to send emails (does not need to be part of a package to receive and view email, in fact, I'd prefer if it wasn't).

Ideally I would like to be able to send attachments as well.

Mike

---------- Post updated at 04:29 PM ---------- Previous update was at 03:22 PM ----------

OK I am shocked this actually works! I am almost there. I need to "wrap" the base64 in something so the mail client recognizes it.
Code:
(   echo "MAIL FROM: xxxxx@yyyyy.com"
    sleep 1
    echo "RCPT TO: zzzzz@yyyyy.com"
    sleep 1
    echo "DATA"                                 #thank goodness it seems to receive DATA fast!
    echo "Subject: Test email"
    echo                                        #requires two new lines for subject
    echo "This is a test"
    echo
    cat filename | base64
    echo "."                                    #end of DATA character
    sleep 1
    echo "QUIT" ) | telnet smtp.yyyyy.com 25

---------- Post updated at 04:59 PM ---------- Previous update was at 04:29 PM ----------

Getting closer although the mail is still plain text.

Code:
--MyDogHasFleas
Content-Type: text/csv; name="thing.csv"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="thing.csv"

base64 lines


--MyDogHasFleas--

Mike

Last edited by Michael Stora; 07-17-2014 at 08:37 PM..
# 2  
Old 07-18-2014
Have you got the mail or sendmail commands available? You can feed these with input files. Have a read of the manual pages. There are configuration files to edit to get them to relay the mail correctly, but that should be pretty easy and it would make your code much simpler.



Robin
# 3  
Old 07-18-2014
Usually, sending email from the prompt involves using a mail-transport-agent like sendmail or mailx. If you don't have one, setting up your own mail server isn't exactly "lean", so I might suggest installing ssmtpd instead -- a "stub" MTA which just forwards to whatever real mail server you configure it to.
# 4  
Old 07-21-2014
OK. I got it! Works great. Had to read some RFC documents. Got a clue where to start from a mention of RFC 2822 in a rejection message and from there I found the RFCs for MIME.

Code:
{   echo "MAIL FROM: me@yyyy.com"
    sleep 1
    echo "RCPT TO: you@xxxx.com"
    sleep 1
    echo "DATA"
    sleep 1
    echo "Subject: Test email" # some sources say extra line after subject but it breaks MIME for some reason.
    echo "From: Me <me@yyyy.com>" # gmail will reject email without additional From: header
    echo "MIME-Version: 1.0"
    echo "Content-Type: multipart/mixed; boundary=\"XXXXboundary text\""
    echo    
    echo "This is a multipart message in MIME format."
    echo "If you can read this you need a newer mail client or you need to stop playing with your time machine."
    echo "RmVsbG93IE1hbW1hbHM6IFdlIHN0cmlrZSB0aGUgRGlub3NhdXJzIGF0IGRhd24uICBMb25nIGxpdmUgdGhlIGV2b2x1dGlvbiEK"
    echo
    echo "--XXXXboundary text"
    echo "Content-Type: text/plain"
    echo    
    echo "this is the body text"
    echo
    echo "--XXXXboundary text"
    echo "Content-Type: text/csv; name=\"thing.csv\""
    echo "Content-Transfer-Encoding: base64"
    echo "Content-Disposition: attachment; filename=\"thing.csv\""
    echo
    base64 <myfile.csv
    echo
    echo "--XXXXboundary text--"
    echo "."
    sleep 1
    echo "QUIT"; } | telnet smtp.yyyy.com 25

Sending mail with attachments from a minimal linux system with no mail client! Smilie

Mike

Last edited by Michael Stora; 07-21-2014 at 07:31 PM..
These 2 Users Gave Thanks to Michael Stora For This Post:
# 5  
Old 07-22-2014
Well done for persevering with this and thanks for updating the thread too.

I've proved it works on RHEL 6 too. We have some servers where we don't want to allow mail for users to work, but we still need to get alerts out. Just installed the telnet client and it fired up. I had to increase the sleep counts and send a HELO first, but that may just be my relay server requirements. Smilie


Good job! Smilie



Robin
# 6  
Old 07-22-2014
Quote:
Originally Posted by rbatte1
Well done for persevering with this and thanks for updating the thread too.

I've proved it works on RHEL 6 too. We have some servers where we don't want to allow mail for users to work, but we still need to get alerts out. Just installed the telnet client and it fired up. I had to increase the sleep counts and send a HELO first, but that may just be my relay server requirements. Smilie


Good job! Smilie



Robin
I should say HELO or EHLO and will add that. I should also add Date: to the extended headers (beginning of the body) under RFC 2822. In addition recipients are assumed to be bcc (by my server?). Those that should be To, CC, and BCC need to be spelled out in the extended header as well. BCC is frounded on by my employer so I'm going to add some code.

In doing more reseach it appears that subject is usually the last line of the extended header and that a blank line separates the extended header from the true "body" which is why many sources say you need a blank line after subject.

Mike
This User Gave Thanks to Michael Stora For This Post:
# 7  
Old 07-22-2014
For this type of script I would use expect. If you're connecting to an email host that either not too close or, has a bad connection. The sleep statements might not work to well.

Using autoexpect to create the skeleton for communications with the email server would ensure that you wait for a response. It's also very configurable as far as what you can do if the server doesn't respond as expected. I. E. the mail server responds that your recipient doesn't exist, etc...

Here's a the script autoexpect generated from a command line connection. Note that the scripts generally work route off the hop. Some "massaging" needs to be done. Also, I remove almost ALL the -exact to take into account variations on responses.
Code:
set timeout -1
spawn $env(SHELL)
match_max 100000
expect -exact "# "
send -- "telnet localhost 25\r"
expect -exact "telnet localhost 25\r
Trying 127.0.0.1...\r\r
Connected to localhost.\r\r
Escape character is '^\]'.\r\r
220 Laptop ESMTP Postfix\r
"
send -- "ehlo localhost\r"
expect -exact "ehlo localhost\r
250-Laptop\r
250-PIPELINING\r
250-SIZE 10240000\r
250-VRFY\r
250-ETRN\r
250-ENHANCEDSTATUSCODES\r
250-8BITMIME\r
250 DSN\r
"
send -- "mail from: me@mydomain.com\r"
expect -exact "mail from me@mydomain.com\r
250 2.1.0 Ok\r
"
send -- "rcpt to: you@yourdomain.com\r"
expect -exact "rcpt to: you@yourdomain.com\r
250 2.1.5 Ok\r
"
send -- "data\r"
expect -exact "data\r
354 End data with <CR><LF>.<CR><LF>\r
"
send -- "Subject: Testing again\r"
expect -exact "Subject: Testing again\r
"
send -- "\r"
expect -exact "\r
"
send -- "This is a test. only Only a test.\r
"
send -- "\r"
expect -exact "\r
"
send -- ".\r"
expect -exact ".\r
250 2.0.0 Ok: queued as EA2CCD202EA\r
"
send -- "quit\r"
expect -exact "quit\r
221 2.0.0 Bye\r
Connection closed by foreign host.\r\r
# "
send -- "^C"
expect -exact "\r
# "
send -- "^D"
expect eof

----------------------------------------------------
Forgot to mention. To make things much easier, you can use variables for the input(send).

Just 2¢ worth of a thought.

Last edited by mph; 07-22-2014 at 03:43 PM.. Reason: Additional thought
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. SuSE

Send outgoing email to my GroupWise email

Dear users, I have Linux server whose versions are Suse 10 SP 3 and Suse 11. I am trying to send email from these servers to my GroupWise email account. In /etc/postfix/main.cf file, The current value of MYHOSTNAME is LINUX.LOCAL. What should be the right value of MYHOSTNAME? Is... (0 Replies)
Discussion started by: JDBA
0 Replies

2. Shell Programming and Scripting

Bash for checking and copy Daily files and send an email

Hello, Trying to get a bash for files received on daily basis and want to copy those files to different directory and send an email notification if file exists then send the file counts else Alert with no file received. FileName format: DailyTransaction_2015-03-09_ 200.csv before 200 their is... (1 Reply)
Discussion started by: krux_rap
1 Replies

3. Shell Programming and Scripting

send email to email id which is having # symbol

Hi, I have one requirement to send email to email id which is having # ( in the begining of the email id). I'm using mailx command to send an email. But not receiving those emails, but email status is showing email sent successful. Code which i'm using is : cat file1.txt | mailx -s... (3 Replies)
Discussion started by: latika
3 Replies

4. UNIX for Dummies Questions & Answers

new to ldap, send email to a ou or group, and see a list from email client

hi, i'm running openldap on ubuntu 10.04, creating new items with apache directory studio (windows version). i use the ldap just as an address book to our small office (email clients are windows live mail 2009, 2011, microsoft outlook 2007 and 2010). a. i cant see a list of the contacts,... (0 Replies)
Discussion started by: V4705
0 Replies

5. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

6. Solaris

Send an email from Solaris using Linux email server

Hello everyone I have a problem and I need your help: I have a Solaris 10 and Solaris 8 UNIX Servers, and Linux Centos4 as email server. I need send an email from Solaris servers preferably using Centos4 email server. I have no mail service configured in my Solaris computers (1 Reply)
Discussion started by: aflores
1 Replies

7. UNIX for Dummies Questions & Answers

Send email where # is in the email address - Using Unix

Hi All, How do I send an email using malix where email address contains a #. I have a email address like this : #test@test.com I want to send email like malix -s "TEST" #test@test.com < SOMEFILE I tried \# but doesn't work. Please let me know how we can achieve this? I am in... (1 Reply)
Discussion started by: jingi1234
1 Replies

8. UNIX for Advanced & Expert Users

Unable to send eMail from a UNIX-Host ( using mailx ) to a Outlook-email-addres(Win)

Hi A) I am able to send eMail using mailx from a UNIX ( solaris 8 ) host to my Outlook-email-ID : FName.Surname@Citigroup.com ( This is NOT my actual -eMail-ID). But in Outlook the "From :" eMail address is displayed as " usr1@unix-host1.unregistered.email.citicorp.com " .i.e the words... (2 Replies)
Discussion started by: Vetrivela
2 Replies
Login or Register to Ask a Question