Bash mail script which asks recipient, subject and body [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a problem trying to make my own bash mail script which every time will ask to enter a recipient, subject and body in console. And then send it. Could anyone help with this? Thanks a LOT!
P.S I am using mail command

read -p "What is your subject ? " subj
read -p "What is your message? " mess
read -p "What is the recipient address? " add
grep -E '[[:alnum:]]+#[[:alnum:]]+(.[[:alnum:]]+){1,2}' <<< $add
if [[ "$?" == "0" ]
then
echo "$mess" | mail -s "$subj" $add
else
echo "ERROR - The recipient address is in the wrong format"
fi
Here we read in responses for subject, body and recipient we then check for the correct format of the email address and use the variables to send a mail if the recipient address is in the correct format. If not, show an error message.

You will use the read command to get user input. Hint: use the -p option.
Don't forget to "quote" all your variables, especially with variables holding unknown user input.

Related

How to use cURL to verify a web page is fully loaded? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a case where after deploying to a server, the UI of my web page takes around 20 mins to load. The API is available almost immediately.
I need a way to use curl to load a web page and verify from the response that whether the web page is loaded or not.
Combining curl with grep you can request your page and see if it loads by looking for a specific string you'd expect to see when it renders correctly.
Something like:
curl -o - https://www.example.com/ | grep "Something from successful response"
if [ $? -eq 0 ] ; then
echo "Success"
else
echo "Fail"
fi
The -o - option to curl outputs the response to stdout which is then piped to grep which looks for a specific string from a successful. Depending on your needs there may be other ways but this sounds like it matches what you're asking.
Also note if your UI takes 20 minutes to load the first time, you might need to adjust some curl options (e.g. --max-time) to allow for longer timeouts.

How to fill in a Command Line Interface?

I am to automate the installation phase of a legacy system, because I do not want to put more efforts again and again when ever I want to install it. During the installation process on Linux Terminal, I have to answer some questions regarding the basic configurations. Indeed, it is easy to automate the shell commands by putting them all in a batch file like the following:
bin/startServer destination/sub_v1
bin/startAdminInterface
....
However, I do not know how to automate the answers of a specific questions like the following:
Enter the server's IP address:
Enter the email of the contact person:
Would you like to disable UDP services?(y/n) [n]:
....
Is there any tool or programming language can deal with this situation? or Is there any way to pass the answers as a parameters within the batch file?
Thanks in advance.
The classic Linux tool for this job is expect.
With expect one can expect different questions and variations on a question, and the question does not have to be typed exactly. expect does not blindly answer every prompt, but rather it provides answers to the questions actually asked.
Here is a short example:
#!/usr/bin/expect -f
spawn someScript.sh
expect "Enter the server's IP address:"
send "10.0.0.4\r"
expect "Enter the email of the contact person:"
send "foo#bar.com\r"
expect "Would you like to disable UDP services?(y/n) [n]:"
send "y\r"
So, imagine this is a simplified version of the script:
#!/bin/bash
read -p "Enter something:" thing
read -p "Enter server IP:" ip
read -p "Enter Email:" email
echo Received $thing, $ip, $email
and this is in a file called answers
thingywhatnot
11.12.33.240
bozo#brains.com
You would run
installationScript < answers
and it would print this
Received thingywhatnot, 11.12.33.240, bozo#brains.com

Escaping bash variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm a bit stuck with this. I'm delaring a variable at the top of my script, then I am creating a file as part of my script:
app="testing"
cat <<EOF >/etc/init.d/test
#!/bin/bash
args="--emperor $APPCONF/test/$APP.ini"
EOF
It doesn't seem to work though, it seems on the $app variable. Must I do something to this variable to get it to display it's value, "testing" inside the file I create?
Use consistent case. variable names are case sensitive.
Let's say you were doing this the Right Way. You'd want to store your data in an array:
args=( --emperor "${appconf}/test/${app}.ini" )
and then convert it to a string for embedding:
printf -v args_str '%q ' "${args[#]}"`
...and use that string inside your heredoc:
#!/bin/bash
args=( $args_str )
EOF
...beyond which, anything inside the script being created would want to expand it as an array:
run_something "${args[#]}"
See BashFAQ #50 for rationale and details.
Besides using consistent case ($app is different from $APP), you may want to enclose your variable names within brackets - you may get issues if you use spaces in between your variables values otherwise, and it's considered a good practice. For example:
args="--emperor ${APPCONF}/test/${APP}.ini"
That way, $APPCONF does not get confused with ${APP}CONF also. I hope this helps!
I'm not sure to understand your question. I suppose that you would like to end with a file
/etc/init.d/test
containing the text:
#!/bin/bash
args="--emperor $APPCONF/test/testing.ini"
if so your script should be:
app="testing"
cat <<EOF >/etc/init.d/test
#!/bin/bash
args="--emperor \$APPCONF/test/$app.ini"
EOF

Postfix transport - invoke script after receive mail [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Debian Sid, latest postfix from Sid.
I need to invoke bash script after user reveive mail. So, what I did:
create file /etc/postfix/transport, for example:
mail#domain.com myscript
run command to create database: postmap transport
add to main.cf: transport_maps = hash:/etc/postfix/transport
add to master.cf: myscript unix - n n - - pipe user=michal flags=FR argv=/home/michal/test.sh
reload postfix
What's the problem? If I configure it this way, after mail is received, script "test.sh" will be executed, but incoming mail will not be delivered to mailbox and it will be deleted immediatelly after receiving.
So - how to avoid this? I need the script to be executed, but incoming mail should be also delivered to my mailbox.
Use Procmail.
:0c
| $HOME/test.sh
The script receives the full message on standard input, but if you don't feel like parsing the message yourself, there are standard techniques for extracting header values into Procmail variables. You can pipe to formail:
SUBJECT=`formail -zcxSubject:`
or you can grab into MATCH, which avoids spawning an external process, but is a bit trickier for more-complex tasks;
:0
* ^Subject:[ ]*\/.+
{ SUBJECT=$MATCH }
(the whitespace inside [ ] should be a space and a tab); either way, you can now pass in $SUBJECT as a parameter on the test.sh command line. Obviously, other header values can be extracted into variables in a similar way.
PS. You cannot inline the formail call like this because it will consume the standard input from the pipe.
:0c
| $HOME/test.sh "`formail -zcxSubject:`" # erroneous!
Instead, you need to split it up, like this:
:0
* ^Subject:[ ]*\/.+
{ SUBJECT=$MATCH }
:0c
| $HOME/test.sh "$SUBJECT"

How to change sender name (not email address) when using the linux mail command for autosending mail? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
Mailbox shows the sender name as "Apache", because the mail I am autosending is being sent from a Perl CGI program. How do I change it to something else?
You just need to add a From: header. By default there is none.
echo "Test" | mail -a "From: Someone <someone#example.com>" other#example.com
You can add any custom headers using -a:
echo "Test" | mail -a "From: Someone <someone#example.com>" \
-a "Subject: This is a test" \
-a "X-Custom-Header: yes" other#example.com
mail -s "$(echo -e "This is the subject\nFrom: Paula <johny#paula.com>\n
Reply-to: 1232564#yourserver.com\nContent-Type: text/html\n")"
milas.josh#gmail.com < htmlFileMessage.txt
the above is my solution..just replace the "Paula" with any name you want e.g Johny Bravo..any extra headers can be added just after the from and before the reply to...just make sure you know your headers syntax before adding them....this worked perfectly for me.
You can use the "-r" option to set the sender address:
mail -r me#example.com -s ...
In case you also want to include your real name in the from-field, you can use the following format
mail -r "me#example.com (My Name)" -s "My Subject" ...
If no From: header is specified in the e-mail headers, the MTA uses the full name of the current user, in this case "Apache". You can edit full user names in /etc/passwd
It depends on what sender address you are talking about. The sender address visble in the recipients mailprogramm is extracted from the "From:" Header. which can probably easily be set from your program.
If you are talking about the SMTP envelope sender address, you can pass the -f argument to the sendmail binary. Depending on the server configuration you may not be allowed to do that with the apache user.
from the sendmail manpage :
-f <address>
This option sets the address of the envelope sender of a
locally-generated message (also known as the return path).
The option can normally be used only by a trusted user, but
untrusted_set_sender can be set to allow untrusted users to
use it. [...]
On Ubuntu 14.04 none of these suggestions worked. Postfix would override with the logged in system user as the sender. What worked was the following solution listed at this link --> Change outgoing mail address from root#servername - rackspace sendgrid postfix
STEPS:
1) Make sure this is set in /etc/postfix/main.cf:
smtp_generic_maps = hash:/etc/postfix/generic
2) echo 'www-data yourusername#yourdomain.com' >> /etc/postfix/generic
3) sudo postmap /etc/postfix/generic
4) sudo service postfix restart

Resources