2017-03-17 61 views
1

我想知道是否可以將不同類型的文件作爲附件發送到電子郵件。我只知道如何使用cUrl發送文本文件。有人能給我一些例子,說明我怎樣才能實現我的目標?cURL Mail Attachment(Imagines,.exe文件,.rar/.zip文件)

這是我到目前爲止有:

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" 

感謝您所有的努力!

回答

0

您可以使用multipart/mixed內容傳輸您的文本正文和每個二進制附件。

這是文件的模板,你可以用它來顯示一個文本文件,並附加2個二進制文件:

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-- 

注意,二進制文件的base64編碼和attachment傳輸。
下面是建立這個文件的一個例子,用bash腳本發送電子郵件:

#!/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 

收到的電子郵件將有inlinetext/plain文件和兩個application/octet-stream類型的attachment文件:

enter image description here