我試圖尋找,但無法找到一個簡單的方法來發送電子郵件。
我正在尋找這樣的事情:
from:"[email protected]"#email sender
To:"[email protected]"# my email
content:open('x.txt','r')
的一切,我發現真的很複雜:我的項目並不需要這麼多的線路。
請,我想了解:在每個代碼發表評論,並解釋
我試圖尋找,但無法找到一個簡單的方法來發送電子郵件。
我正在尋找這樣的事情:
from:"[email protected]"#email sender
To:"[email protected]"# my email
content:open('x.txt','r')
的一切,我發現真的很複雜:我的項目並不需要這麼多的線路。
請,我想了解:在每個代碼發表評論,並解釋
import smtplib
def prompt(prompt):
return raw_input(prompt).strip()
fromaddr = prompt("From: ")
toaddrs = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"
# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
% (fromaddr, ", ".join(toaddrs)))
while 1:
try:
line = raw_input()
except EOFError:
break
if not line:
break
msg = msg + line
print "Message length is " + repr(len(msg))
server = smtplib.SMTP('localhost')
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
'email'是低級'smtplib'模塊的包裝器,例如硬編碼頭。 – katrielalex 2010-09-09 13:43:18
不要忘記給予信用:http://docs.python.org/library/smtplib.html – nmichaels 2010-09-09 13:43:20
哈哈,這很有趣。我知道我的這個Python代碼被埋在了我的homedir的某個地方,我早就忘記了它的來源。謝謝 :) – 2010-09-09 13:47:08
的docs相當straitforward:
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP()
s.sendmail(me, [you], msg.as_string())
s.quit()
一個簡單的例子,這對我的作品使用smtplib:
#!/usr/bin/env python
import smtplib # Brings in the smtp library
smtpServer='smtp.yourdomain.com' # Set the server - change for your needs
fromAddr='[email protected]' # Set the from address - change for your needs
toAddr='[email protected]' # Set the to address - change for your needs
# In the lines below the subject and message text get set up
text='''Subject: Python send mail test
Hey!
This is a test of sending email from within Python.
Yourself!
'''
server = smtplib.SMTP(smtpServer) # Instantiate server object, making connection
server.set_debuglevel(1) # Turn debugging on to get problem messages
server.sendmail(fromAddr, toAddr, text) # sends the message
server.quit() # you're done
這是我在發現的代碼並修改。
如果我們像usenmae='[email protected]'那樣使用stmp並且通過_ ='test2312' – 2010-09-09 13:39:32