Mail files from linux

How to Send An Email With File Attachment from Command Line

The key to becoming an advanced Linux user is to use more of the command line and less of the GUI; more of the keyboard and less of the mouse! As the diaspora of Linux command-line tools grows, not only administrative but several non-administrative, in fact, crucial day-to-day tasks, are performed using the command line.

In this article, we will learn how to send an email with a file attachment using the mail command in Linux.

Pre-Requisites

You must have already configured your Email with SMTP in your Linux machine. This Email and server will be made use of by the ‘Mail‘ program which we will learn about today.

Sending an Email from Command Line in Linux

The program ‘mail’ can be used to send an Email from the command line, along with file attachments with the Email. This program is not available by default, and can be installed in Debian and Red Hat-based distributions using:

$ sudo apt install mailutils [On Debian/Ubuntu/Mint] $ sudo dnf install mailx [On RedHat/CentOS/Fedora]

The syntax to send an Email using ‘mail’ is as follows:

You can see that we are using the echo command to output the message body and redirecting this output to the ‘mail’ command. This is because the ‘mail’ command reads message body input from the standard input.

Sending an Email with File Attachment from Command Line

Similarly, to attach a file with the mail, the argument ‘-A’ can be used:

Send an Email with File Attachement from Terminal

Sending an Email to Multiple Recipients from Command Line

To send the email to multiple recipients, simply specify the multiple Email IDs separated by a comma.

$ echo «Email Message Body» | mail -s «Subject of the Email» [email protected];[email protected],[email protected] -A

Send Email to Multiple Recipients

To include a text file as the message body of the Email, instead of using echo for the same, you can redirect the text of a file to the command as shown below:

Conclusion

Today we have seen a way to send an Email from the Linux command line along with an attachment. There are some other programs like ‘mutt‘ and ‘Sendmail‘ which are similar to ‘mail‘ and can be used for the same purpose.

Читайте также:  Alias in linux bash

Thanks for reading and let us know your thoughts or questions in the comments below!

Источник

How can I send a file from Linux to email? [duplicate]

How can I send a zip file (~600MB) to an email account? What is the command line should be used in bash? What if I want to FTP the files to a server automatically?

I wanted to close because it’s a duplicate, but I missed the link and now I cannot change it. SO needs UI fix 😉

3 Answers 3

Well, in my opinion 600Mb is too large for e-mail, since some clients and servers will choke on that size. But that’s your choice, and if you own the webserver then obviously you can do what you like.

The unix command mail can be used (when configured) to send emails.

You might also want to look at perl’s Net::SMTP module, which is for this sort of thing.

Most linux distributions contain a mail command (from the mailx package). You can attach a file from the command line using the -a option:

mail -s 'file attached' -a /path/to/file someone@example.com 

That said, most mail systems won’t be happy with 600MB attachments.

The ncftp package has a number of commands that may be useful for automated transferring of files over FTP, in particular the ncftpput command (see the manpages for more information).

Depending on where you are sending the file, if the other end supports ssh, it might be better to use tools like scp or ssh and rsync. With public key authentication, you don’t even have to worry about embedding a password anywhere.

If you are doing backups, consider a tool like Duplicity (but not for a full zip file as it loses most of its advantages) as it supports a number of protocols, performs compression on-the-fly and can perform incremental backups. Oh, and the backups are encrypted and digitally signed to ensure their integrity.

Источник

How can I email an attachment from the command line

Is it possible to email attachments from the command line? If possible, I’d like something as simple as:

mail -a myfile.txt -t me@example.com -s "Here's my file" 

The default ubuntu command line mail user-agent mail (aka mailx ) is MIME ignorant therefore cannot handle attachments in any way that isn’t 1980ish. This is a good question, I’m looking for the canonical Canonical MUA; more to come.

Читайте также:  Linux audio file info

7 Answers 7

Of all the mail user agents in the Ubuntu repository, it appears that mutt is the command-line MUA that is blessed with Long Term Support.

According to the manual, you can do something exactly like:

mutt -a myfile.txt -s "Here's my file" -- me@example.com 

except it won’t go anywhere since one also needs a Mail Transfer Agent. Popular ones are:

and the only ones that Canonical seems to support are postfix (thanks for the correction Steve) and exim4.

