用Python傳送郵件

2021-06-09 12:56:15 字數 1968 閱讀 8909

伺服器需要週期性的檢查郵件佇列,然後通過smtp伺服器傳送出去,就網上搜了搜,然後修改了加工了一下,寫了乙個郵件傳送的簡單模組

兩個檔案

config.py:配置資訊

send_msg.py:傳送郵件

send_msg.py

#coding=utf-8

import

smtplib,config,email,sys

from

email.message 

import

message

defconnect():

"connect to smtp server and return a smtplib.smtp instance object

"server

=smtplib.smtp(config.smtpserver,config.smtpport)

server.ehlo()

server.login(config.smtpuser,config.smtppass)

return

server

defsendmessage(server,to,subj,content):

"using server send a email

"msg 

=message()

msg[

'mime-version']

='1.0'

msg[

'from

']    

=config.smtpuser

msg['to

']      =to

msg[

'subject'] 

=subj

msg[

'date

']    

=email.utils.formatdate()          

#curr datetime, rfc2822

msg.set_payload(content)

try:    

failed 

=server.sendmail(config.smtpuser,to,str(msg))   

#may also raise exc

except

exception ,ex:

print

exception,ex

print

'error - send failed

'else

:print

"send success!"if

__name__=="

__main__":

#frm=raw_input('from? ').strip()to=

raw_input(

'to? 

').strip()

subj

=raw_input(

'subj? 

').strip()   

print

'type message text, end with line="."

'text =''

while

true:

line 

=sys.stdin.readline()

ifline =='

. ': break

text 

+=line

server

=connect()

sendmessage(server,to,subj,text)

config.py

***********************************==

smtpserver='mail.***x.net'

smtpuser='user@***.net'

smtppass='pwd'

smtpport='25'

***********************************==

**:

用python傳送怎麼傳送郵件?

python高階程式設計中有乙個smtp協議,smtp mail transfer protocol 即簡單郵件傳輸協議,它是一組用於由源位址到目的位址傳送郵件的規則,由它來控制信件的中轉方式。import smtplib smtpobj smtplib.smtp host port local h...

用python傳送swf郵件

端午快到了,公司要向客戶 一封賀歲郵件,內容就是一段flash,swf格式的 首先將swf檔案上傳伺服器,然後將swf巢狀於html中,最後用python以html格式傳送郵件即可。下面是將swf巢狀於html中的 注意 在傳送此類含flash的郵件時,一般都要加上外鏈結,並標明若不能正常顯示,這裡...

用JMail傳送郵件

function sendmail byval from as string,byval recipient as string,byval recipientcc as string,byval recipientbcc as string,byval subject as string,byva...