在python中使用命令发送电子邮件

时间:2015-01-31 18:17:19

标签: python email

有时候它有效,它不会发送电子邮件。如果没有发送,我不会收到任何错误。在发送另一封电子邮件之前我是否有限制或时间等待?

示例代码:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

if cmd == "emailA":
              # Define email addresses to use
              addr_to   = 'toEmail'
              addr_from = 'fromEmail'
              # Define SMTP email server details
              smtp_server = 'smtp.gmail.com'
              smtp_user   = 'myEmail'
              smtp_pass   = 'pass'
              s = smtplib.SMTP(smtp_server,587)
              s.starttls()
              # Construct email
              msg = MIMEMultipart('alternative')
              msg['To'] = addr_to
              msg['From'] = addr_from
              msg['Subject'] = 'Subject #1'
              # Create the body of the message (a plain-text and an HTML version).
              text = "Subject #1"
              message = """\
              <html>
                <head></head>
                <body>
                Subject #1
                </body>
              </html>
                </body>
              </html>
              """
              part1 = MIMEText(text, 'plain')
              part2 = MIMEText(message, 'html')
              msg.attach(part1)
              msg.attach(part2)

              s.login(smtp_user,smtp_pass)
              s.sendmail(addr_from, addr_to, msg.as_string())
              s.quit()

if cmd == "emailB":
              # Define email addresses to use
              addr_to   = 'toEmail'
              addr_from = 'fromEmail'
              # Define SMTP email server details
              smtp_server = 'smtp.gmail.com'
              smtp_user   = 'myEmail'
              smtp_pass   = 'pass'
              s = smtplib.SMTP(smtp_server,587)
              s.starttls()
              # Construct email
              msg = MIMEMultipart('alternative')
              msg['To'] = addr_to
              msg['From'] = addr_from
              msg['Subject'] = 'Subject #2'
              # Create the body of the message (a plain-text and an HTML version).
              text = "Subject #2"
              message = """\
              <html>
                <head></head>
                <body>
                Subject #2
                </body>
              </html>
                </body>
              </html>
              """
              part1 = MIMEText(text, 'plain')
              part2 = MIMEText(message, 'html')
              msg.attach(part1)
              msg.attach(part2)

              s.login(smtp_user,smtp_pass)
              s.sendmail(addr_from, addr_to, msg.as_string())
              s.quit()

我想使用命令Eg发送电子邮件。 !emailA

0 个答案:

没有答案