我正在寻找一个关于如何发送具有多个 CC: 的 Gmail 电子邮件的快速示例。任何人都可以建议一个示例片段吗?
问问题
5735 次
2 回答
1
如果你可以使用一个库,我强烈建议http://libgmail.sourceforge.net/,我之前使用过,非常好用。您必须在您的 gmail 帐户中启用 IMAP/POP3 才能使用它。
至于代码片段(我没有机会尝试这个,如果可以的话我会编辑它):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
#EDIT THE NEXT TWO LINES
gmail_user = "your_email@gmail.com"
gmail_pwd = "your_password"
def mail(to, subject, text, attach, cc):
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject
#THIS IS WHERE YOU PUT IN THE CC EMAILS
msg['Cc'] = cc
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
mail("some.person@some.address.com",
"Hello from python!",
"This is a email sent with python")
对于我修改这个的片段
于 2011-03-14T21:46:18.967 回答
1
我已经为您准备了一些代码,展示了如何连接到 SMTP 服务器、构建电子邮件(在 Cc 字段中有几个地址)并发送它。希望评论的自由应用将使其易于理解。
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
## The SMTP server details
smtp_server = "smtp.gmail.com"
smtp_port = 587
smtp_username = "username"
smtp_password = "password"
## The email details
from_address = "address1@domain.com"
to_address = "address2@domain.com"
cc_addresses = ["address3@domain.com", "address4@domain.com"]
msg_subject = "This is the subject of the email"
msg_body = """
This is some text for the email body.
"""
## Now we make the email
msg = MIMEText(msg_body) # Create a Message object with the body text
# Now add the headers
msg['Subject'] = msg_subject
msg['From'] = from_address
msg['To'] = to_address
msg['Cc'] = ', '.join(cc_addresses) # Comma separate multiple addresses
## Now we can connect to the server and send the email
s = SMTP_SSL(smtp_server, smtp_port) # Set up the connection to the SMTP server
try:
s.set_debuglevel(True) # It's nice to see what's going on
s.ehlo() # identify ourselves, prompting server for supported features
# If we can encrypt this session, do it
if s.has_extn('STARTTLS'):
s.starttls()
s.ehlo() # re-identify ourselves over TLS connection
s.login(smtp_username, smtp_password) # Login
# Send the email. Note we have to give sendmail() the message as a string
# rather than a message object, so we need to do msg.as_string()
s.sendmail(from_address, to_address, msg.as_string())
finally:
s.quit() # Close the connection
关于多个抄送地址的具体问题,如您在上面的代码中看到的,您需要使用逗号分隔的电子邮件地址字符串,而不是列表。
如果您想要名称和地址,您不妨使用该email.utils.formataddr()
函数来帮助将它们转换为正确的格式:
>>> from email.utils import formataddr
>>> addresses = [("John Doe", "john@domain.com"), ("Jane Doe", "jane@domain.com")]
>>> ', '.join([formataddr(address) for address in addresses])
'John Doe <john@domain.com>, Jane Doe <jane@domain.com>'
希望这会有所帮助,如果您有任何问题,请告诉我。
于 2011-03-20T22:59:32.613 回答