Exemple #1
0
def email_results(msg_body):
    from_address = "*****@*****.**"
    recipients = ['*****@*****.**', '*****@*****.**']
    to_address = ", ".join(recipients)
    msg = MIMEMultipart()
    msg['From'] = from_address
    msg['To'] = to_address
    msg['Subject'] = "FundBot"
    msg.attach(MIMEText(msg_body, 'plain'))

    for filename in os.listdir(os.path.join("plots")):
        if filename.endswith(".png"):
            attachment = open(os.path.join("plots", filename), "rb")
            part = MIMEBase('application', 'octet-stream')
            part.set_payload(attachment.read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition',
                            "attachment; filename= %s" % filename)
            msg.attach(part)

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(from_address, "123456789")
    text = msg.as_string()
    server.sendmail(from_address, to_address, text)
    server.quit()
def send_email_attachment(filename):
    msg = MIMEMultipart()
    file_content = open(filename, "rb").read()

    part = MIMEBase("application", "octet-stream")
    part.set_payload(file_content)
    Encoders.encode_base64(part)
    today = datetime.now().strftime('%Y-%m-%d')
    part.add_header("Content-Disposition", "attachment; filename=content-%s.csv" % today)

    msg.attach(part)
    msg['Subject'] = "PI | Automation Framework - [%s]" % today
    msg['From'] = sender
    msg['To'] = rcvr
    try:
        smtpObj = smtplib.SMTP('localhost')
        smtpObj.sendmail(sender, rcvr, msg.as_string())
        smtpObj.close()
    except smtplib.SMTPException:
        print "Failed to send email"
Exemple #3
0
    def send_email(self,
                   email_to,
                   email_from,
                   subject,
                   body,
                   attachment_path=None):

        msg = MIMEMultipart()
        msg['From'] = email_from
        msg['To'] = email_to
        msg['Subject'] = subject
        msg.attach(MIMEText(body, 'plain'))

        if attachment_path:
            part = MIMEBase('application', 'octet-stream')
            part.set_payload(attachment_path.read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition',
                            "attachment; filename= %s" % attachment_path)
            msg.attach(part)

        server = smtplib.SMTP(self.smtp_server, self.smtp_port)
        server.sendmail(email_from, email_to, msg.as_string())
        server.quit()
Exemple #4
0
# 如果Email中要加上附件怎么办?带附件的邮件可以看做包含若干部分的邮件:文本和各个附件本身,所以,可以构造一个MIMEMultipart对象代表邮件本身,然后往里面加上一个MIMEText作为邮件正文,再继续往里面加上表示附件的MIMEBase对象即可:
# 邮件对象:
msg = MIMEMultipart()
msg['From'] = _format_addr(u'Python爱好者 <%s>' % from_addr)
msg['To'] = _format_addr(u'管理员 <%s>' % to_addr)
msg['Subject'] = Header(u'来自SMTP的问候……', 'utf-8').encode()

# 邮件正文是MIMEText:
msg.attach(MIMEText('send with file...', 'plain', 'utf-8'))

# 添加附件就是加上一个MIMEBase,从本地读取一个图片:
with open('/Users/michael/Downloads/test.png', 'rb') as f:
    # 设置附件的MIME和文件名,这里是png类型:
    mime = MIMEBase('image', 'png', filename='test.png')
    # 加上必要的头信息:
    mime.add_header('Content-Disposition', 'attachment', filename='test.png')
    mime.add_header('Content-ID', '<0>')
    mime.add_header('X-Attachment-Id', '0')
    # 把附件的内容读进来:
    mime.set_payload(f.read())
    # 用Base64编码:
    encoders.encode_base64(mime)
    # 添加到MIMEMultipart:
    msg.attach(mime)
# 然后,按正常发送流程把msg(注意类型已变为MIMEMultipart)发送出去,就可以收到如下带附件的邮件:
# mimemultipart
# 发送图片
# 如果要把一个图片嵌入到邮件正文中怎么做?直接在HTML邮件中链接图片地址行不行?答案是,大部分邮件服务商都会自动屏蔽带有外链的图片,因为不知道这些链接是否指向恶意网站。
# 要把图片嵌入到邮件正文中,我们只需按照发送附件的方式,先把邮件作为附件添加进去,然后,在HTML中通过引用src="cid:0"就可以把附件作为图片嵌入了。如果有多个图片,给它们依次编号,然后引用不同的cid:x即可。
# 把上面代码加入MIMEMultipart的MIMEText从plain改为html,然后在适当的位置引用图片:
msg.attach(
Exemple #5
0
        filename = "/home/pi/NAS/error.log" # In same directory as script
       
        # Open PDF file in binary mode
        with open(filename, "rb") as attachment:
            #Add file as application/octet-stream
            # Email client can usually download this automatically as attachment
            part = MIMEBase("application", "octet-stream")
            part.set_payload(attachment.read())
  
            # Encode file in ASCII characters to send by email    
            encoders.encode_base64(part)


            
            # Add header as key/value pair to attachment part
            part.add_header("Content-Disposition", "attachment; filename=logfile.txt",)
            # Add attachment to message and convert message to string
            message.attach(part)
            text = message.as_string()

            # Log in to server using secure context and send email
            context = ssl.create_default_context()
            with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
                server.login(sender_email, password)
                server.sendmail(sender_email, receiver_email, text)
     
        print("E-mail sent")
      
    sys.exit()

Exemple #6
0
# 构造MIMEText对象做为邮件显示内容并附加到根容器
body = MIMEText(mail_body)
msg.attach(body)

# 构造MIMEBase对象做为文件附件内容并附加到根容器
# 等同于如下3行
# contype = 'application/octet-stream'
# maintype, subtype = contype.split('/', 1)
# part = MIMEBase(maintype, subtype)
part = MIMEBase('application', 'octet-stream')

# 读入文件内容并格式化,此处文件为当前目录下,也可指定目录 例如:open(r'/tmp/123.txt','rb')
part.set_payload(open('123.txt', 'rb').read())
Encoders.encode_base64(part)
## 设置附件头
part.add_header('Content-Disposition', 'attachment; filename="herb.zip"')
msg.attach(part)

# 设置根容器属性
msg['Subject'] = 'this is the title'
msg['From'] = mail_from
msg['To'] = ';'.join(mail_to)
msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')
# 如上得到了格式化后的完整文本msg.as_string()
# 用smtp发送邮件
smtp = smtplib.SMTP()
smtp.connect('')  # 服务,如果是163的邮箱,就填上smtp.163.com
smtp.login('发件的邮箱', '发件的密码')
smtp.sendmail(mail_from, mail_to, msg.as_string())
smtp.quit()
print 'ok'