簡體   English   中英

curl:發送帶有嵌入圖像和附件的 html email

[英]curl : send html email with embedded image and attachment

我的目標是使用curl和帶有嵌入圖像的 html 主體發送 email,例如:

電子郵件模板圖像

我像這樣發送 email:

curl "smtp://smtp.gmail.com:587" -v \
     --mail-from "sender@gmail.com" \
     --mail-rcpt "receiver@gmail.com" \
     --ssl -u sender@gmail.com:secretpassword \
     -T "message.txt" -k --anyauth

我的message.txt看起來像:

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/html; charset=utf-8
Content-Transfer-Encoding: base64
Content-Disposition: inline

PGh0bWw+Cjxib2R5PgogICAgPGRpdj4KICAgICAgICA8cD5IZWxsbywgPC9wPgogICAgICAgIDxw
PlBsZWFzZSBzZWUgdGhlIGxvZyBmaWxlIGF0dGFjaGVkPC9wPgogICAgICAgIDxwPkFkbWluIFRl
YW08L3A+CiAgICAgICAgPGltZyBzcmM9ImFkbWluLnBuZyIgd2lkdGg9IjE1MCIgaGVpZ2h0PSI1
MCI+CiAgICA8L2Rpdj4KPC9ib2R5Pgo8L2h0bWw+Cg==
--MULTIPART-ALTERNATIVE-BOUNDARY--
--MULTIPART-MIXED-BOUNDARY

解碼后的 html 為:

<html>
<body>
    <div>
        <p>Hello, </p>
        <p>Please see the log file attached</p>
        <p>Admin Team</p>
        <img src="admin.png" width="150" height="50">
    </div>
</body>
</html>

How can I embed admin.png in this html and attach another file log.txt to this email using curl and bash ?

我想出的解決方案是對所有附件(圖像和文本文件)進行 base64 編碼,並將它們直接包含在multipart/mixed正文中的上傳文件中,例如:

--MULTIPART-MIXED-BOUNDARY
Content-Type: image/png
Content-Transfer-Encoding: base64
Content-Disposition: inline
Content-Id: <admin.png>
iVBORw0KGgoAAAANSUhEUgAAAIAAAACgCAIAAABL8POqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA
B3RJTUUH4AQNDwEVouBdqAAAG2xJREFUeNrtfX9oHFe25jdDBU5BG25BG7pABhXEkDJjSIsYIs1m
WbfJA8ubhcjjgdiTQNJOYCInj0RKYGIl8CbyPF4iZSCxEkgsB5LIgWQlL2Pcfow3bdgw0mMzox6e
....


--MULTIPART-MIXED-BOUNDARY
Content-Type: text/plain
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=log.txt
c29tZSBsb2cgaW4gYSB0eHQgZmlsZSB0byBhdHRhY2ggdG8gdGhlIG1haWwK


--MULTIPART-MIXED-BOUNDARY--

Content-Id標頭用於標識可以在 html 中引用的資源: cid: like:

<img src="cid:admin.png" width="150" height="50">

這是一個完整的bash示例,用於發送帶有admin.png嵌入圖像和log.txt的 html 電子郵件:

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

# html message to send
echo "<html>
<body>
    <div>
        <p>Hello, </p>
        <p>Please see the log file attached</p>
        <p>Admin Team</p>
        <img src=\"cid:admin.png\" width=\"150\" height=\"50\">
    </div>
</body>
</html>" > message.html

# log.txt file to attached to the mail
echo "some log in a txt file to attach to the mail" > log.txt

mail_from="Some Name <$rtmp_from>"
mail_to="Some Name <$rtmp_to>"
mail_subject="example of mail"
mail_reply_to="Some Name <$rtmp_from>"
mail_cc=""

# add an image to data.txt : 
# $1 : type (ex : image/png)
# $2 : image content id filename (match the cid:filename.png in html document)
# $3 : image content base64 encoded
# $4 : filename for the attached file if content id filename empty
function add_file {
    echo "--MULTIPART-MIXED-BOUNDARY
Content-Type: $1
Content-Transfer-Encoding: base64" >> "$file_upload"

    if [ ! -z "$2" ]; then
        echo "Content-Disposition: inline
Content-Id: <$2>" >> "$file_upload"
    else
        echo "Content-Disposition: attachment; filename=$4" >> "$file_upload"
    fi
    echo "$3

" >> "$file_upload"
}

message_base64=$(cat message.html | base64)

