943,937 Members | Top Members by Rank

Ad:
Aug 2nd, 2005
0

Bash Mail script help

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrchcol is offline Offline
22 posts
since Aug 2005
Aug 2nd, 2005
0

Re: Bash Mail script help

what have you come up with so far? I can write a basic script, but don't have any experience using the mail command.
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Aug 2nd, 2005
0

Re: Bash Mail script help

Well I can't even get the mail command to work right. I can get the basics to work but then it fails
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrchcol is offline Offline
22 posts
since Aug 2005
Aug 2nd, 2005
0

Re: Bash Mail script help

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.
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Aug 2nd, 2005
0

Re: Bash Mail script help

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrchcol is offline Offline
22 posts
since Aug 2005
Aug 2nd, 2005
0

Re: Bash Mail script help

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.
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Aug 2nd, 2005
0

Re: Bash Mail script help

Thanks a million that helped a whole lot.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chrchcol is offline Offline
22 posts
since Aug 2005
Aug 2nd, 2005
0

Re: Bash Mail script help

your welcome :-)
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Aug 2nd, 2005
0

Re: Bash Mail script help

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.
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Mar 17th, 2008
0

Re: Bash Mail script help

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>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
riotxix is offline Offline
1 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: SED: Conditionally removing \n
Next Thread in Shell Scripting Forum Timeline: Append line based on fixed position





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC