getting dead.letter file on Linux? - linux

i am sending mail to list of users, i am sending mail using mailx utility
mailx -s "$SUBJECT" "$TO" < $FILE
its working fine with valid emails, but problem is that i am getting dead.letter file when i tried to send to mail id like adffadf , it string not a valid email,
expected - i want this dead.latter must not be occur even user having anything for email ID.
like abc#gmail.com, abc#def.cc, adffdfs

The man page for my mailx says a lot of things about set nosave and so on, but they don't seem to work. The only way to stop your dead.letter file growing I have found is to replace it by a link to the special file /dev/null.
rm ~/dead.letter
ln -s /dev/null ~/dead.letter

Related

Shell script to send a custom message to users listed in a file?

I would like to write a shell script, that opens two text files (e.g message.txt, users.txt) and sends the message found in the first file ($1) to every single user found in the second file ($2)?
If a user is currently logged in, the message should be sent with the "write" command, else (if they are not currently logged in), it will be sent as a "mail".
I tried running this code:
#!/bin/bash
msg=`cat $1`
input=$2
while IFS= read -r usr
do
`write $usr $msg`
if [ $? -eq 1 ]
#here I tried checking if the $usr found in the $2 file is not online
`mail $usr $msg`
#The Subject part could also be a problem here
fi
done < "$input"
However, I am unsure how the mail part should be done, when running the code it even says that there is an error on line 22 (where I tried mailing the user).
You can use the following syntax to send an email via bash:
mail -s 'message subject' username#gmail.com <<< 'testing message body'
So in your case, you should add the -s parameter before the subject and you should add the body to add to your email. (you can use '' for empty body)
Another syntax to perform that:
echo "text message body" | mail -s "message subject" username#gmail.com
PS: You should not use the backticks (``) for the mail command

adding the .txt file's content in the body of the message in the mail of mailx command in linux

I have written a script where i am storing the name of the top 10 processes which are using the greatest ram of the cpu with a command:
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head >/etc/tmp/file.txt
Now I am attaching this file in the mailx command to send a mail with its body as content with the command:
echo -e "Warning, server memory is running low!\n\nFree memory: $free MB\n$file" | mailx -a "ALERT" -s "$subject" -r "$from" -c "$also_to" "$to"
this "subject" , "from", "also_to", "to" are some variables and have actual mailid defined in the shell script.
when I get receive the mail I get the content in a very rough order like the picture below that i have added.that mail's pic
I want this content to of the file to be in proper format or if I could just send this txt file as an attachment, I made a whole study for sending the file as an attachment using the mailx command but to no avail.
You are misusing the -a option. You are supposed to use it to add a mail header (Header: value), not the single word "ALERT" like you did.
As to how to send the file as an attachment, I usually use the -A option with no trouble (at worst, I vaguely remember that sometimes I had to specify the file encoding so that accented characters are correctly displayed, but you don't need this with your ASCII file)

Change the From field in an email with the mail command under linux without the -a option

Problem: when I use the mail command under linux (Ubuntu Server 16.04) as root to send an email (several scripts on my server do so), the From: field in the mail header looks like From: root#mydomain.org. I want it to look like From: admin#mydomain.org.
Attempt: I already found the option -a "From: admin#mydomain.org" to add the field to the mail header.
My whole command looks like this:
echo "content" | mail -s "subject" "recipient#wherever.org" -a "From: admin#mydmain.org"
Second Problem: However, I do not want to write the -a option at every point I use the mail command in a script because this is some kind of hard coding.
Second Attempt: My best attempt yet is to write a wrapper, though I think there should be a cleaner method to always add that header field to mails sent with the mail command.
Question: Does anyone know a better way which does not include hard coding? Still I want to use such a simple command line as above to not make things unnecessary complicated.
Best
Fabian
the mail command reads ~/.mailrc or a different startup file given by the environment variable MAILRC. (see manpage, http://manpages.ubuntu.com/manpages/xenial/man1/bsd-mailx.1.html#contenttoc4)
this file can contain the line set from=you#example.org.
conf="$(mktemp)"
trap 'rm -f "$conf"' EXIT
echo set from=you#example.org > "$conf"
export MAILRC="$conf"
# now mail will use you#example.org as From:

Saving messages from mailx command line

Is there a way to save messages to a file using mailx, using only the command line? I know that I can copy messages to a file by first entering mailx:
mailx -A my_account
Then typing
& c 1-10 first_ten_messages.txt
Which would save the first 10 messages to a file.
What i'd like to do is something similar but without having the interactive part. So something like:
mailx -A my_account --options "c 1-10 first_ten_messages.txt"
Is this possible?
Thanks
this should do it.
echo 'c 1-10 first_ten_messages.txt' | mailx -A my_account
If you want to select messages from specific a specific sender, you can run a similar command:
echo 'c from "Baji Boo" from_baji_boo.txt' | mailx -A my_account.
It is important to note that from works with the enveloped name, not straight email address.
In general, running mailx and typing h will give you good information as well as reading man mailx.
You can search messages in different ways and save to a file using the echo method.

Bash mail - Send as another user only

I've been working on sending HTML emails using BSD mail and so far I've been successful. I've even been able to modify, not change though, the sender.
Current Command:
cat $htmlFile | mail -s "$(echo -e "$subject\nContent-Type: text/html")" $recipient -v -- -F $sender
However when the email comes through, the sender is only appending the $sender to the host name. Let's assume that the following is true
$user=root
$HOSTNAME=server.com
$sender='Application Support<support#acmeinc.com>'
When the email comes through it reads:
Application Support<support#acmeinc.com> <root#server.com>
How can I make it so that only the $sender variable is used in the email instead of appended?
OS: RHEL 5.10
Kernel Rev: 2.6.18-371.8.1.el5
Still not sure what you're trying to do here, so I will take a stab and guess that you are looking for the -r flag? It sets the 'from-addr'. I don't think it is present/supported in BSD mail, but it is in mailx.
Other options include doing stuff with postfix/sendmail in order to set the sender address. You can do lots of stuff with aliases and the like, and more advanced mangling can be done through "transports" if you write your own handler.

Resources