cURL邮件附件(想象,.exe文件,.rar / .zip文件)

时间:2017-03-17 13:50:52

标签: file email curl exe attachment

我想知道我是否可以将不同类型的文件作为附件发送到电子邮件中。我只知道如何使用cUrl发送文本文件。有人能举例说明我如何实现目标吗?

这是我到目前为止所做的:

curl --url "smtps://smtp.gmail.com:465" --mail-from "mail.gaming@gmail.com" --mail-rcpt "mail2@gmail.com" --ssl --user "mail@gmail.com:password" --upload-file "C:\Folder\File.txt"

感谢您的所有努力!

1 个答案:

答案 0 :(得分:0)

您可以使用multipart/mixed内容传输文本正文和每个二进制附件。

这是一个文件模板,可用于显示文本文件并附加2个二进制文件:

From: Some Name <sender@gmail.com>
To: Some Name <receiver@gmail.com>
Subject: example of mail
Reply-To: Some Name <sender@gmail.com>
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="sender@gmail.com"
rtmp_to="receiver@gmail.com"
rtmp_credentials="sender@gmail.com: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

收到的电子邮件将包含inline text/plain个文档以及attachment类型的application/octet-stream文档:

enter image description here