如何通过多个CC发送Gmail电子邮件:'s

如何通过多个CC发送Gmail电子邮件:'s

问题描述:

我在寻找一个关于如何使用多个CC发送Gmail电子邮件的快速示例:'s。任何人都可以提出一个示例代码片段?如何通过多个CC发送Gmail电子邮件:'s

+0

告诉我们你如何发送单个CC,我们会尽力帮助你发送更多的CC。 – 2011-03-14 21:38:01

+0

恐怕您的问题有点不清楚 - 您是否想要发送CC'd到多个Gmail地址的电子邮件,或者您是否正在从Gmail地址向多个CC发送电子邮件? – 2011-03-14 21:38:23

+0

我想发一封电子邮件给一个人,但CC:人。 – 2011-03-14 21:40:27

我以前做的片段为您呈现了一段代码,演示了如何连接到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 = "[email protected]" 
to_address = "[email protected]" 

cc_addresses = ["[email protected]", "[email protected]"] 

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 

Here's the code above on pastie.org for easier reading

关于多个抄送地址的具体问题,你可以在上面的代码中看到,你需要用逗号分隔的电子邮件地址的,而不是一个列表。

如果你想要的名称以及地址,您不妨使用email.utils.formataddr() function来帮助他们进入正确的格式:

>>> from email.utils import formataddr 
>>> addresses = [("John Doe", "[email protected]"), ("Jane Doe", "[email protected]")] 
>>> ', '.join([formataddr(address) for address in addresses]) 
'John Doe <[email protected]>, Jane Doe <[email protected]>' 

希望这会有所帮助,让我知道,如果你有任何问题。

+0

py2.7的文档指出,使用'SMTP_SSL'时不需要'starttls()'http://docs.python.org/library/smtplib.html?highlight=smtplib#smtplib.SMTP_SSL – 2012-05-09 13:55:26

如果您可以使用库,我强烈建议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 = "[email protected]" 
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("[email protected]", 
    "Hello from python!", 
    "This is a email sent with python") 

对于我修改this

+0

这不适用于我: msg ['cc'] = ['[email protected]','[email protected]'] – 2011-03-14 22:07:10

+1

**不要**使用libgmail。它通过抓取网络界面来工作,现在已经贬值了,因为Gmail具有IMAP和SMTP。 – Acorn 2011-03-20 23:06:00