Not able to send email from linux terminal - linux

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/).

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%

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.

Bash send mail only if file contains string

A process creates a new log file each time it runs. If the log file contains the string ERROR, I want it to be emailed to me using mail. I do not want an email if there were no errors.
Created alias named admins in .mailrc for recipients
This command works fine to always send the log via email:
cat /home/user/import.log | mail -s 'Import Error Log' admins
Now then what I want is a concatenated command to check if the file contains 'ERROR' then run the mail command only if it does.
Thanks in advance.
if fgrep ERROR /home/user/import.log; then
mail -s 'Import Error Log' admins
fi

How to program Mutt to take an action upon new email arriving?

There is an option beep_new in the Mutt's configuration variables, which causes Mutt to issue a beep when new email arrives. Also there is <shell_esc> command, which executes a command in internal shell. Is there a way to cause Mutt to execute some user defined action (shell command) when new email arrives? Maybe <pipe-message> can help? (One can use <pipe-message> in a configuration file to call a shell command. Is there a way to do this in running Mutt?)
EDIT: as per Glenn advice, a script from Mutt can be called by configuring the custom status format in the .muttrc file:
set status_format="/some/script.sh '%r %f (%L) |"
But I can't figure out how to use this for new email detection, since the "New mail in..." notification appears on the command line, and not in the status line. And if the email arrived to a mailbox that is not the current, then the status line doesn't change at all.
EDIT 2: OK, the %b variable in the status_format did the thing.
Mutt can't do this (at least not without some workaround).
NeoMutt can: see it's new_mail_command.
You can achieve that through inotifywait as the mutt manual suggests.
Install inotify
(example for Debian based distros: apt install inotify-tools)
Watch mutt folder and run your script
(bash script example:
while : ; do inotifywait -e modify -r ~/.mutt ; /my_facy/script ; done

CPanel Email piping to shell script in linux, syntax

I am trying to write a shell script, where emails get piped to (by an email forward in cpanel).
The shell script will then post the entire email to a url using curl.
The script looks like this:
curl -d "param=$1" http://localhost/stuff/
And the forward looks like this:
|/home/usr/script/curlthis.sh
This is only sort of working.
The email gets bounced back even though the curl posts to the url successfully. (it looks like only part of the email is getting posted, but I am not 100% sure)
I have been told the email bounces because I am not reading the stdin, but I am not sure why I need to do that and why I cannot use $1?
How can I read the entire contents of the pipe (then post it using curl), and will that stop the mail server from bouncing it back?
EDIT
Using the answer below here is what I came up with:
#!/bin/bash
m=$(cat -)
escapedm="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$m")"
curl -silent -G -d "param=$escapedm" http://localhost/stuff/ 2>&1 >/dev/null
This part:
2>&1 >/dev/null
is shockingly important. If you don't redirect the stdout/err to null then the email gets bounced back for whatever reason.
Your mail is being passed to the script as a stream on stdin, and not as a parameter ($1). Note that your forward script begins with a pipe, and that's the mechanism passing the mail into your script.
So you should be able to read this in your shell (bash?) using the read statement. See this SO answer for more details.

Resources