用于读取和输出邮箱正文到文件的简单脚本。 [英] simple script to read and output Mailbox body to file.

查看:71
本文介绍了用于读取和输出邮箱正文到文件的简单脚本。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉再次(再次)在这里播放脚本。


我仍​​然不明白为什么get_payload()没有产生

在testwwws用户邮箱中发送电子邮件的纯文本邮件正文。

你可以看到我尝试过一些东西,但没有任何快乐,我错过了什么。


是与get_payload相关的另一个片段,用于访问正文内容

打印并处理到文件。


干杯

查克


ds9:[pythonScriptMail]%cat getSurveyMail.py

########## ################################################## ###

##此脚本将打开并解析电子邮件正文内容。

##此Python脚本将驻留在ds9上的邮件服务器上:

##电子邮件都是普通/文本你可以写下面的

##这将留下一个字符串列表,每个字符串都是一个消息体。

# #Survey用户是testwws和.procmailrc文件夹i s

##调查。 ie / home / testwws / Mail / inbox / Survey。

############################## #################################

## file:getSurveyMail.py已创建: 06/06/04修改日期:07/06/04

############################# ##################################

#以下行让它自己运行(UN * X上的可执行脚本)

#!/ usr / bin / env python


import sys

导入os

导入邮箱

导入邮箱


#fp表示工厂参数

#模式只有'r'才能读取文件,'w''只能写'

#(将删除具有相同名称的现有文件),并且''a''打开文件

#进行追加;写入文件的任何数据都会自动添加到

结尾。

#''r +''打开文件进行读写。模式。

输出=(" / tmp / SurveyResults"," w + a")

#output =(''/ tmp / SurveyResults'', ''w'')


#open()返回一个文件对象,最常用的有两个参数:

#" open(filename ,模式)" ;.

#/ home / testwwws /邮件/工作



#fp传递的文件或类文件对象在实例化时间。这可以是

#用于阅读邮件内容。

fp = open(" / var / spool / mail / testwwws")

>
#fp = open(" / home / testwwws / Mail / work")

#message_from_file从
$ b返回消息对象结构树$ b#打开文件对象。


mbox = mailbox.UnixMailbox(fp,email.message_from_file)

#正文消息列表。

bodies = []


msg = email.message_from_file(fp)

#for循环遍历mbox(邮箱)中的msg。

#消息的子部分可以通过 -

#get_payload()方法访问将返回一个字符串对象。

#如果是多部分,使用走路迭代每个部分的方法和



#获取有效载荷。在我们的例子中,它不是多部分所以忽略。

#部分在msg.walk():

#msg = part.get_payload()

##做点什么(打印)

$ b mbox中msg的$ b:

body = msg.get_payload()

bodies.append(正文)

#output.close()关闭它并释放由

打开文件占用的任何系统资源。

#调用output.close()后,尝试使用该文件对象将

自动失败。

#print机构

打印fp

打印消息

打印消息[''body'']

#print body - NameError:name''msg''未定义



#print> ;>输出,正文

#output.close()

#print消息的正文列表。

打印正文

Sorry to bovver you again (again) here''s script.

I still can''t see why the get_payload() doesn''t produce
the plain text message body of an emails in the testwwws users mailbox.
As you can see I have tried a few things but no joy what am I missing.

Is the another snippet in relation to get_payload to access the body contents
print and process to a file.

Cheers

Chuck

ds9:[pythonScriptMail] % cat getSurveyMail.py
################################################## #############
## This script will open and parse email messages body content.
## This Python script will reside on Mail Server on ds9:
## Emails are all plain/text you could just write the following
## Which will leave a list of strings , each one a message body.
## The Survey User is testwws and the .procmailrc file folder is
## Survey . i.e /home/testwws/Mail/inbox/Survey .
################################################## #############
## file:getSurveyMail.py Created : 06/06/04 Amended date: 07/06/04
################################################## #############

#The following line makes it run itself(executable script on UN*X)
#!/usr/bin/env python

import sys
import os
import email
import mailbox

