How can I build a dynamic parameter list for mail command in bash linux? - linux

I am trying to send a mail with a file names in the message body from my terminal to my gmail account. I am using mail command to do it. My requirement is that I should dynamically update the file names in the message body as the code run...I no need to send as an attachment. I am using centos7.
Below is my script:
v_cfg_email_adresse_to="abc#gmail.com"
v_cfg_email_subject="Report from December 2016"
v_tmp_email_text_name="Message Body"
v_email_main_file="test3.sh"
v_tmp_path="/home/centos/rr/"
if [ ! -z "${v_email_main_file}" ]; then
v_mailx_parameters+=( -a "${v_tmp_path}${v_email_main_file}" )
fi
v_mail_x_parameters+=( -s "${v_cfg_email_subject}" )
v_mail_x_parameters+=( "${v_cfg_email_adresse_to}" )
printf '/binmailx %s < %s\n' "${v_mail_x_parameters[*]}" "${v_tmp_email_text_name}"
/bin/mailx "${v_mailx_parameters[#]}" < "${v_email_main_file}"
If the code is not correct..Please help me..
Thanks in advance

Instead of above code i just redirected my output in the mail command using > .I works fine!!

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

Mail command sending the error log file as attachment instead of in the Body in Linux

I'm having an issue while sending an email from the Linux box using mail command. Instead of sending the error log as in the Body it is sending as an attachment in .bin format. Where as in few instances it is sending in the Body. Below is the log details which I'm trying to send via email as body. Is it because of Special characters like "/" in the log it is sending as .bin attachemnt. I can use sendmail inorder to fix this but we want to send it using mail command.
Caused by: org.springframework.integration.MessagingException: Failed to write to '/DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz.tmp' while uploading the file Caused by: java.io.IOException: Failed to rename '/DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz.tmp' to /DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz'. Server replied with: 550 'PartsMasterFull_NNANissanV5124_20211015071711.xml.gz.tmp': cannot rename.
2021-10-15 02:01:49,342 ERROR | dbs-intg-scheduler-18 | c.n.d.j.adapter.support.MDCFatalErrorChannelInterceptor | [DBS] Fatal Error [org.springframework.integration.MessageDeliveryException: Error handling message for file [/datapp/common/batch_datafile/parts/P-16/outComingFolder/PartsMasterFull_NNANissanV5124_20211015071711.xml.gz]] -org.springframework.integration.MessagingException: Failed to write to '/DBSInboundandOutbound/prod/outbound/DSP/PartsMaster/PartsMasterFull_NNANissanV5124_2021101507171
1.xml.gz.tmp' while uploading the file
mContent=`cat $1`
msgimp=$2
mailsub=$3
monitor=$4
logname=$5
echo >> /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt
echo $mContent >> /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt
mail -s "[Prod] [$monitor] [$msgimp] [$mailsub] found in $logname log" -r "DBS Production
Alert <noreply#*******.com>" alert.*********.com <
/datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt
rm -f /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt
mContent=`cat $1`
...
echo $mContent >> /datapp/common/operation_admin/monitor/monitor_log/$mailsub.txt
At this point, the contents of $mailsub.txt are not the same as the contents of $1. The mContent variable is unquoted, therefore all sequences of whitespace (including newlines!) get replaced by a single space. So the body of the email is one long line.
I'm guessing here, but if that line is very long, I can imagine some part of the email system would change the message to move the body into an attachment.
I don't see why it would be necessary to read the input file, write it (incorrectly) to a new file, mail the contents of the new file, and delete the new file. Try this instead:
mData=$1
subject=$(printf "[Prod] [%s] [%s] [%s] found in %s log" "$4" "$2" "$3" "$5")
from="DBS Production Alert <noreply#*******.com>"
to="alert.*********.com"
mail -s "$subject" -r "$from" "$to" < "$mData"

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

How to send a html email with the bash command "sendmail"?

