1

複数のCC:を使用してGmailのメールを送信する方法の簡単な例を探しています。誰かがサンプルスニペットを提案できますか?

4

2 に答える 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

読みやすいように、上の pastie.org のコードを次に示します。

複数の Cc アドレスに関する具体的な質問については、上記のコードでわかるように、リストではなく、コンマで区切られた電子メール アドレスの文字列を使用する必要があります。

住所だけでなく名前も必要な場合は、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 に答える