Saving messages from mailx command line - linux

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.

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:

getting dead.letter file on 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

Making the script issue "enter" after executing command in shell script [duplicate]

I've created a really simple bash script that runs a few commands.
one of these commands needs user input during runtime. i.e it asks the user "do you want to blah blah blah?", I want to simply send an enter keypress to this so that the script will be completely automated.
I won't have to wait for the input or anything during runtime, its enough to just send the keypress and the input buffer will handle the rest.
echo -ne '\n' | <yourfinecommandhere>
or taking advantage of the implicit newline that echo generates (thanks Marcin)
echo | <yourfinecommandhere>
You can just use yes.
# yes "" | someCommand
You might find the yes command useful.
See man yes
Here is sample usage using expect:
#!/usr/bin/expect
set timeout 360
spawn my_command # Replace with your command.
expect "Do you want to continue?" { send "\r" }
Check: man expect for further information.
You could make use of expect (man expect comes with examples).
I know this is old but hopefully, someone will find this helpful.
If you have multiple user inputs that need to be handled you can use process substitution and use echo as a 'file' for cat with whatever is needed to handle the first input like this:
# cat ignores stdin if it has a file to look at
cat <(echo "selection here") | command
and then you can handle subsequent inputs by piping the yes command with the answer:
cat <(echo "selection here") | yes 'y' | command

Resources