예제 #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()
예제 #2
0
파일: Storage.py 프로젝트: yarcat/manent
  def save_container(self, container):
    print "Saving container"

    s = smtplib.SMTP()
    s.set_debuglevel(1)
    print "connecting"
    #s.connect("gmail-smtp-in.l.google.com")
    s.connect("alt2.gmail-smtp-in.l.google.com")
    #s.connect("smtp.gmail.com", 587)
    print "starting tls"
    s.ehlo("www.manent.net")
    s.starttls()
    s.ehlo("www.manent.net")
    print "logging in"
    
    print "sending header in mail"
    #s.set_debuglevel(0)
    header_msg = MIMEMultipart()
    header_msg["Subject"] = "manent.%s.%s.header" % (
      self.backup.label, str(container.index))
    #header_msg["To"] = "*****@*****.**"
    #header_msg["From"] = "*****@*****.**"
    header_attch = MIMEBase("application", "manent-container")
    filename = container.filename()
    header_file = open(os.path.join(
      Config.paths.staging_area(), filename), "rb")
    header_attch.set_payload(header_file.read())
    header_file.close()
    Encoders.encode_base64(header_attch)
    header_msg.attach(header_attch)
    s.set_debuglevel(0)
    s.sendmail("*****@*****.**",
      "*****@*****.**", header_msg.as_string())
    s.set_debuglevel(1)
    
    print "sending data in mail"
    data_msg = MIMEMultipart()
    data_msg["Subject"] = "manent.%s.%s.data" % (self.backup.label,
      str(container.index))
    data_attch = MIMEBase("application", "manent-container")
    filename = container.filename()
    data_file = open(
      os.path.join(Config.paths.staging_area(), filename+".data"), "rb")
    data_attch.set_payload(data_file.read())
    data_file.close()
    Encoders.encode_base64(data_attch)
    data_msg.attach(data_attch)
    s.set_debuglevel(0)
    s.sendmail("*****@*****.**",
      "*****@*****.**", data_msg.as_string())
    s.set_debuglevel(1)
    print "all done"
    
    s.close()
    print header_msg.as_string()
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"
예제 #4
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()
예제 #5
0
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(
    MIMEText(
        '<html><body><h1>Hello</h1>' + '<p><img src="cid:0"></p>' +
        '</body></html>', 'html', 'utf-8'))
# 再次发送,就可以看到图片直接嵌入到邮件正文的效果:
예제 #6
0
        message["From"] = sender_email
        message["To"] = receiver_email
        message["Subject"] = Betreff
        message["Bcc"] = receiver_email  # Recommended for mass emails 

        # Add body to email
        message.attach(MIMEText(Inhalt, "plain"))

        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:
예제 #7
0
# 构造MIMEMultipart对象做为根容器
msg = MIMEMultipart()

# 构造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('发件的邮箱', '发件的密码')