User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 425,941 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,670 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 24967 | Replies: 9
Reply
Join Date: Aug 2005
Posts: 20
Reputation: chrchcol is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
chrchcol chrchcol is offline Offline
Newbie Poster

Bash Mail script help

  #1  
Aug 2nd, 2005
I am trying to write a command line bash script that will allow you to send mail to an email address using some variables.

The email is a signup form with username and passwords.

The subject line and most of the body will be hard coded.

The script should ask the email address first, then what is the username, then what is the password.

At that point hopefully, the variables are populated in the email and then its sent.

I cannot seem to get it to work. And I need as much help as possible including apparently, the ciommand line options to send the mail, I seem to be running blind.

Thank you
Chris
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Bash Mail script help

  #2  
Aug 2nd, 2005
what have you come up with so far? I can write a basic script, but don't have any experience using the mail command.
Reply With Quote  
Join Date: Aug 2005
Posts: 20
Reputation: chrchcol is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
chrchcol chrchcol is offline Offline
Newbie Poster

Re: Bash Mail script help

  #3  
Aug 2nd, 2005
Well I can't even get the mail command to work right. I can get the basics to work but then it fails
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Bash Mail script help

  #4  
Aug 2nd, 2005
from other posts I have read, would something like this work?
mail -s "subject" user_name@email_address.com < email_file


show me what you have done, and how it is failing.
Reply With Quote  
Join Date: Aug 2005
Posts: 20
Reputation: chrchcol is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
chrchcol chrchcol is offline Offline
Newbie Poster

Re: Bash Mail script help

  #5  
Aug 2nd, 2005
Well I guess first thing is first you can format a mail like

mail chris.collins@fuse.net -s "subject"

But then how do you bring in the body of the email

What I need to do is something like
________________________________
echo What is your email address?
read email
echo What is the username?
read user
echo What is the password
read password

mail $email -s "Account Activation"

And in the body of the email we have a long letter that needs to be populate in several places with the username and password variables.

echo What is your
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Bash Mail script help

  #6  
Aug 2nd, 2005
I think you use input redirection for the body(your email file) by placing the file at the end with a '<' will redirect it to the mail command
mail -s "subject" chris.collins@fuse.net < email
the file called email would contain the body of or email.
Reply With Quote  
Join Date: Aug 2005
Posts: 20
Reputation: chrchcol is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
chrchcol chrchcol is offline Offline
Newbie Poster

Re: Bash Mail script help

  #7  
Aug 2nd, 2005
Thanks a million that helped a whole lot.
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Bash Mail script help

  #8  
Aug 2nd, 2005
your welcome :-)
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Bash Mail script help

  #9  
Aug 2nd, 2005
you might get some ideas from this
#!/bin/bash

echo "enter password"
read PASSWORD
echo "enter email address"
read EMAIL
echo "enter username"
read USER_NAME


#this line just represents the location of the email body file as a variable
EMAIL_BODY=/location/of/original    #this contains your body(template)

# these following sed commands make a new email to be sent with the user input substituted

# this first sed command creates a new email body that will be sent with the mail command
# since the -i option is not used a new email will be made, without changeing your original
sed "s/PassWord/$PASSWORD/" ${EMAIL_BODY} > ammended_email

# the -i option is used now because it will be changeing the new ammended email
sed -i "s/EmAil/$EMAIL/" ammended_email
sed -i "s/UsEr/$USER_NAME/" ammended_email

mail -s "subject" person@gmail.com < ammended_email

for your body(template) I use words like UsEr, EmAil, PassWord. Use those odd words in your original. When sed looks to replace them with the user input, it will not accidently replace the wrong word. It leaves no room for an accidental mistake.
Reply With Quote  
Join Date: Mar 2008
Posts: 1
Reputation: riotxix is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
riotxix riotxix is offline Offline
Newbie Poster

Re: Bash Mail script help

  #10  
Mar 17th, 2008
Hi,

this had me perlexed for days. mail doesn't work, or give an error (unless I do it through my hosting company). Why? Because mail does not allow you to specify an smtp sever to relay your mail through, and unless you can go through the the grief of setting up you machine as a bonafide mail server, you're attempts will get rejected by the recepient as spam (without informing you).

Solution:
use nail, which does allow you to specify an smtp server to relay your mail through. You don't need to go through any sendmail config files.

http://forums.fedoraforum.org/showthread.php?t=143690
"Rupert Pupkin"
<i>The problem with the standard 'mail' command is that it assumes that the machine it is running on is a full-fledged SMTP server. So unless you've configured your machine to be a bonafide mail server (or to act as an SMTP relay), then your mail will not go anywhere outside your machine (i.e. it will only work for addresses local to your machine, e.g. some_user@localhost). There is no way to specify an external SMTP server (like your ISP's) to use with the 'mail' command. That's why you're getting a dead letter.

If you want a command-line mailer that does support using an external SMTP server, then get nail from Fedora Extras. Nail supports specifying an external SMTP server. You can do this on a per-mail basis, like this:

nail -r "myaddress@something.com" -s "Some subject" -S smtp=some.smtp.server info@company.com < msg.txt

or you can permanently set the SMTP server in your ~/.mailrc file (or /etc/nail.rc if you want to set it system-wide), which removes the need for using the "-S smtp=..." option on the command-line:

set smtp=some.smtp.server

See the nail man page for more details. In my opinion, no one should be using 'mail' anymore, nail is vastly superior."</i>
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 9:35 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC