1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
#coding=utf-8
# - 星期五之前无法发送
# - 修改日期检查(>10分钟将询问)
# - 自动添加周段日期
import
os
import
datetime
import
smtplib
from
email.MIMEMultipart
import
MIMEMultipart
from
email.mime.text
import
MIMEText
from
email.MIMEBase
import
MIMEBase
from
email
import
Encoders
# configuration
talkweb_username
=
''
# user name without @...
talkweb_password
=
'='
# password
talkweb_subject
=
u'[周报][
%s
]XXX'
# title %s will replace with 0000.00.00-00.00
talkweb_content
=
u'如附件'
# content
talkweb_mailto_list
=
[
''
]
# mail to list
talkweb_attachment
=
''
# weekly report location
talkweb_from_address
=
'
%s
@talkweb.com.cn'
%
talkweb_username
talkweb_smtp_config
=
dict
()
talkweb_smtp_config
[
'host'
]
=
'mail.talkweb.com.cn'
talkweb_smtp_config
[
'port'
]
=
25
talkweb_smtp_config
[
'username'
]
=
talkweb_username
talkweb_smtp_config
[
'password'
]
=
talkweb_password
has_colorama
=
False
try
:
from
colorama
import
init
init
()
from
colorama
import
Fore
has_colorama
=
True
except
Exception
,
e
:
print
'Warning: '
,
e
;
pass
def
send_mail
(
smtp_config
,
from_address
,
to_address_list
,
subject
,
content
,
attachments
=
None
):
msg
=
msg
=
MIMEMultipart
()
msg
[
'Subject'
]
=
subject
msg
[
'From'
]
=
from_address
msg
[
'To'
]
=
';'
.
join
(
to_address_list
)
msg
.
attach
(
MIMEText
(
content
,
'plain'
,
'utf-8'
))
for
attachment
in
attachments
:
attachment_part
=
MIMEBase
(
'application'
,
"octet-stream"
)
attachment_part
.
set_payload
(
open
(
attachment
,
"rb"
)
.
read
())
Encoders
.
encode_base64
(
attachment_part
)
attachment_part
.
add_header
(
'Content-Disposition'
,
'attachment; filename="
%s
"'
%
os
.
path
.
basename
(
attachment
))
msg
.
attach
(
attachment_part
)
smtp_conn
=
smtplib
.
SMTP
()
smtp_conn
.
connect
(
smtp_config
[
'host'
],
smtp_config
[
'port'
])
smtp_conn
.
login
(
smtp_config
[
'username'
],
smtp_config
[
'password'
])
smtp_conn
.
sendmail
(
from_address
,
to_address_list
,
msg
.
as_string
())
smtp_conn
.
close
()
def
grace_print
(
info
,
type
=
None
):
if
type
==
'error'
:
print
Fore
.
RED
if
has_colorama
else
''
,
'Error: '
,
info
elif
type
==
'warning'
:
print
Fore
.
YELLOW
if
has_colorama
else
''
,
'Warning: '
,
info
elif
type
==
'progress'
:
print
Fore
.
CYAN
if
has_colorama
else
''
,
'==> '
,
info
elif
type
==
'done'
:
print
Fore
.
GREEN
if
has_colorama
else
''
,
'Done: '
,
info
else
:
print
info
def
main
():
today
=
datetime
.
datetime
.
today
()
weekday
=
today
.
weekday
()
weekday
+=
1
if
weekday
<
5
:
grace_print
(
'Today is not friday'
,
'error'
)
return
delayed
=
weekday
-
5
if
delayed
>
0
:
grace_print
(
'Delayed for
%d
'
%
delayed
,
'warning'
)
# check modify time
modify_time
=
datetime
.
datetime
.
fromtimestamp
(
os
.
path
.
getmtime
(
talkweb_attachment
))
modify_escape
=
today
-
modify_time
modify_escape_secs
=
modify_escape
.
total_seconds
()
/
60
if
modify_escape_secs
>
10
:
grace_print
(
'Please edit your weekly report first, last modify:
%s
'
%
modify_time
,
'error'
)
continue_answer
=
raw_input
(
'Continue anyway?(y/N)'
)
if
continue_answer
.
lower
()
!=
'y'
:
return
friday
=
today
-
datetime
.
timedelta
(
delayed
)
monday
=
friday
-
datetime
.
timedelta
(
4
)
friday_to_monday
=
'
%s
-
%s
'
%
(
monday
.
strftime
(
'%Y.%m.
%d
'
),
friday
.
strftime
(
'%m.
%d
'
))
grace_print
(
'Sending weekly report'
,
'progress'
)
subject
=
talkweb_subject
%
friday_to_monday
content
=
talkweb_content
attachments
=
[]
attachments
.
append
(
talkweb_attachment
)
send_mail
(
talkweb_smtp_config
,
talkweb_from_address
,
talkweb_mailto_list
,
subject
,
content
,
attachments
)
grace_print
(
'Sended weekly report'
,
'done'
)
if
__name__
==
'__main__'
:
main
()
|