Sponsored Content
Top Forums Shell Programming and Scripting Leanest way to send an email from bash Post 302910127 by mph on Tuesday 22nd of July 2014 02:35:53 PM
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
 

8 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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
SEND-UUCP(8)						      System Manager's Manual						      SEND-UUCP(8)

NAME
send-uucp, send-nntp, send-ihave - send Usenet articles to remote site SYNOPSIS
{ send-nntp | send-uucp | send-ihave } [ -d ] sitename:hostname | sitename [ sitename:hostname | sitename .. ] DESCRIPTION
The send-* utilities are scripts that process the batch files written by innd(8) to send Usenet articles to a remote NNTP or UUCP site. The sites to be fed may be specified by giving sitename hostname pairs on the command line. The sitename is the label the site has in the newsfeeds file, the hostname is the real hostname of the remote site, a FQDN (Fully Qualified Domain Name) in the case of an NNTP host, or the UUCP name in the case of a UUCP host. Normally, the sitename and the hostname are the same, and as such don't have to be specified as sitename:hostname pairs but just as a sitename. send-uucp compresses batches of news and sends the to the remote site with uux. send-nntp Starts an innxmit to send the articles to the remote site. send-ihave encapsulates the articles in an ihave control message and uses inews to send the articles to a to.sitename pseudo-group. Using send-ihave is discouraged, nobody uses it anymore and even the author of this manpage is unsure as to how it actually works or used to work. send-* expect that the batchfile for a site is named <pathoutgoing in inn.conf>/sitename. To prevent batchfile corruption, shlock(1) is used to ``lock'' these files. OPTIONS
-d The ``-d'' flag causes nntpsend to send output to stdout rather than the log file <pathlog in inn.conf>/<program-name>.log. NOTES
You should probably not use send-nntp, but innfeed, or if that is not possible, nntpsend. The usual flags in the newsfeed file to write a batch file suitable for processing by send-uucp are Tf,Wfb . The usual flags for a batch file for send-nntp are Tf,Wfm SEE ALSO
newsfeeds(5), nntpsend(8) SEND-UUCP(8)
All times are GMT -4. The time now is 03:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy