如何使用多个CC发送Gmail电子邮件:

时间:2011-03-14 21:32:10

标签: python email-headers

我正在寻找一个关于如何使用多个CC发送Gmail电子邮件的快速示例:。有人可以建议一个示例代码段吗?

2 个答案:

答案 0 :(得分: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")

对于我修改this

的代码段

答案 1 :(得分: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

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

关于多个抄送地址的具体问题,如上面的代码所示,您需要使用逗号分隔的字符串电子邮件地址,而不是列表。

如果您想要姓名和地址,也可以使用email.utils.formataddr() function帮助他们获得正确的格式:

>>> 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>'

希望这有帮助,如果您有任何问题,请告诉我。