How to execute shell command by mailx - linux

I am using mailx in linux to send mail. Now i want to execute some shell command and paste the output in body of mail
For example i want to send date from linux in mail body
I tried below but that doesnt seems working
mailx -S smtp=$smtpip < date

Related

Linux sendmail command not sending mail while in a cron

I'm trying to run a sendmail command in a Red-hat Linux environment on a bash shell script through a cronjob. I can run this script successfully when it is ran manually and every other job within the shell runs correctly other than the mailing part.I have never used sendmail and am not sure if I need to restructure how it is being presented.
I have tried mail and mailx. I am able to send the emails but the log file contain many weird characters that it put the text format into a att00001.bin attachment on the email which I do not want. The sendmail command seems to be the only one that doesn't send an attachment when ran manually. Other cron jobs works correctly and are able to send emails they just do not have the special characters in the log file.
echo '##################################################'
date
echo '##################################################'
#Run Script and write to log file
/comp/gfb281m.sh > /usr/local/bin/oracle/getload/getload.log 2>&1
#Send log file to developer group
(echo "Subject:GetLoad Shell"; echo; cat
/usr/local/bin/oracle/getload/getload.log) | sendmail -v
exampleEmail#outlook.com exampleEmail2#mail.mil
When ran this cron job should send the contents of the getload.log file to a group a of users.
Fixed the issue thanks to another source. I was not using the sendmail's full path. I was just stating "| sendmail -v email" and not sendmails full path which was "/usr/sbin/sendmail" for me. Not sure if links are allowed here but below is where I found the answer.
https://www.unix.com/red-hat/271632-bash-sendmail-command-not-found.html
crontab sets PATH to /usr/bin:/bin. To avoid typing absolute command names like /etc/sbin/sendmail you can set up the PATH in you crontab:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/30 * * * * sendmail user#example.com%subject: Sample email%%Email body%

Command that outputs script results into a text file then emails results to personal email

I am writing a bash script that outputs server status results and want to have a way for it to put the results into a text file then email it to an email entered depending on user input. How would I go about doing this?
Thanks for the help!
to output the results to a file, use the >> or > to at the end of your command followed by a file name.
>> will append to the file. Which means the previous content of your file will remain intact.
> will remove everything and create a file with the new ouput.
for eg:
echo $serverOutput>output.txt
now to mail it :
read email // will read the email address of the user.
to mail:
uuencode output.txt output.txt|mailx -s "server output" $email
your file will be sent as an attachment.
Is this what you require?
Creating a cron job would be another task. But for your initial question about having the serverStatus inside a text file and then mail that to the user's email. Below is an example:
say we use "ps -ef|grep processname" command. replace "top" with the command you have.
ssh -q username#serverName "ps -ef|grep -i process">>output.txt
echo "enter email:"
read email
uuencode output.txt output.txt|mailx -s "serverStatusReport" $email
if you have any script/template which needs to be given this functionality, please share the code or elaborate a bit.
Regarding the cron job you would need to include the command/script in the crontab file along with the time you want it to schedule.
for eg:
echo "* * * * * /path/to/your/script.sh ">>crontab
Hope this gives you a path forward on your task

How do you pull text out of a file and Email it as the body in Redhat?

We have another piece of software saving data into an rtf file. We need to pull that data out and email it as the body of the Email. In SCO Unix we were able to use this:
mailx –s “Subject of email” email#example.com </text.rtf
But now that we are running Redhat the mailx package used is an entirely different package and will only attach the file as an attachment. Any help on this subject is greatly appreciated.
We had a similar problem and solved it by faking mailx by a shell script. Change $PATH so that your "mailx" shell script is found first.
In this script we use sendEmail (Perl script, look here http://caspian.dotconf.net/menu/Software/SendEmail/) to send the mail:
sendEmail $ABSAUTH $(echo \-u "$(echo "$1"|sed -e 's/^-s//')") -t $2
We ended up using the mail command instead.
$ mail –s “Subject of email” email#example.com < /text.rtf
some documentation can be found at https://www.binarytides.com/linux-mail-command-examples/ or using the "man mail" command.

How do I send email from linux terminal, with contents of multiple files as the mail-body?

I am aware that I can use the following command from my linux terminal to send an email:
sendmail user#example.com < file1.text
This command will basically send the contents of the file1.text as mail body.
But how can I add contents of multiple files inside mail body using the same command?
I tried the following which obviously did not work:
sendmail user#example.com < file1.text,file2.text
Use the cat command. cat is short for "concatenate"; it will concatenate all the files given on the command line. Then pipe the output to sendmail:
cat file1.text file2.text | sendmail user#example.com

Not able to send email from linux terminal

To send mail I followed the link https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/ .
When I enter the command
echo "My message" | mail -s subject myemailID#gmail.com
then no error displays but I am not getting the mail. I am attaching screenshot of file /etc/postfix/main.cf.
If you need to use your own mail server, you need to reconfigure it as an internet mail server. In debian like systems you can make it via shell like this:
sudo dpkg-reconfigure postfix
After this you will receive mails but i think you will can find them in spam dir.
But if you need simply to send mails from command line i recommend you to use another one tool - mutt (read this: https://www.linux.com/training-tutorials/how-installsetup-mutt-gmail-centos-and-ubuntu/).

Resources