Bash Mail script help

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 20
Reputation: chrchcol is an unknown quantity at this point 
Solved Threads: 0
chrchcol chrchcol is offline Offline
Newbie Poster

Bash Mail script help

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Bash Mail script help

 
0
  #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 Quick reply to this message  
Join Date: Aug 2005
Posts: 20
Reputation: chrchcol is an unknown quantity at this point 
Solved Threads: 0
chrchcol chrchcol is offline Offline
Newbie Poster

Re: Bash Mail script help

 
0
  #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 Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Bash Mail script help

 
0
  #4
Aug 2nd, 2005
from other posts I have read, would something like this work?
Shell Scripting Syntax (Toggle Plain Text)
  1. mail -s "subject" user_name@email_address.com < email_file


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

Re: Bash Mail script help

 
0
  #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 Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Bash Mail script help

 
0
  #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
Shell Scripting Syntax (Toggle Plain Text)
  1. mail -s "subject" chris.collins@fuse.net < email
the file called email would contain the body of or email.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 20
Reputation: chrchcol is an unknown quantity at this point 
Solved Threads: 0
chrchcol chrchcol is offline Offline
Newbie Poster

Re: Bash Mail script help

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

Re: Bash Mail script help

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

Re: Bash Mail script help

 
0
  #9
Aug 2nd, 2005
you might get some ideas from this
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2.  
  3. echo "enter password"
  4. read PASSWORD
  5. echo "enter email address"
  6. read EMAIL
  7. echo "enter username"
  8. read USER_NAME
  9.  
  10.  
  11. #this line just represents the location of the email body file as a variable
  12. EMAIL_BODY=/location/of/original #this contains your body(template)
  13.  
  14. # these following sed commands make a new email to be sent with the user input substituted
  15.  
  16. # this first sed command creates a new email body that will be sent with the mail command
  17. # since the -i option is not used a new email will be made, without changeing your original
  18. sed "s/PassWord/$PASSWORD/" ${EMAIL_BODY} > ammended_email
  19.  
  20. # the -i option is used now because it will be changeing the new ammended email
  21. sed -i "s/EmAil/$EMAIL/" ammended_email
  22. sed -i "s/UsEr/$USER_NAME/" ammended_email
  23.  
  24. 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 Quick reply to this message  
Join Date: Mar 2008
Posts: 1
Reputation: riotxix is an unknown quantity at this point 
Solved Threads: 0
riotxix riotxix is offline Offline
Newbie Poster

Re: Bash Mail script help

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC