2011-03-14 154 views
1

我在尋找一個關於如何使用多個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

回答

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 = "[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

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 = "[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

相關問題