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
Related
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)
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!!
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.
This question already has answers here:
How do I send a file as an email attachment using Linux command line?
(25 answers)
Closed 5 years ago.
I'm on a server running a Linux shell.
I need to mail a simple file to a recipient.
How to do this, prefereably using only the mail command?
UPDATE: got a good solution, using mutt instead:
$ echo | mutt -a syslogs.tar.gz admin#domain.org
Example using uuencode:
uuencode surfing.jpeg surfing.jpeg | mail sylvia#home.com
and reference article:
http://www.shelldorado.com/articles/mailattachments.html
Note:
you may apt install sharutils to have uuencode command
mail on every version of modern Linux that I've tried can do it. No need for other software:
matiu#matiu-laptop:~$ mail -a doc.jpg someone#somewhere.com
Subject: testing
This is a test
EOT
ctrl+d when you're done typing.
$ echo | mutt -a syslogs.tar.gz admin#domain.org
But it uses mutt, not mail (or mailx).
mailx might help as well. From the mailx man page:
-a file
Attach the given file to the message.
Pretty easy, right?
My answer needs base64 in addition to mail, but some uuencode versions can also do base64 with -m, or you can forget about mime and use the plain uuencode output...
FROM=me#mydomain.com
TO=someone#mydomain.com
SUBJECT="Auto emailed"
MIME="application/x-gzip" # Adjust this to the proper mime-type of file
FILE=somefile.tar.gz
ENCODING=base64
boundary="---my-unlikely-text-for-mime-boundary---$$--"
(cat <<EOF
From: $FROM
To: $REPORT_DEST
Subject: $SUBJECT
Date: $(date +"%a, %b %e %Y %T %z")
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="$boundary"
Content-Disposition: inline
--$boundary
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
This email has attached the file
--$boundary
Content-Type: $MIME;name="$FILE"
Content-Disposition: attachment;filename="$FILE"
Content-Transfer-Encoding: $ENCODING
EOF
base64 $FILE
echo ""
echo "--$boundary" ) | mail
mailx -a /path/to/file email#address
You might go into interactive mode (it will prompt you with "Subject: " and then a blank line), enter a subject, then enter a body and hit Ctrl+D (EOT) to finish.
mpack -a \
-s"Hey: might this serve as your report?" \
-m 0 -c application/x-tar-gz \
survey_results.tar.gz \
hesco#example.net
mpack and munpack work together with metamail to extend mailx
and make it useful with modern email cluttered with HTML markup and attachments.
Those four packages taken together will permit you to handle
any email you could in a GUI mail client.
Using ubuntu 10.4, this is how the mutt solution is written
echo | mutt -a myfile.zip -- admin#domain.org
There are a lot of answers here using mutt or mailx or people saying mail doesn't support "-a"
First, Ubuntu 14.0.4 mail from mailutils supports this:
mail -A filename -s "subject" email#example.com
Second, I found that by using the "man mail" command and searching for "attach"
The following is a decent solution across Unix/Linux installations, that does not rely on any unusual program features. This supports a multi-line message body, multiple attachments, and all the other typical features of mailx.
Unfortunately, it does not fit on a single line.
#!/bin/ksh
# Get the date stamp for temporary files
DT_STAMP=`date +'%C%y%m%d%H%M%S'`
# Create a multi-line body
echo "here you put the message body
which can be split across multiple lines!
woohoo!
" > body-${DT_STAMP}.mail
# Add several attachments
uuencode File1.pdf File1.pdf > attachments-${DT_STAMP}.mail
uuencode File2.pdf File2.pdf >> attachments-${DT_STAMP}.mail
# Put everything together and send it off!
cat body-${DT_STAMP}.mail attachments-${DT_STAMP}.mail > out-${DT_STAMP}.mail
mailx -s "here you put the message subject" nobody#test-address.com < out-${DT_STAMP}.mail
# Clean up temporary files
rm body-${DT_STAMP}.mail
rm attachments-${DT_STAMP}.mail
rm out-${DT_STAMP}.mail
On Linux I would suggest,
# FILE_TO_BE_ATTACHED=abc.gz
uuencode abc.gz abc.gz > abc.gz.enc # This is optional, but good to have
# to prevent binary file corruption.
# also it make sure to get original
# file on other system, w/o worry of endianness
# Sending Mail, multiple attachments, and multiple receivers.
echo "Body Part of Mail" | mailx -s "Subject Line" -a attachment1 -a abc.gz.enc "youremail#domain.com anotheremail#domain.com"
Upon receiving mail attachment, if you have used uuencode, you would need uudecode
uudecode abc.gz.enc
# This will generate file as original with name as same as the 2nd argument for uuencode.
I use mailutils and the confusing part is that in order to attach a file you need to use the capital A parameter. below is an example.
echo 'here you put the message body' | mail -A syslogs.tar.gz admin#domain.org
If you want to know if your mail command is from mailutils just run "mail -V".
root#your-server:~$ mail -V
mail (GNU Mailutils) 2.99.98
Copyright (C) 2010 Free Software Foundation, inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
With mailx you can do:
mailx -s "My Subject" -a ./mail_att.csv -S from=noreply#foo.com recipient#bar.com < ./mail_body.txt
This worked great on our GNU Linux servers, but unfortunately my dev environment is Mac OsX which only has a crummy old BSD version of mailx. Normally I use Coreutils to get better versions of unix commands than the Mac BSD ones, but mailx is not in Coreutils.
I found a solution from notpeter in an unrelated thread (https://serverfault.com/questions/196001/using-unix-mail-mailx-with-a-modern-mail-server-imap-instead-of-mbox-files) which was to download the Heirloom mailx OSX binary package from http://www.tramm.li/iWiki/HeirloomNotes.html.
It has a more featured mailx which can handle the above command syntax.
(Apologies for poor cross linking linking or attribution, I'm new to the site.)
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