How to send mail with attachment using sendmail

To send a mail with an attachment using sendmail, you can use the following command:

sendmail -t -i -f [email protected] [email protected] < message.txt

Here:

The message.txt file should contain the following format:

To: [email protected]
Subject: Your Subject
Content-Type: text/plain; charset=utf-8

Your message body

Content-Type: application/octet-stream; name="attachment_name"
Content-Disposition: attachment; filename="attachment_name"

[attachment contents]

Here:

For example, if you want to send a mail with an attachment named example.txt, you can create a file named message.txt with the following content:

To: [email protected]
Subject: Your Subject
Content-Type: text/plain; charset=utf-8

Your message body

Content-Type: application/octet-stream; name="example.txt"
Content-Disposition: attachment; filename="example.txt"

[attachment contents]

Then, you can use the following command to send the mail:

sendmail -t -i -f [email protected] [email protected] < message.txt

Make sure to replace [email protected], [email protected], and [attachment contents] with your actual email addresses and attachment data.

Note: The [attachment contents] part should contain the actual contents of the attachment file. You can use the base64 command to encode the attachment file and include it in the message.txt file.

For example, if you have a file named example.txt with the contents Hello World, you can encode it using the following command:

base64 example.txt

This will output the base64 encoded contents of the file. You can then include this output in the message.txt file.

For example:

To: [email protected]
Subject: Your Subject
Content-Type: text/plain; charset=utf-8

Your message body

Content-Type: application/octet-stream; name="example.txt"
Content-Disposition: attachment; filename="example.txt"

SGVsbG8sIFdvcmxkIQ==

Make sure to replace SGVsbG8sIFdvcmxkIQ== with the actual base64 encoded contents of your attachment file.