One could also say that xdg-email is also a proper Ubuntu MUA, but it is a bare-bones front end which only executes your preferred MUA on your behalf.

If you’d like advice on which MTA might be suitable for your use, perhaps open another question here.

Great question, thanks. You prompted me to get mail working on my box. I used nullmailer MTA to blindly relay to my ISP, and the example mutt command worked as advertised. Incidentally nullmailer has a fake sendmail front-end so that most programs that expect to talk to sendmail get shuffled off to a real MTA with zero effort from me.

msw, I’m not quite sure why you’re claiming exim4 is the only one canonical supports, postfix is also in main and is what is enabled if you select the Mail Server task selection from the ubuntu-server install. I believe exim4is the debian default MTA, and in the past, packaging preferences may have caused it to be pulled in by default, but for a while now, postfix is definitely the preferred MTA in Ubuntu.

Oh, and also note that if you went with bsd mail/mailx as your (non-mime aware) MUA, you would still need an MTA of some kind to actually send the mail. But mutt’s better for you anyway. 🙂

I had bad trouble with sending attachment files, too. When I sent an email without attachment, it was successful but was not with attachment. This problem was existent with sendemail , mutt , mail , mailx , uuencode commands.

Fortunately, It was solved funnily. I use Gmail for sending email. You can configure your gmail for sending emails via commands in terminal as declared at http://www.linuxandlife.com/2013/01/send-email-from-linux-terminal.html .

You can send a text email using:

mail -s "hello" RECEIVE@mail.com < /home/masoud/YOURFILE.txt 

but you can't send same file as attachment as below:

mail -s "hello" RECEIVE@mail.com -a /home/masoud/YOURFILE.txt 
mail -s "hello" -a /home/masoud/YOURFILE.txt RECEIVE@mail.com 

Finally, I understood that only this format could send an attachment:

echo "your message here" | mail -s "title" -a /home/masoud/YOURFILE.txt RECEIVE@mail.com 

Funnily, the difference is existence of the "echo" command.

Читайте также:  Linux лучшие базы данных

Update: 201808

Seems that the -a option has been changed with Uppercase -A for attachment now. The -a seems to be for changing header according to doc

Excellent! Thanks. I was trying to send my .zshrc file to someone and though I use mail a lot to send quick messages, trying to sent an attachment wasn't working. I just needed confirmation that it was possible to realize that argument order is very important. This worked for me: mail -s "zshrc" -a ~/.zshrc username@example.com . of course I still had to type the message body and hit ctrl+d on the last blank line.

Using mail username@example.com -a ~/.zshrc did "work" in that it sent the message but it did not send the file. Had I tried mail username@example.com -a ~/.zshrc -s 'zshrc' it would have been obvious that the params after the recipient address were being ignored.

@Masoud lnk is broken. wanted to update it, but you know better what documentation you provided so I let it to you

I found that the command and parameters have been changed recently.

If your want to send your attachments with this command:

mail -s "your subject" you@gmail.com -A /your/attachment/absolute/path < /home/you/message.txt 

The option attachment should be -A which is CAPITAL A .

You may want to send a file from the shell, but otherwise use Thunderbird.

In this case, try thunderbird -remote . is useful - assuming thunderbird is usually running:

The command opens a mail compose window of a running thunderbird instance.
The "From" address is your default address configured in thunderbird.
Also, the existing account settings are used, there is no separate setup needed.

For a mail adressed to you@example.com , with subject "S", body "B", and an attachment /some/absolute/file.txt , the command is

thunderbird -remote "xfeDoCommand(composeMessage,subject='S',to='you@example.com',body='B',attachment='/some/absolute/file.txt')" 

The attached file needs to be given by an absolute path, which is tedious in practice. That can be handeled by using readlink -f to resolve relative paths:

thunderbird -remote "xfeDoCommand(composeMessage,subject='S',to='you@example.com',body='B',attachment='$(readlink -f file.txt)')" 

Also, the command is to long. Use a shell script or shell function, with four arguments:

With this function, the command becomes readable:

thunderbird-compose 'Some Subject' test@example.com 'Body of message' file1 

will open a thunderbird "Write" window with the attachment, and From, To, Subject, and body text filled in. It can be edited before sending it.

Источник

Оцените статью
Adblock
detector