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:
sendmail
is the command to send the mail.-t
tells sendmail to read the message from the standard input.-i
tells sendmail to ignore dot (.) at the end of each line.-f
specifies the sender's email address.[email protected]
is the sender's email address.[email protected]
is the recipient's email address.message.txt
is the file containing the message and attachment.
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:
To
andSubject
specify the recipient and subject of the mail.Content-Type
specifies the type of the message body and attachment.Content-Disposition
specifies the disposition of the attachment.[attachment contents]
contains the actual attachment data.
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.