2017-03-17 3 views
1

Je me demandais si je pouvais envoyer différents types de fichiers à un courriel en pièce jointe. Je sais seulement comment envoyer un fichier texte en utilisant cUrl. Quelqu'un pourrait-il me donner quelques exemples de comment puis-je accomplir mon objectif?cURL Mail Attachment (Imagines, fichier .exe, fichiers .rar/.zip)

C'est ce que j'ai jusqu'à présent:

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" 

Merci pour tous les efforts!

Répondre

0

Vous pouvez utiliser le contenu multipart/mixed pour transmettre votre corps de texte et chacune de vos pièces jointes.

Voici un modèle de fichier que vous pouvez utiliser pour afficher un fichier texte et joindre 2 fichiers binaires:

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

Notez que les fichiers binaires sont codés en base64 et sont transmises sous forme attachment.
Voici un exemple de la construction de ce fichier et envoyer l'e-mail avec un script 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 

Le reçu email aura le inlinetext/plain document et les deux attachment documents de type application/octet-stream:

enter image description here