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

Recommended Answers

All 9 Replies

what have you come up with so far? I can write a basic script, but don't have any experience using the mail command.

Well I can't even get the mail command to work right. I can get the basics to work but then it fails

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.

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

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.

Thanks a million that helped a whole lot.

your welcome :-)

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.

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>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.