Anyone has a demo available?
Sendmail is said to be not scalable,but it's free,so I decided to use it first for now:)
The following works:
(
echo "From: ${from}";
echo "To: ${to}";
echo "Subject: ${subject}";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "";
echo "${message}";
) | sendmail -t
For troubleshooting msmtp, which is compatible with sendmail, see:
https://wiki.archlinux.org/index.php/Msmtp
https://superuser.com/a/868900/9067
If I understand you correctly, you want to send mail in HTML format using linux sendmail command. This code is working on Unix. Please give it a try.
echo "From: me#xyz.com
To: them#xyz.com
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary='PAA08673.1018277622/server.xyz.com'
Subject: Test HTML e-mail.
This is a MIME-encapsulated message
--PAA08673.1018277622/server.xyz.com
Content-Type: text/html
<html>
<head>
<title>HTML E-mail</title>
</head>
<body>
<a href='http://www.google.com'>Click Here</a>
</body>
</html>
--PAA08673.1018277622/server.xyz.com
" | sendmail -t
For the sendmail configuration details, please refer to this link. Hope this helps.
I understand you asked for sendmail but why not use the default mail? It can easily send html emails.
Works on: RHEL 5.10/6.x & CentOS 5.8
Example:
cat ~/campaigns/release-status.html | mail -s "$(echo -e "Release Status [Green]\nContent-Type: text/html")" to.address#company.com -v
CodeShare: http://www.codeshare.io/8udx5
This page should help - http://www.zedwood.com/article/103/bash-send-mail-with-an-attachment
It includes a script to send e-mail with a MIME attachment, ie with a HTML page and images included.
Found solution in http://senthilkl.blogspot.lu/2012/11/how-to-send-html-emails-using-sendemail.html
sendEmail -f "oracle#server" -t "name#domain.com" -u "Alert: Backup complete" -o message-content-type=html -o message-file=$LOG_FILEĀ  -a $LOG_FILE_ATTACH
-a option?
Cf. man page:
-a file
Attach the given file to the message.
Result:
Content-Type: text/html: No such file or directory
To follow up on the previous answer using mail :
Often times one's html output is interpreted by the client mailer, which may not format things using a fixed-width font. Thus your nicely formatted ascii alignment gets all messed up. To send old-fashioned fixed-width the way the God intended, try this:
{ echo -e "<pre>"
echo "Descriptive text here."
shell_command_1_here
another_shell_command
cat <<EOF
This is the ending text.
</pre><br>
</div>
EOF
} | mail -s "$(echo -e 'Your subject.\nContent-Type: text/html')" to.address#company.com
You don't necessarily need the "Descriptive text here." line, but I have found that sometimes the first line may, depending on its contents, cause the mail program to interpret the rest of the file in ways you did not intend. Try the script with simple descriptive text first, before fine tuning the output in the way that you want.
It's simpler to use, the -a option :
cat ~/campaigns/release-status.html | mail -s "Release Status [Green]" -a "Content-Type: text/html" to.address#company.com

How do I Send Email from the Command Line?

I would like to quickly send email from the command line. I realize there are probably a number of different ways to do this.
I'm looking for a simple way to do this from a linux terminal (likely a bash shell but anything should do) and an alternative way to do this on Windows. I want to be able to whip up an email right on the command line or have the flexibility to pipe the message into the command line program. How would you go about doing this? If you have small scripts that would be fine as well.
$ echo "This is the email body" | mail -s "This is the subject" me#email.com
Alternatively:
$ cat | mail -s "A few lines off the top of my head" me#here.com
This is where my
multiline
message would go
^D
^D - means press ctrl+d
You can use mail:
$mail -s <subject> <recipients>
You then type your message and end it with a line that has only a period. This signals you are done and sends the message.
You can also pipe your email in from STDIN and it will be sent as the text of an email:
$<mail-generating-program> | mail -s <subject> <recipients>
One small note with this approach - unless your computer is connected to the internet and your DNS settings are set properly, you won't be able to receive replies to your message. For a more robust command-line program you can link to your POP or IMAP email account, check out either pine or mutt.
You can also use this sendmail version for windows. It is very simple to use, standard UNIX-like behavior. Fast. Does not need any installation, just call the EXE wherever it is located on your system.
Composing the email:
echo To: you#example.com, me#example.com >> the.mail
echo From: them#example.com >> the.mail
echo Subject: This is a SENDMAIL notification >> the.mail
echo Hello World! >> the.mail
echo This is simple enough. >> the.mail
echo .>> the.mail
Sending the file:
\usr\lib\sendmail.exe -t < the.mail
type the.mail | C:\Projects\Tools\sendmail.exe -t
If you are looking to do this from a Windows command line, there is a tool called blat that can be used from a CMD prompt.
It is a bit more fun from PowerShell. Since PowerShell has access to the .NET Framework, you can use the classes from System.Net.Mail to send email. There is an example script on the PowerShell Community Script Repository.
IIRC you'll also have to configure a mail transfer agent (MTA) to use mail or most email libraries. Sendmail is the most well known but is a real pig when it comes to configuration. Exim, Qmail and Postfix are all popular alternatives that are a bit more modern.
There are also more lightweight MTAs that are only able to send out mail, not receive it: nullmailer, mstmp, ssmtp, etc.
Postfix is default for Ubuntu. This wiki article describes how to configure it - be sure to only allow forwarding from your local address!
Here is a Power Shell example of a script to send email:
$smtp = new-object Net.Mail.SmtpClient("mail.example.com")
if( $Env:SmtpUseCredentials -eq "true" ) {
$credentials = new-object Net.NetworkCredential("username","password")
$smtp.Credentials = $credentials
}
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = "script#mycompany.com"
$objMailMessage.To.Add("you#yourcompany.com")
$objMailMessage.Subject = "eMail subject Notification"
$objMailMessage.Body = "Hello world!"
$smtp.send($objMailMessage)
If you want to invoke an email program, then see this article:
How do I open the default mail program with a Subject and Body in a cross-platform way?
If you are on a Linux server, but mail isn't available (which can be the case on shared servers), you can write a simple PHP / Perl / Ruby (depending on what's available) script to do the same thing, e.g. something like this:
#! /usr/bin/php
<?php
if ($argc < 3) {
echo "Usage: " . basename($argv[0]) . " TO SUBJECT [CC]\n";
exit(1);
}
$message = file_get_contents('php://stdin', 'r');
$headers = $argc >= 4 ? "Cc: $argv[3]\r\n" : null;
$ret = mail($argv[1], $argv[2], $message, $headers);
exit($ret ? 0 : 1);
Then invoke as follows:
mail me#example.com test < message

Resources