使用带有 uuencode 的 cat 通过 unix 发送电子邮件
sending email through unix by using cat with uuencode
这里我在 sun Solaris 10
上工作,我之前尝试过发送带附件和不带附件的普通电子邮件,它对我来说工作正常,但现在我正在尝试 cat-file 元素和在同一封电子邮件中发送附件,但它不起作用
这是我的 result.txt 文件正文:
Total Number of Files = 8
Total Number of Fixed Files = 4
Total Number of Notification Files = 4
Total Number of Courrpted Files = 0
Total Execution Time = 3 Seconds.
这是我的 data.txt 文件正文:
Notification Files :
file number 1
file number 2
unix 命令:
RECEIPIENTS="mm@gmail.com"
SUBJECT="!! testtt !!"
(cat Results.txt ; uuencode Data.txt Data.txt) | mailx -s "$SUBJECT" -c mm@gmail.com,dd@gmail.com -r zz@gmail.com $RECEIPIENTS
这是我发送邮件后得到的:
Total Number of Files = 8
Total Number of Fixed Files = 4
Total Number of Notification Files = 4
Total Number of Courrpted Files = 0
Total Execution Time = 3 Seconds.
begin 777 Data
M#0I.;W1I9FEC871I;VX@1FEL97,@.B -"@T*,2U#1$M(34LU2D]21DPP,#0R
M,@T*,BU#1$Q454]-2D]21DPP-S$Q- T*,RU#1$Q454]-2D]21DPP-S$Q-@T* M-"U#1$Q604Q-2D]21DPP-#0U, T*#0I&:7AE9"!&:6QE<R Z( T*#0HQ+4-$ M051'2SE*3U)&3# P,3$T#0HR+4-1'2SE*3U)&3# P,3$U#0HS+4-[=13=]TA.
C0U5*3U)&3#,Q.3DT#0HT+4-[=13=]TA.0U5*3U)&3#,Q.3DU#0HU
end
注意:如果我使用 echo
或 echo -e
或使用没有 cat 的 uuencode 附加文件,它工作正常,只有当我使用 cat
和 uuencode
时才会出现问题
在上个千年的某个时候 uuencode
很有意义,但在这个时代你真的应该使用 MIME。
这是一个简单的 Python 3 脚本,它将来自标准输入的消息与附件组合成一个有效的 RFC822 消息,您可以使用 sendmail
或类似的方式发送该消息。它非常接近于 Python documentation's examples.
#!/usr/bin/env python3
from email.message import EmailMessage
import sys
msg = EmailMessage()
msg['From'] = sys.argv[1]
msg['To'] = sys.argv[2]
msg['Subject'] = sys.argv[3]
# Don't mess with the preamble, the documentation is wrong to suggest you do
msg.set_content(''.join([line for line in sys.stdin]))
with open(sys.argv[4], 'r') as attachment:
msg.add_attachment(attachment.read(), maintype='text', subtype='plain')
print(msg.as_string())
要在您的示例中使用它,
# If `sendmail` is not in your `PATH`, maybe add it.
# For example, if you have it in `/usr/lib/sendmail`
# and `/usr/lib´ is not in your `PATH`, you can add it with
#PATH=$PATH:/usr/lib
# Don't use upper case for private variables; check spelling of "recipients"
# To demo an example of sending to multiple addresses, adds a second recipient
recipients="mm@gmail.com,another@example.com"
subject="!! testtt !!"
python3 path/to/script.py "zz@gmail.com" "$recipients" "$subject" Data.txt <Results.txt |
sendmail -oi -t
您可以省略通向 sendmail
的管道以查看消息的外观。它不是在消息 body 中轻击 uuencode
,而是创建一个包含两部分的多部分 MIME 消息,其中一部分是内联的并包含来自标准输入的文本,另一部分被正确标记为附件.它有点笨重,但我在这里包含一个示例只是为了向您展示。
From: zz@gmail.com
To: mm@gmail.com,another@example.com
Subject: !! testtt !!
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="===============2189027902917283968=="
--===============2189027902917283968==
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Total Number of Files = 8
Total Number of Fixed Files = 4
Total Number of Notification Files = 4
Total Number of Courrpted Files = 0
Total Execution Time = 3 Seconds.
--===============2189027902917283968==
Content-Type: text/plain
Content-Transfer-Encoding: base64
MIME-Version: 1.0
Content-Disposition: attachment
Tm90aWZpY2F0aW9uIEZpbGVzIDogCmZpbGUgbnVtYmVyIDEKZmlsZSBudW1iZXIgMgo=
--===============2189027902917283968==--
在这个特定的例子中,附件被不必要地 base64
编码了;如果你绝对需要 human-readable,你可以调整 Python 代码(但 uuencode
也不是)。
(另外,不知道为什么 body 部分有一个单独的 MIME-Version:
header -- 这似乎是一个错误。)
这里我在 sun Solaris 10
上工作,我之前尝试过发送带附件和不带附件的普通电子邮件,它对我来说工作正常,但现在我正在尝试 cat-file 元素和在同一封电子邮件中发送附件,但它不起作用
这是我的 result.txt 文件正文:
Total Number of Files = 8
Total Number of Fixed Files = 4
Total Number of Notification Files = 4
Total Number of Courrpted Files = 0
Total Execution Time = 3 Seconds.
这是我的 data.txt 文件正文:
Notification Files :
file number 1
file number 2
unix 命令:
RECEIPIENTS="mm@gmail.com"
SUBJECT="!! testtt !!"
(cat Results.txt ; uuencode Data.txt Data.txt) | mailx -s "$SUBJECT" -c mm@gmail.com,dd@gmail.com -r zz@gmail.com $RECEIPIENTS
这是我发送邮件后得到的:
Total Number of Files = 8
Total Number of Fixed Files = 4
Total Number of Notification Files = 4
Total Number of Courrpted Files = 0
Total Execution Time = 3 Seconds.
begin 777 Data
M#0I.;W1I9FEC871I;VX@1FEL97,@.B -"@T*,2U#1$M(34LU2D]21DPP,#0R
M,@T*,BU#1$Q454]-2D]21DPP-S$Q- T*,RU#1$Q454]-2D]21DPP-S$Q-@T* M-"U#1$Q604Q-2D]21DPP-#0U, T*#0I&:7AE9"!&:6QE<R Z( T*#0HQ+4-$ M051'2SE*3U)&3# P,3$T#0HR+4-1'2SE*3U)&3# P,3$U#0HS+4-[=13=]TA.
C0U5*3U)&3#,Q.3DT#0HT+4-[=13=]TA.0U5*3U)&3#,Q.3DU#0HU
end
注意:如果我使用 echo
或 echo -e
或使用没有 cat 的 uuencode 附加文件,它工作正常,只有当我使用 cat
和 uuencode
时才会出现问题
在上个千年的某个时候 uuencode
很有意义,但在这个时代你真的应该使用 MIME。
这是一个简单的 Python 3 脚本,它将来自标准输入的消息与附件组合成一个有效的 RFC822 消息,您可以使用 sendmail
或类似的方式发送该消息。它非常接近于 Python documentation's examples.
#!/usr/bin/env python3
from email.message import EmailMessage
import sys
msg = EmailMessage()
msg['From'] = sys.argv[1]
msg['To'] = sys.argv[2]
msg['Subject'] = sys.argv[3]
# Don't mess with the preamble, the documentation is wrong to suggest you do
msg.set_content(''.join([line for line in sys.stdin]))
with open(sys.argv[4], 'r') as attachment:
msg.add_attachment(attachment.read(), maintype='text', subtype='plain')
print(msg.as_string())
要在您的示例中使用它,
# If `sendmail` is not in your `PATH`, maybe add it.
# For example, if you have it in `/usr/lib/sendmail`
# and `/usr/lib´ is not in your `PATH`, you can add it with
#PATH=$PATH:/usr/lib
# Don't use upper case for private variables; check spelling of "recipients"
# To demo an example of sending to multiple addresses, adds a second recipient
recipients="mm@gmail.com,another@example.com"
subject="!! testtt !!"
python3 path/to/script.py "zz@gmail.com" "$recipients" "$subject" Data.txt <Results.txt |
sendmail -oi -t
您可以省略通向 sendmail
的管道以查看消息的外观。它不是在消息 body 中轻击 uuencode
,而是创建一个包含两部分的多部分 MIME 消息,其中一部分是内联的并包含来自标准输入的文本,另一部分被正确标记为附件.它有点笨重,但我在这里包含一个示例只是为了向您展示。
From: zz@gmail.com
To: mm@gmail.com,another@example.com
Subject: !! testtt !!
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="===============2189027902917283968=="
--===============2189027902917283968==
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Total Number of Files = 8
Total Number of Fixed Files = 4
Total Number of Notification Files = 4
Total Number of Courrpted Files = 0
Total Execution Time = 3 Seconds.
--===============2189027902917283968==
Content-Type: text/plain
Content-Transfer-Encoding: base64
MIME-Version: 1.0
Content-Disposition: attachment
Tm90aWZpY2F0aW9uIEZpbGVzIDogCmZpbGUgbnVtYmVyIDEKZmlsZSBudW1iZXIgMgo=
--===============2189027902917283968==--
在这个特定的例子中,附件被不必要地 base64
编码了;如果你绝对需要 human-readable,你可以调整 Python 代码(但 uuencode
也不是)。
(另外,不知道为什么 body 部分有一个单独的 MIME-Version:
header -- 这似乎是一个错误。)