2017-03-17 1 views
1

전자 메일에 첨부 파일로 다른 유형의 파일을 보낼 수 있는지 궁금합니다. 나는 cUrl을 사용하여 텍스트 파일을 보내는 방법 만 알고 있습니다. 누군가 내 목표를 어떻게 달성 할 수 있는지에 대한 몇 가지 예를 들려 줄 수 있습니까?cURL 메일 첨부 파일 (상상해보십시오 .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 콘텐츠를 사용하여 텍스트 본문과 각 이진 첨부 파일을 전송할 수 있습니다. 바이너리 파일을 base64로 인코딩하고 attachment로 전송되는 것을

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

참고 : 여기에

는 2 개 바이너리 파일을 텍스트 파일을 표시하고 연결하는 데 사용할 수있는 파일의 템플릿입니다.
여기 이 파일을 작성하는 예이며 bash 스크립트로 이메일을 보내

(가) 이메일이 inline text/plain 문서 및 유형 application/octet-streamattachment 문서를 모두해야합니다받은
#!/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 

:

enter image description here