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

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:

Related

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)

Ldapsearch filtering using variables not displaying data

I am currently trying to query an LDAP server to find whether the email passed to the script exists on our system.
Below is the ldapsearch command I am trying to use:
ldapdata=`ldapsearch -h ### -b "ou=###,o=###" "email=$email" email firstname surname`
echo "ldapdata: $ldapdata"
This works perfectly when the filter includes a predetermined email, ie "mail=firstname-surname####" however when passed a variable, such as $email, the output is not able to be manipulated by further grep / awk statements and will not display any data in the echo statement.
From some Googling I have figured out It could be to do with the line wrapping which LDAP uses.
What I have already tried to solve the issue:
| perl -p00e 's/\r?\n //g
| sed '/^$/d
-o ldif-wrap=no
My question is, what is the best method to solve this issue. Many thanks in advance.
Just for anyone having the same issue, the issue was actually due to me writing and testing the program in a Windows environment.
I was pulling the $email variable from a file that is in the dos format.
To fix this all I did was :
dos2unix $FILELOCATION

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.

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

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