echo "From: $mail_from
To: $mail_to
Subject: $mail_subject
Reply-To: $mail_reply_to
Cc: $mail_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/html; charset=utf-8
Content-Transfer-Encoding: base64
Content-Disposition: inline

$message_base64
--MULTIPART-ALTERNATIVE-BOUNDARY--" > "$file_upload"

# add an image with corresponding content-id (here admin.png)
image_base64=$(curl -s "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_116x41dp.png" | base64)
add_file "image/png" "admin.png" "$image_base64"

# add the log file
log_file=$(cat log.txt | base64)
add_file "text/plain" "" "$log_file" "log.txt"

# add another image 
#image_base64=$(curl -s "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_116x41dp.png" | base64)
#add_file "image/png" "something.png" "$image_base64"

# 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

我真的很想通過請求,CURL和BeautifulSoup來跟蹤您以前的舊報廢表單wsj中的文章 是否可以要求您更新答案? 非常感謝。 已經研究了許多小時,但無法弄清楚該怎么做。

另外,您的代碼示例有點誤導性強,實際上它不起作用。 正如您所說,您只需將每個部分括在一個邊界中,在您的情況下,分隔符是:

“--MULTIPART-MIXED-BOUNDARY”。

盡管如此,它可以是任何其他用戶定義的字符串。 您正在嵌套多個邊界類型,這是不必要的。 HTML 內容不需要是 base64 編碼的,如果你願意,它可以被編碼。

每個分隔符前后都需要一個空行。

我建議查看 W3 論文和示例: https : //www.w3.org/Protocols/rfc1341/7_2_Multipart.html

這是您可以使用的 shell 腳本。 我大量來自https://blog.ambor.com/2021/08/using-curl-to-send-e-mail-with.html

密碼可以存儲在 .netrc 文件中,如下所述: https ://everything.curl.dev/usingcurl/netrc 但我沒有嘗試過。

只需確保您需要的圖像使用 base64 編碼嵌入到 HTML 中。

#!/bin/bash

declare -a VOPTS;
declare -a HOPTS;

sesAccess="sender.account.authentication@email.id" ;
sesSecret="sender.account.passwordXXXXXX";
sesFromName="Sender Full Name";
sesFromAddress='<sender@email.id>';
sesToName="Recipient Full Name";
sesToAddress="<recepient@email.id>"
sesSubject="Email Subject Line";
sesSMTP='mail.server.fqdn';
sesPort='465';
sesMessage=$'Test of line 1\nTest of line 2'
sesFile="$1"; # attachment is first argument
sesHTMLbody="/path/to/html/file.html"; # content of this file will be used to create HTML body

sesMIMEType=`file --mime-type "$sesFile" | sed 's/.*: //'`;
# sesMIMEType=`file -b --mime-type "$sesFile"`;

VOPTS=();
HOPTS=();

#Curl Options
VOPTS+=("-v");
VOPTS+=("--url"); VOPTS+=("smtps://$sesSMTP:$sesPort"); 
VOPTS+=("--ssl-reqd")
VOPTS+=("--user"); VOPTS+=("${sesAccess}:${sesSecret}"); 
VOPTS+=("--mail-from"); VOPTS+=("${sesFromAddress}");
VOPTS+=("--mail-rcpt"); VOPTS+=("${sesToAddress}");

#Header Options
HOPTS+=("-H"); HOPTS+=("Subject: ${sesSubject}");
HOPTS+=("-H"); HOPTS+=("From: ${sesFromName} ${sesFromAddress}"); 
HOPTS+=("-H"); HOPTS+=("To: ${sesToName} ${sesToAddress}"); 

curl "${VOPTS[@]}" -F '=(;type=multipart/mixed' -F "=<$sesHTMLbody;type=text/html;encoder=base64" -F "file=@$sesFile;type=$sesMIMEType;encoder=base64" -F '=)' "${HOPTS[@]}"

exit

@bertrand-martel 的答案在我運行 Ubuntu 22.04 並提交到我網絡中的后綴服務器的系統上大部分是正確的。 html email 消息通過就好了,我有正確命名的空附件。 我必須編輯他定義的 add_file function 並在最后的 $3 之前添加一個空行。 如果 Content-Disposition: 和 base64 編碼之間沒有空行,email 將無法工作。 我還必須在 --MULTIPART-ALTERNATIVE-BOUNDARY-- 之后插入一個空行。 一旦我進行了這些更改,html 消息、嵌入的圖像和附件都正確出現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM