Reputation: 15
I was wondering if I can send different type of files to an email as an attachments. I only know how to send a text file using cUrl. Could someone give me some examples of how can I accomplish my goal ?
This is what I have so far :
curl --url "smtps://smtp.gmail.com:465" --mail-from "[email protected]" --mail-rcpt "[email protected]" --ssl --user "[email protected]:password" --upload-file "C:\Folder\File.txt"
Thank you for all the effort !
Upvotes: 1
Views: 4637
Reputation: 45432
You can use multipart/mixed
content to transmit your text body and each of your binary attachments.
Here is a template of file you could use to display a text file and attach 2 binary files :
From: Some Name <[email protected]>
To: Some Name <[email protected]>
Subject: example of mail
Reply-To: Some Name <[email protected]>
Cc:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="MULTIPART-MIXED-BOUNDARY"
--MULTIPART-MIXED-BOUNDARY
Content-Type: multipart/alternative; boundary="MULTIPART-ALTERNATIVE-BOUNDARY"
--MULTIPART-ALTERNATIVE-BOUNDARY
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
Content-Disposition: inline
This is an email example. This is text/plain content inside the mail.
--MULTIPART-ALTERNATIVE-BOUNDARY--
--MULTIPART-MIXED-BOUNDARY
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="file.rar"
<HERE BASE64 ENCODED RAR FILE>
--MULTIPART-MIXED-BOUNDARY
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="file.zip"
<HERE BASE64 ENCODED ZIP FILE>
--MULTIPART-MIXED-BOUNDARY--
Note that the binary files are encoded in base64 and are transmitted as attachment
.
Here is an example of building this file and send the email with a bash
script :
#!/bin/bash
rtmp_url="smtp://smtp.gmail.com:587"
rtmp_from="[email protected]"
rtmp_to="[email protected]"
rtmp_credentials="[email protected]:secretpassword"
file_upload="data.txt"
echo "From: Some Name <$rtmp_from>
To: Some Name <$rtmp_to>
Subject: example of mail
Reply-To: Some Name <$rtmp_from>
Cc:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=\"MULTIPART-MIXED-BOUNDARY\"
--MULTIPART-MIXED-BOUNDARY
Content-Type: multipart/alternative; boundary=\"MULTIPART-ALTERNATIVE-BOUNDARY\"
--MULTIPART-ALTERNATIVE-BOUNDARY
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
Content-Disposition: inline
This is an email example. This is text/plain content inside the mail.
--MULTIPART-ALTERNATIVE-BOUNDARY--
--MULTIPART-MIXED-BOUNDARY
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"file.rar\"" > "$file_upload"
# convert file.rar to base64 and append to the upload file
cat file.rar | base64 >> "$file_upload"
echo "--MULTIPART-MIXED-BOUNDARY
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"file.zip\"" >> "$file_upload"
# convert file.zip to base64 and append to the upload file
cat file.zip | base64 >> "$file_upload"
# end of uploaded file
echo "--MULTIPART-MIXED-BOUNDARY--" >> "$file_upload"
# send email
echo "sending ...."
curl -s "$rtmp_url" \
--mail-from "$rtmp_from" \
--mail-rcpt "$rtmp_to" \
--ssl -u "$rtmp_credentials" \
-T "$file_upload" -k --anyauth
res=$?
if test "$res" != "0"; then
echo "sending failed with: $res"
else
echo "OK"
fi
The received email will have the inline
text/plain
document and both of attachment
document of type application/octet-stream
:
Upvotes: 1