Sending email with CC BCC and sender's address in unix mailx - email

I want to send email from HP unix using mailx command.
I have to include cc and bcc in my email and have to use the specific email address as the sender.
But -r (which is to define the sender's email address) will disalbe ~ commands so if i have to define the sender's email address, i cannot use ~c and ~b commands for cc and bcc.
Is there any work around???? cos these are the requirements from the user.
Thanks.

Just re-order the arguments to mailx command. That would give the desired result
$ echo "something" | mailx -s "subject" -b bcc_user#some.com -c cc_user#some.com -r sender#some.com recipient#example.com

In my case I have to keep multiple id's in cc which has been done by giving the email-id's comma separated one by one as below:
$ echo -e "Hi Team, \n \n Action Needed \n \n Regards, \n XYZ team"| mailx -s "subject" -b bcc_user1#some.com,bcc_user2#some.com -c cc_user1#some.com,cc_user2#some.com -r sender#some.com receiver#xyz.com
Also made use of the echo command to pass multiple lines to mailx utility. Thought it will be helpful.

Related

Using mailx command for sending mail with attachment and messagebody in UNIX

i have been trying to send an email from my script which is generating an excel and then zipping it. i want to send this zipped file as an attachment along with a message body, but no success.
i have been using the below code:
1) uuencode inputFileName OutputFileName | mailx -s "Report" abc#gmail.com -- -f abc#gmail.com
this commandline is successfully sending my attachment
2) uuencode inputFileName OutputFileName | mailx -s "Report" abc#gmail.com -- -f abc#gmail.com < MessageBody.txt
with this commandline, i am trying to send the same attachment with mail body fetched from external file MessageBody.txt, but it is then sending the mail only with mail body, and NO attachment.
You are giving two different sources for stdin. At the beginning of the line you are taking stdin from stdout of uuencode but at the end of the line you are taking stdin from the file MessageBody.txt.
Instead you could try the following:
uuencode inputFileName OutputFileName | cat - MessageBody.txt | mailx -s "Report" abc#gmail.com -- -f abc#gmail.com
regards Henrik

How to add X-header to unix mailx or add attachment to usr/sbin/sendmail

