Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用SMTP发送邮件_Python_Email_Smtp - Fatal编程技术网

在python中使用SMTP发送邮件

在python中使用SMTP发送邮件,python,email,smtp,Python,Email,Smtp,我有一个问题,我将如何使用这个 SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options]) 这是我的密码: import sys import os import re from smtplib import SMTP_SSL as SMTP from email.MIMEMultipart import MIMEMultipart from email.MI

我有一个问题,我将如何使用这个

    SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
这是我的密码:

    import sys
    import os
    import re
    from smtplib import SMTP_SSL as SMTP
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText

    SMTPserver = 'server.me.com'
    sender =     'from@yyy.com'
    destination = 'to@yyy.com'
    recipient = ['rcp1@aaa.com', 'rcp2@aaa.com', 'rcp3@aaa.com']

    USERNAME = 'user'
    PASSWORD = 'pass'
    text_subtype = 'plain'
    content='This is a test for mail sending'
    subject='Test'

    try:
        msg = MIMEMultipart()
        msg = MIMEText(content, text_subtype)
        msg['From'] = sender
        msg['To'] = destination
        msg['Cc'] = ', '.join(recipient)
        msg['Subject']= subject

        conn = SMTP(SMTPserver)
        conn.login(USERNAME, PASSWORD)
        try:
            **conn.sendmail(sender, destination, msg.as_string())**
        except Exception, exc:
            sys.exit( "connection failed; %s" % str(exc) )
        finally:
            conn.close()

    except Exception, exc:
        sys.exit( "mail failed; %s" % str(exc) )

这是正确的语法还是代码?我想要的是,我也想向收件人列表发送邮件,但我应该将其放在代码中的什么位置?

这基本上没问题。您是否确实尝试过使用您的代码(另请参见注释)? 唯一需要注意的是,conn.sendmail()中的
destination
需要是一个列表,因此
[destination]
。然后,您可以轻松地将其替换为收件人,因为这已经是一个列表了。 这里有一些很好的示例:,您的代码与这些示例中的代码基本相同


当您运行代码并遇到无法解决的实际错误时,欢迎您回到这里提出新问题。

请比“我有问题”更具体一些。你有什么问题?你能告诉我们你犯了什么错误吗?