# Open the testwws user mailbox (tmp user chuck)
# fp denotes factory paraemeter
# mode can be ''r'' when the file will only be read, ''w'' for only writing
#(an existing file with the same name will be erased), and ''a'' opens the file
# for appending; any data written to the file is automatically added to the
end.
# ''r+'' opens the file for both reading and writing. The mode.
output =("/tmp/SurveyResults", "w+a")
#output =(''/tmp/SurveyResults'',''w'')

# open() returns a file object, and is most commonly used with two arguments:
# "open(filename, mode)".
# /home/testwwws/Mail/work
#
# fp The file or file-like object passed at instantiation time. This can be
# used to read the message content.
fp = open("/var/spool/mail/testwwws")

#fp = open("/home/testwwws/Mail/work")

# message_from_file returns a message object struct tree from an
# open file object.

mbox = mailbox.UnixMailbox(fp, email.message_from_file)
# list of body messages.
bodies = []

msg = email.message_from_file(fp)
# for loop iterates through the msg in the mbox(mailbox).
# Subparts of messages can be accessed via the -
# get_payload() method will return a string object.
# If it is multipart, use the "walk" method to iterate through each part and
the
# get the payload.In our case it''s not multipart So ignore.
# for part in msg.walk():
# msg = part.get_payload()
# # do something(print)

for msg in mbox:
body = msg.get_payload()
bodies.append(body)
# output.close() to close it and free up any system resources taken up by the
open file.
# After calling output.close(), attempts to use the file object will
automatically fail.
#print bodies
print fp
print msg
print msg[''body'']
# print body - NameError: name ''msg'' is not defined
#
#print >> output,bodies
#output.close()
#print the bodies list of the messages.
print bodies

推荐答案

Chuck Amadi< ch *** @ smtl.co.uk>写道:
Chuck Amadi <ch***@smtl.co.uk> wrote:
很抱歉再次(再次)在这里播放脚本。

我仍然不明白为什么get_payload()没有产生<在testwwws用户邮箱中发送电子邮件的纯文本邮件正文。
正如你所看到的,我已经尝试了一些东西,但没有任何快乐我错过了什么。

是另一个关于get_payload访问正文的片段
内容打印并处理到文件。
Sorry to bovver you again (again) here''s script.

I still can''t see why the get_payload() doesn''t produce
the plain text message body of an emails in the testwwws users mailbox.
As you can see I have tried a few things but no joy what am I missing.

Is the another snippet in relation to get_payload to access the body
contents print and process to a file.



[snip]


你仍然没有回答中心问题。

- 你想要将电子邮件正文放在单独的文件中,还是将所有电子邮件正文放在

一个文件中?

- 你想收集这些邮件正文吗?或者定期从''mbox''文件获得
吗?


-

William Park,Open Geometry Consulting,< op ********** @ yahoo.ca>

不,我不会修理你的电脑!不过,我会重新格式化你的硬盘。


[snip]

You still haven''t answered central questions.
- Do you want email bodies in separate files, or all email bodies in
one file?
- Do you want to collect these email bodies as they come in, or
periodically from a ''mbox'' file?

--
William Park, Open Geometry Consulting, <op**********@yahoo.ca>
No, I will not fix your computer! I''ll reformat your harddisk, though.


2004年6月7日星期一17:20:42 +0100,Chuck Amadi< ch *** @ smtl。 co.uk>

写道:
On Mon, 07 Jun 2004 17:20:42 +0100, Chuck Amadi <ch***@smtl.co.uk>
wrote:
fp = open(" / var / spool / mail / testwwws")

#fp = open(" / home / testwwws / Mail / work")
#message_from_file从
#打开文件对象返回消息对象结构树。

mbox = mailbox.UnixMailbox(fp,email.message_from_file)
#lice of body messages。
bodies = []

msg = email.message_from_file(fp)
##做某事(打印)
对于mbox中的msg:
body = msg.get_payload()
bodies.append(body)


修剪了几条评论


好​​的,你不需要传递UnixMailbox email.message_from_file如果你的

将在以后用msg明确解析= email.message_from_file(fp)


