英文:
sending email through unix by using cat with uuencode
问题
这是我在Sun Solaris 10上的工作。我之前尝试过发送普通邮件,包括附件和不包括附件,都能正常工作。但是现在我尝试将cat-file元素和附件一起发送到同一封邮件中,但不起作用。
这是我的result.txt文件内容:
总文件数 = 8
总固定文件数 = 4
总通知文件数 = 4
总损坏文件数 = 0
总执行时间 = 3秒。
这是我的data.txt文件内容:
通知文件:
文件编号 1
文件编号 2
Unix命令:
RECEIPIENTS="mm@gmail.com"
SUBJECT="!! 测试 !!"
(cat Results.txt ; uuencode Data.txt Data.txt) | mailx -s "$SUBJECT" -c mm@gmail.com,dd@gmail.com -r zz@gmail.com $RECEIPIENTS
这是我发送邮件后收到的内容:
总文件数 = 8
总固定文件数 = 4
总通知文件数 = 4
总损坏文件数 = 0
总执行时间 = 3秒。
开始 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* -"U#1$Q604Q-2D]21DPP-#0U, T*#0I&:7AE9"!&:6QE<R Z( T*#0HQ+4-$ 051'2SE*3U)&3# P,3$T#0HR+4-$ 051'2SE*3U)&3# P,3$U#0HS+4-$0TA.
C0U5*3U)&3#,Q.3DT#0HT+4-$0TA.0U5*3U)&3#,Q.3DU#0HU
结束
注意:如果我使用echo
或echo -e
,或者不使用cat
直接使用uuencode
附加文件,都能正常工作。问题只出现在我使用cat
和uuencode
结合时。
英文:
here I'm working on sun Solaris 10
, I have tried before to send a normal email with and without attach and its worked fine with me , but now I'm trying to cat-file element and send attach in the same email but it does not work
this is my result.txt file body :
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.
this is my data.txt file body :
Notification Files :
file number 1
file number 2
unix command :
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
this is what i got after sending the email :
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-$051'2SE*3U)&3# P,3$U#0HS+4-$0TA.
C0U5*3U)&3#,Q.3DT#0HT+4-$0TA.0U5*3U)&3#,Q.3DU#0HU
end
note: if I use echo
or echo -e
or attach the file with uuencode without cat its worked fine, the problem only when I use cat
with uuencode
答案1
得分: 2
在上个千年时代,uuencode
有其合理的用处,但在今天,您应该使用 MIME 代替。
以下是一个简单的 Python 3 脚本,将标准输入中的消息与附件组合成一个有效的 RFC822 消息,您可以使用 sendmail
或类似的工具发送。它在很大程度上基于Python 文档的示例。
#!/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]
# 不要修改序言,文档建议您不要这样做
msg.set_content(''.join())
with open(sys.argv[4], 'r') as attachment:
msg.add_attachment(attachment.read(), maintype='text', subtype='plain')
print(msg.as_string())
在您的示例中使用它,
# 如果 `sendmail` 不在您的 `PATH` 中,可以添加它。
# 例如,如果您将其放在 `/usr/lib/sendmail` 中
# 而 `/usr/lib` 不在您的 `PATH` 中,您可以使用以下方式添加:
# PATH=$PATH:/usr/lib
# 不要使用大写字母表示私有变量;检查“recipients”的拼写
# 为了演示发送到多个地址的示例,添加了第二个收件人
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
的管道以查看消息的外观。它不是直接放在消息正文中的 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
编码;如果您绝对需要它可读,可以调整 Python 代码(不过 uuencode
也不是可读的)。
(此外,不清楚为什么正文部分有一个单独的 MIME-Version:
标头 - 这似乎是一个错误。)
英文:
There was a time in the last millennium when uuencode
made sense, but you really should be using MIME instead in this day and age.
Here is a simple Python 3 script which combines a message from standard input with an attachment into a valid RFC822 message which you can send with sendmail
or similar. It is rather closely based on the 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())
with open(sys.argv[4], 'r') as attachment:
msg.add_attachment(attachment.read(), maintype='text', subtype='plain')
print(msg.as_string())
To use this in your example,
# 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
You can leave out the pipe to sendmail
to see what the message looks like. Instead of uuencode
smack dab in the message body, it creates a multipart MIME message with two parts, one of which is inline and contains the text from standard input, and another which is properly tagged as an attachment. It's slightly bulky, but I'm including an example here just to show you.
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==--
The attachment is rather needlessly base64
-encoded in this particular example; you can tweak the Python code if you absolutely need it to be human-readable (but then neither was the uuencode
).
(Also, no idea why the body part has a separate MIME-Version:
header -- that seems like a bug.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论