I can send email from my gmail account by ssmtp in Linux now. But how can I attach files to the email?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
echo -e "to: <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed9f888e88849b889fad8982808c8483c3998189">[email protected]</a>nsubject: subjectn"| (cat - && uuencode /path/to/attachment attachment.name) | ssmtp <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9bbacaaaca0bfacbb89aea4a8a0a5e7aaa6a4">[email protected]</a>
This solution does not depend on mutt.
Method 2
To send an attachment, you need to encode the message using MIME.
You could use Mutt
mutt -s SUBJECT -a ATTACHMENT_FILE_1 ATTACHMENT_FILE_2 -- EMAIL_ADDRESS < MESSAGE_FILE
or mpack
mpack -s SUBJECT -D MESSAGE_FILE ATTACHMENT_FILE EMAIL_ADDRESS
See also:
- How do I send a file as an email attachment using Linux command line?
- How to send mail from the command line?
- Sending email with attachments on UNIX systems
Method 3
$ echo -e "to: <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="54263137313d22312614303b39353d3a7a203830">[email protected]</a>nsubject: testn"| (cat - && uuencode /path/to/file file.name) | ssmtp <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c3f292228293e0c2b212d2520622f2321">[email protected]</a>
Provided that SSMTP is configured, and you’ve verified that messages without attachments are reaching their destination and does not depend on mutt.
Method 4
$ sudo apt-get install uudeview $ echo -e "From: <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c51455d58584e594f4f7c51455853515d5552125f5351">[email protected]</a>nTo: <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f76607a7d6e6b6b7d6a7c7c4f76607a7d6b60626e6661216c6062">[email protected]</a>nSubject: mySubjectnnBody-Text"|uuenview -a -bo MyAttachment|sendmail -t
Method 5
another alternative to uuencode is to use base64 commend instead ->
cat msg_source.txt | (cat - base64 && attachment.bin) | ssmtp -vvvv <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d28202c24210d2922202c2423632e2220">[email protected]</a>
where msg_source.txt contains header tags, like To:, From:, Subject:, Content-Type:, etc
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0