您解析电子邮件两次。


如果是还是有问题,请尝试分解一下


mbox = mailbox.UnixMailbox(fp)

邮件在mbox中:

print mail.read()

break #just看一条消息


''mail''是指向一封电子邮件的文件obj,所以打印mail.read()

应该显示一个带有标题的电子邮件。如果这不起作用我们在到达任何地方之前都会遇到问题


如果这给了我们一些合理的输出,请尝试解析

电子邮件。


mbox = mailbox.UnixMailbox(fp)

邮箱中的邮箱:

msg = email.message_from_file(mail)

print`msg` #backticks

print msg [''Subject'']

print msg.get_content_type( )

休息#just看一条消息


应该给我们类似的东西:


< email。 Message.Message实例位于0xa0e442c>

Re:最佳IDE?

text / plain


如果可以,那么


msg.get_payload()应该返回正文。

< {{{*>
fp = open("/var/spool/mail/testwwws")

#fp = open("/home/testwwws/Mail/work")

# message_from_file returns a message object struct tree from an
# open file object.

mbox = mailbox.UnixMailbox(fp, email.message_from_file)
# list of body messages.
bodies = []

msg = email.message_from_file(fp)
# # do something(print)

for msg in mbox:
body = msg.get_payload()
bodies.append(body)
Trimmed out a few comments

Ok, you dont need to pass UnixMailbox email.message_from_file if your
going to explictly parse later with msg = email.message_from_file(fp)

Your parsing the email message twice.

If there are still problems, try breaking it down a bit

mbox = mailbox.UnixMailbox(fp)
for mail in mbox:
print mail.read()
break #just look at one message

''mail'' is a file obj pointing at a single email, so print mail.read()
should display one email, with headers. If that doesn''t work we have
problems before we get anywhere

If that gives us something sensible for an output, try parsing the
email.

mbox = mailbox.UnixMailbox(fp)
for mail in mbox:
msg = email.message_from_file(mail)
print `msg` #backticks
print msg[''Subject'']
print msg.get_content_type()
break #just look at one message

Should give us something like:

<email.Message.Message instance at 0xa0e442c>
Re: Best IDE?
text/plain

And if that works, then

msg.get_payload() should return the body.
<{{{*>



William Park写道:
William Park wrote:
Chuck Amadi< ch *** @ smtl.co.uk>写道:

Chuck Amadi <ch***@smtl.co.uk> wrote:

很抱歉再次(再次)在这里播放脚本。

我仍然不明白为什么get_payload ()不会在testwwws用户邮箱中生成电子邮件的纯文本邮件正文。
正如您所看到的,我尝试了一些事情,但没有任何快乐,我错过了什么。

与get_payload相关的另一个片段是否可以访问正文
内容打印并处理到文件。
Sorry to bovver you again (again) here''s script.

I still can''t see why the get_payload() doesn''t produce
the plain text message body of an emails in the testwwws users mailbox.
As you can see I have tried a few things but no joy what am I missing.

Is the another snippet in relation to get_payload to access the body
contents print and process to a file.


[snip]
- 你想要将电子邮件正文放在单独的文件中,还是将所有电子邮件正文放在一个文件中?
- 你想收集这些电子邮件吗?当他们进来时,或者定期从mbox文件进来?


[snip]

You still haven''t answered central questions.
- Do you want email bodies in separate files, or all email bodies in
one file?
- Do you want to collect these email bodies as they come in, or
periodically from a ''mbox'' file?



你好,


我希望所有电子邮件正文都在一个文件中。收集这些电子邮件正文,或定期从mbox文件中收集?使用cron。


我会说cron运行脚本并将有问题的邮件用户框清空到一个文件以提取到数据库。


干杯


Chuck


Hi There ,

I would like all email bodies in one file.Collect these email bodies as they come in, or periodically from a ''mbox'' file? using cron.

I would lie cron to run the script and empty the mail user box in question to a file for extraction to a database.

Cheers

Chuck


这篇关于用于读取和输出邮箱正文到文件的简单脚本。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