I am attempting to add the x-header X-APP-VOLT: Yes to the header of my email with a .tar attachment. I only have access to usr/sbin/sendmail and mailx. I do not have root access so I can't download other versions of mailx or mutt.
I can add the x-header to usr/sbin/sendmail using the below code, but I can't figure out how to add the .tar attachment.
/usr/sbin/sendmail -i -- toemail << END
To: toemail
Subject: Test
X-APP-VOLT: Yes
Hope this works! END
I can attach a .tar file to mailx using the the below code, but I can't figure out how to add a x-header. My mailx also does not have the -a option.
cat file | uuencode filename | mailx -s "Test" toemail
Thank you
One way is to construct your input in a temporary file:
cat > tmpfile$$ << END
To: toemail
Subject: Test
X-APP-VOLT: Yes
Hope this works!
END
uuencode filename < file >> tmpfile$$
/usr/sbin/sendmail -i -- toemail < tmpfile$$
Also, I usually use sendmail's -t flag in this case, rather than repeating the recipient:
/usr/sbin/sendmail -i -t < tmpfile$$
If you don't want to use a temporary file, if you want to use a pure pipeline, you can use ( ) to create a subshell to do the construction:
(
echo "To: toemail"
echo "Subject: Test"
echo "X-APP-VOLT: Yes"
echo
echo "Hope this works!"
echo
uuencode filename < file
) | /usr/sbin/sendmail -i -t
(Of course, these days most recipients will probably find it easier to deal with MIME attachments rather than uuencode. It's pretty straightforward to create MIME attachments with a shell script, also.)

Mailx - Daily email of log file to list of recipients

I was given a few example usages of mailx and asked to come up with another by my manager.
I have a program that will be left running. Once a day, we want to send the log file it generates to a list of recipients.
This was the closest example I received
00 11 * * * unset noclobber;cat /home/emailList.txt /tools/temp/summary.csv | mailx -t -s "Report `/bin/date`" -r person#company.com > /home/file.log
Seems like I want something like:
MyProgram | mailx -t -s "Report `/bin/date`" /home/emailList.txt -r person#company.com
But here, I'm not sending it once a day. This sends at MyProgram closing, right? I'm also sending MyProgram's output, not it's log file.
Thanks in advance.

Mailx with uuencode not sending multiple attachments with a proper email body on Suse Linux

This particular piece of code below worked correctly on AIX
( echo "mailbody";
uuencode a.txt 'arenamed.txt';
uuencode ab.txt 'abrenamed.txt';
uuencode abc.txt 'abcrenamed.txt';
uuencode abcd.txt 'abcdrenamed.txt'; ) | mailx -s "$subject" $emailaddress;
But on Linux, any occurrence of uuencode is printing begin 644 blocks in the body of the email viewed on Outlook 2010.
begin 644 abc.txt
5:F%H<V1L:G-A"F%S9&MJ87-J9#L*
`
end
I have tried, using different variations of ( echo $body ; uuencode filename filenamechanged ) with echo first, uuencode later and vice versa but it doesn't help.
I would have used "mail -a" but I want to rename files which are emailed, so, was looking at uuencode.
Any suggestions other than using sendmail/mutt here?
This is what worked
(echo "Subject: $Mail_Subject";
echo "To:$Mail_List";
echo $Mail_Body;
uuencode $LOG_DIR/FileName1 'AttachmentDisplayName1';
uuencode $LOG_DIR/FileName2 'AttachmentDisplayName2') | sendmail -t $Mail_List
Hope this helps anyone who is looking for this kind of issue.

How does PHP's `mail` work?

PHP's mail function seems to deliver mail on a clean system, with no apparent configuration done by the administrator or webmaster (no SMTP configuration in php.ini, etc.). How does the mail function deliver mail to a remote server?
On *nix it invokes the sendmail binary, which then uses the mail configuration to route the email. On Windows, it sends to a SMTP server. In both cases the sysadmin sets up the mail system.
You can detect how it works as below.
First method
$ ltrace php -r "mail('tester#127.0.0.1', 'Test', 'Hello world');" 2>&1 | grep sendmail
memcpy(0x095ea168, "sendmail_from", 14) = 0x095ea168
memcpy(0x095ea1e0, "sendmail_path", 14) = 0x095ea1e0
popen("/usr/sbin/sendmail -t -i ", "w") = 0x0977c7c0
From the results of the above command can be seen that the popen() function opens the process of /usr/sbin/sendmail -t -i.
$ ls -l /usr/sbin/sendmail
... /usr/sbin/sendmail -> exim4
So sendmail is the symbolic link to exim4 and hence sendmail -t -i invokes exim4 -t -i.
And in the manual page of exim4 you can read about these options -t -i:
$ man exim4 | grep ' -t -i'
-ti This option is exactly equivalent to -t -i. It is provided for compatibility with Sendmail.
Second method
Install snoopy and run:
# grep snoopy /var/log/auth.log | tail
... php -r mail('tester#127.0.0.1', 'Test', 'Hello world');
... /usr/sbin/sendmail -t -i
... /usr/sbin/exim4 -Mc 1YxxYn-0006a7-Nw
... /usr/sbin/exim4 -t -oem -oi -f <> -E1YxxYn-0006a7-Nw
... /usr/sbin/exim4 -Mc 1YxxYn-0006aB-Oj
The results of the above command show the sequence of the commands which were performed.
mail() uses sendmail, that uses DNS to find MX record of target domain and delivers there directly. thats it.
and since destination server probably does not know your ip address, especially if it is NATed it may be marked as spam.
you can modify your config to use different (legit ad known) smtp server to act as intermediary.
It's really not that reliable, actually, unless the underlying sendmail or something is properly configured.
Amazon SES has better servers than whatever server you're using and gets mail there more times than with mail().
The real reason you shouldn't use mail() is because your server's IP address is probably completely unknown to mail services such as GMail, Yahoo, etc, and there is a higher chance it will get marked as spam. Why does it get marked as spam? Because mail() is very easy and simple to exploit for spam purposes.