Sending both a body and attachment using mail (Linux) - linux

I want to send an email from Linux with body and attachment. However, it only sends either one of the two. When I have both in the command, only the attachment is sent. I'm working with a log file as body and a CSV file as an attachment. I have tried the following;
mail -a "FROM: Name <name#email.com>" -s "subject of email" "recipient#email.com" -A /home/Export/attachment.csv <<< "$BODY_SUCCES"
For which the variable $BODY_SUCCESS is a log file. I have also tried making it work with mutt and mailx, but neither seemed to work, unfortunately. I can't figure out what's going wrong here. Can anyone help?

Related

Use Japanese characters in subject and attach file with mailx in RedHat Linux

I am trying to use the mailx command for sending email with attachment (zipped) and am facing two issues, below is the command I use:
(echo "$BODY"; UUENCODE $ZIP_FILE $ZIP_FILE) \
| mailx -s $SUBJECT_1 -r " " $SENDER $RECIPIENT
My email subject contains space and Japanese characters.
The variable $SUBJECT_1 has the following statement
Subject: [Budget] Subtype Error and some JAPANESE CHARECTERS
I get bet following error:
contains invalid character '\203'
Moreover for testing purpose I changed the statement of SUBJECT_1 to Test Message
SUBJECT_1="Test Message"
It worked, but I receive only Test instead of Test Message and in the mail I could see two more email ids in the To like Message#domain.com and -r#domain.com
I have not implemented the mail body yet, once subject issue fixed will implement the same in body because Body will also have Japanese characters.
Please help me with this error, how to resolve and what am i doing wrong
There's a list of things you need help with here, more than I want to to handle exhaustively on a sunny Saturday afternoon. But some hints.
Quote your variables.
"$SUBJECT_1" is a single string, whereas $SUBJECT_1 is a list of space-separated words. The second word is your email recipient, and subsequent options are also recipients.
Subject.
The basic idea is that you need to include encoding data in the subject, because email headers are only supposed to include 7-bit ASCII.
Here is a hint at how you put special characters in your Subject line.
Here is another hint.
Here is the RFC that describes in lurid detail what you need to do. Asking your favourite search engine for information about "utf8 email subject" and "rfc1522" is probably a good idea.
Email client.
Finally, rather than learning how to use MIME, consider using mutt instead of mailx to send your mail. Mutt has a -a option to add attachments, making it WAY easier than constructing your own headers and body, which I'm not even sure you'd be able to do with mailx in the first place.

Where are received mails by Mailcatcher stored?

I have installed postfix on my Mac with and changed the following parameters :
mydestination =
relayhost = 127.0.0.1:1025
I've modified mydestination to be blank so that whoever the mail is sent to like bellow in the Terminal it necessarily goes through the relayhost (even if it's sent to #localhost) and I can see it appear in my web-browser in Mailcatcher's tab :
echo "Body test" | mail -s "Subject test" test#dev.local
But here's the thing, when I receive the mail I do receive it instantly with the built-in websockets but if I do reload the page localhost:1080, emails are still there, meaning they must be stored somewhere.
After searching I found that the following URL redirects to a .json that contains received mails that are print out in Mailcatcher's tab :
localhost:1080/messages
But if I do a ls -l in my webroot directory, there's no messages.json that appears. And when you do click on the "Quit" button in Mailcatcher's, then relaunch it by doing mailcatcher and opening again localhost:1080 mails have disappeared...
I don't understand, is there a file that is deleted when you do click "Quit" and that I might haven't seen ?
Thanks for your help !
If you check the source on GitHub you will find something like this SQLite3::Database.new(":memory:", :type_translation => true).tap do |db| in mail.rb.
You may not have noticed, but MailCatcher uses SQLite, and the above tells you that it is configured to run in memory. So, when turning off MailCatcher all the rows that are contained in this database are lost.

Perl - How to send local mail?

I would like to integrate the following terminal command into a Perl script.
Terminal command:
mutt -s "User Monitoring" -a "/home/mipa/Documents/System_Monitoring/protocol_name.csv" -- mipa#localhost.localdomain
The command sends local mail containing a file attachment to a user on the same system.
I have a small problem with the command though. It does seem to require more user interaction than just the command listed here. The command requires the user to follow a menu to confirm the values and hit the "y" key to send.
My question here is two-folded. Is there a similar mail command that does not require user interaction and works by just following a single command with predefined flags? And how would I integrate this command into a Perl script where I would be able to choose the file name, and the receiving user followed by issuing the command?
Any guidance regarding a possible solution is highly appreciated.
there are a few ways to send command line emails in Linux: How do I send a file as an email attachment using Linux command line?
why is the -- in your command? that maybe confusing mutt.
https://unix.stackexchange.com/questions/108916/automatically-attach-a-file-to-a-mail-with-mutt has a few more suggestions for sending mail with mutt.
I prefer to use MIME::Lite to send emails, rather than spawning an external command, which avoids the problems you are having. MIME::Lite is able to handle sending emails with attachments.
Here is a quick example:
#!/usr/bin/perl
use strict;
use MIME::Lite;
my $msg = MIME::Lite->new(
To => 'foo.bar#foobar.com',
Subject => 'Test message with attachments',
Type => 'multipart/mixed'
);
$msg->attach(
Type => 'TEXT',
Data => "Here's the file you wanted"
);
$msg->attach(
Type => 'image/png',
Path => 'somefile.png',
Filename => 'somefile.png',
Disposition => 'attachment'
);
$msg->send();
This would send a message containing a small amount of text and a single attachment.
There are a lot more examples given in the POD for MIME::Lite.

Create serial mail on linux commandline

I would like to generate serial emails on the linux commandline. Assume I have a file indicating mail address, subject and message text in columns on separate lines for each recipient. I.e.
recipient1#mail.com subject1 text1
recipient2#mail.com subject2 text2
...
The script should use standard commands as I intend to send it to colleagues who should create some emails for me.
The loop over the lines could be with xargs ... Can I use the commandline tool mail?
It is important that the mails are not send immediately. Ideally it creates files for import in the users preferred mail client. So that senders can check the mails before they submit.
I also would like to be able to add attachments to the Mails.
I tried e.g.
function mail_kmail {
kmail -s "$2" --body "$3" --attach "$4" --composer "$1"
}
function mail_thunderbird {
thunderbird -compose "to='$1',subject='$2',body='$3',attachment='$4'"
}
and reading the input data from file with
while read recipient subject body attach $file
do
mail_kmail "$recipient" "$subject" "$body" "$attach";
done
but this will work only if my colleagues installed and set up either of these mail clients.
I found this (closed) related question:
How can i send automated email in linux? .
You can use mutt to send the emails, here is an example:
echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient#domain.com
Since it's hard to know what you are trying to achieving here, you can even create a configuration file to be used, but you'll to investigate more or give more details about your use case.

How do I send an email using awk to a list of email addresses in a text file?

If I have a text file with a list of email addresses, how can I go through the list and send an email to each of those email addresses with a text file as the message.
I.e. I want to take in an email as a variable so I can execute this command:
mail -s "Welcome" email#address.com < welcome.txt
for example you have a mails_addresses.txt file with one address per line like that:
email1#mail.com
email2#mail.com
email3#mail.com
In case you have another complex structure which you need to parse with for example awk you should to show it us.
So you need just to write a loop which will read it and send it to mail command:
while read MAIL
do
mail -s "Welcome" "$MAIL" < welcome.txt
done < mails_addresses.txt
You can do this even without awk:
cat users-list | while read addr
do
mail -s "Welcome" "$addr" < welcome.txt
done

Resources