def mail_smtp(): mail_host = 'smtp.qq.com' mail_pass = SECRET_PASS mail_user = '******' to_list = ['*****@*****.**'] with open('./weibo-qq-login.png', 'rb') as png: message = MIMEImage(png.read()) message['From'] = mail_user message['To'] = ",".join(to_list) subject = 'A岛登陆啦' # 邮件主题 message['Subject'] = Header(subject, 'utf-8') message.add_header('Content-Disposition', 'attachment', filename="weibo-qq-login.png") try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号 smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(mail_user, to_list, message.as_string()) print("邮件发送成功") except smtplib.SMTPException as e: print(e.args) print("Error: 无法发送邮件")
def setUp(self): #set up the image for the message ePic = 'v:/workspace/HandlingEmail_Homework/src/python-logo.png' att = open(ePic, 'rb') img = MIMEImage(att.read()) att.close() img.add_header('Content-Disposition', 'attachment', filename=os.path.basename(ePic)) #set up the message body msgText = MIMEText("This is a test string", 'plain') #build the message msg = MIMEMultipart() msg['To'] = '*****@*****.**' msg['From'] = '*****@*****.**' msg['Subject'] = 'Test Email' msg.attach(msgText) msg.attach(img) self.Mmsg = msg #create a set for comparison self.attachmentSet = {msgText.as_string(), img.as_string()} #engages the function attachments = [ePic] mailObj = emailReturn('*****@*****.**', 'This is a test string', attachments) self.mailTest = mailObj
def getmimeimage(sub, fr, to, img): img_encode = cv2.imencode('.jpg', img)[1] img_bytes = img_encode.tobytes() mime_img = MIMEImage(img_bytes) mime_img['Content-type'] = 'application/octet-stream' mime_img['Cotent-Disposition'] = 'attashment; filename=pic.jpg' mime_img['Subject'] = sub mime_img['From'] = fr mime_img['To'] = to return mime_img.as_string()
def roll_call(schoolnum_l, account, smtp): for i in range(len(schoolnum_l)): from_addr = account to_addr = schoolnum_l[i] + '@mail.nuk.edu.tw' image_name = 'good.jpg' with open(image_name, "rb") as file: filecontent = file.read() mime = MIMEImage(filecontent) mime["Content-Type"] = "application/octet-stream" mime["Content-Disposition"] = 'attachment; filename=' + image_name mime["Subject"] = '點名成功' msg = mime.as_string() status = smtp.sendmail(from_addr, to_addr, msg) if status == {}: print("郵件傳送成功!") else: print("郵件傳送失敗!")
def imageEml(self): smtpServer = "smtp.163.com" user = "******" password = "******" sender = "*****@*****.**" receiver = "*****@*****.**" subject = u"发送图片邮件" #发送1张图片 imageFile = open(r"C:\Users\Administrator\Desktop\1.PNG", "rb").read() msg = MIMEImage(imageFile) msg.add_header('Content-ID', '<image1>') #图片ID msg["Subject"] = subject msg["To"] = receiver smtp = smtplib.SMTP() smtp.connect(smtpServer) smtp.login(user, password) smtp.sendmail(sender, receiver, msg.as_string())
def sendMail(self): subject = "Piton Ar-Ge Internship" message = "Bu bir test mesajıdır." content = "Subject: {0}\n\n{1}".format(subject, message) fp = open('resim3.jpg', 'rb') msgImage = MIMEImage(fp.read()) fp.close() self.myMailAddress = "*****@*****.**" #sending mail should be entered here password = "******" #sending mail's password should be entered here self.sendTo = self.mail.text() mail = SMTP("smtp.gmail.com", 587) mail.ehlo() mail.starttls() mail.login(self.myMailAddress, password) mail.sendmail(self.myMailAddress, self.sendTo, msgImage.as_string()) print("mail gönderme işlemi başarılı")
def mail(ip): ret = True try: # msg = MIMEMultipart() # content = MIMEText(ip[0], 'plain', 'utf-8') # 正文 # msg.attach(content) img_file = open("screenPicture1.png", "rb") sendimagefile = img_file.read() msg = MIMEImage(sendimagefile) msg.add_header('Content-ID', '<image1>') msg.add_header('Content-Disposition', 'attachment', filename='二维码.png') msg['From'] = formataddr(["堕落天使", my_sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号 msg['To'] = formataddr(["邪神·洛基", my_user]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号 msg['IP'] = "一居室" # 邮件的主题,也可以说是标题 # 账户密码 server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器 server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码 server.sendmail(my_sender, [my_user, ], msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件 server.quit() # 关闭连接 except Exception: # 如果 try 中的语句没有执行,则会执行下面的 ret=False ret = False return ret
# ch26_12.py import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from_addr = '*****@*****.**' # 設定發信帳號 pwd = input('請輸入 %s 的密碼 : ' % from_addr) # 要求輸入發信帳號密碼 to_addr_list = ['*****@*****.**', '*****@*****.**'] # 設定收件人 with open('rushmore.jpg', 'rb') as fn: # 讀取圖片內容 mailPict = fn.read() msg = MIMEImage(mailPict) msg['Content-Type'] = 'application/octet-stream' msg['Content-Disposition'] = 'attachment; filename="rushmore.jpg"' msg['Subject'] = '傳送圖片附加檔案' msg['From'] = '我愛Python' msg['To'] = '*****@*****.**' msg['Cc'] = '*****@*****.**' mySMTP = smtplib.SMTP('smtp.gmail.com', 587) # 執行連線 mySMTP.ehlo() # 啟動對話 mySMTP.starttls() # 執行TLS加密 mySMTP.login(from_addr, pwd) # 登入郵件伺服器 status = mySMTP.sendmail(from_addr, to_addr_list, msg.as_string()) # 執行發送信件 if status == {}: # 檢查是否發信成功 print("發送郵件成功!") mySMTP.quit() # 結束連線
# strTime = localtime.strftime("%Y-%m-%d %H:%M") # msg = MIMEText('Motion Detected in RPi: '+strTime)#mail massage #msg['From'] = SMTP_RECIPIENT+'@gmail.com' #image fp = open('./image0.jpg', 'rb') msg = MIMEImage(fp.read()) fp.close() msg['Subject'] = 'Raspi Noti' msg['To'] = SMTP_RECIPIENT # Send the email via Gmail print("Sending the mail") server = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT) server.login(SMTP_USERNAME, SMTP_PASSWORD) #server.send_message(msg) server.sendmail(FROM, TO, msg.as_string()) server.quit() print("Mail sent") else: print('-') time.sleep(1) except IOError: print("Error") GPIO.cleanup() break except KeyboardInterrupt: print("Good Bye") GPIO.cleanup() break
# coding=utf-8 from email.mime.image import MIMEImage image_data = '' image_msg = MIMEImage(image_data) print image_msg.as_string()
# 发件人邮箱 mail_sender = '*****@*****.**' # 邮箱登录授权码 mail_license = 'FDNXMSGLFPZNXRZZ' # 收件人邮箱,可以为多个收件人 mail_receivers = '*****@*****.**' # ②构建图片邮件对象 with open("E:\\111.jpg", 'rb') as f: mail_body = f.read() msg = MIMEImage(mail_body) msg['Content-Disposition'] = 'attachment;filename="shoe.png"' # ③设置邮件头部内容 subject = 'python邮件测试' msg['Subject'] = Header(subject, 'utf-8') msg['From'] = 'ZhangSan<*****@*****.**>' msg['To'] = mail_receivers # ④发送邮件 # 创建smtp对象 smtp = smtplib.SMTP() # 设置发件人邮箱的域名和端口 smtp.connect(mail_host) # 登录邮箱 smtp.login(mail_sender, mail_license) # 发送邮件 smtp.sendmail(mail_sender, mail_receivers, msg.as_string()) print('邮件发送成功!') smtp.quit()
def send_email_report_notification(): # Return the current date and time current_time_stamp = datetime.datetime.today() # Define the text based message content email_message_content = "Current Status Report - Luminosity: {0} V\n Temperature: {1} F\n Humidity: {2} %\n Soil Moisture: {3} V\n Solenoid Valve: {4}\n Linear Actuator: {5}\n Output #1: {6}\n Output #2: {7}\n Output #1: {8}\n Record Date: {9}\n Record Time: {10}\n Current Timestamp: {11}\nAy-yah\'s Greenhouse Automation System Version 1.03\n\n".format( current_luminosity_sensor_value, current_temperature_value, current_humidity_value, current_soil_moisture_sensor_value, current_solenoid_valve_status_value, current_actuator_extension_status_value, current_output_one_status_value, current_output_two_status_value, current_output_three_status_value, current_record_date_value, current_record_time_value, current_time_stamp) + EMAIL_MESSAGE_TAG_LINE # Define the html based message content email_message_html_content = '<html>' \ '<head><title>Ay-yah\'s Greenhouse Automation System Version 1.04</title>' \ '<style>' \ 'table, th, td {' \ ' border: 1px solid black;' \ ' border-collapse: collapse;' \ '}' \ 'th, td {' \ ' padding: 10px;' \ '}' \ '</style>' \ '</head>' \ '<body>' \ ' <center>' \ '<h1 align="center">Ay-yah\'s Greenhouse Status Report</h1>' \ ' <table style="width:30%" align="center">' \ ' <tr>' \ ' <td valign="top">' \ '<h2 align="center" valign="center">Status Information</h2>' \ '<table style="width:20%" align="center" valign="top">' \ ' <tr>' \ ' <th>Reading Name</th>' \ ' <th>Value</th>' \ ' </tr>' \ ' <tr>' \ ' <td align="center">Luminosity</td>' \ ' <td align="center">' + \ "{0}" \ ' V</td>' \ ' </tr>' \ ' <tr>' \ ' <td align="center">Temperature</td>' \ ' <td align="center">' \ "{1}" \ ' F</td>' \ ' </tr>' \ ' <tr>' \ ' <td align="center">Humidity</td>' \ ' <td align="center">' \ "{2}" \ ' %</td>' \ ' </tr>' \ ' <tr>' \ ' <td align="center">Soil Moisture</td>' \ ' <td align="center">' \ "{3}" \ ' V</td>' \ ' </tr>' \ ' <tr>' \ ' <td align="center">Solenoid Valve</td>' \ ' <td align="center">' \ "{4}" \ ' </td>' \ ' </tr>' \ ' <tr>' \ ' <td align="center">Linear Actuator</td>' \ ' <td align="center">' \ "{5}" \ ' </td>' \ ' </tr>' \ ' <tr>' \ ' <td align="center">Output One</td>' \ ' <td align="center">' \ "{6}" \ ' </td>' \ ' </tr>' \ ' <tr>' \ ' <td align="center">Output Two</td>' \ ' <td align="center">' \ "{7}" \ ' </td>' \ ' </tr>' \ ' <tr>' \ ' <td align="center">Output Three</td>' \ ' <td align="center">' \ "{8}" \ ' </td>' \ ' </tr>' \ ' <tr>' \ ' <td align="center">Record Date</td>' \ ' <td align="center">' \ "{9}" \ ' </td>' \ ' </tr>' \ ' <tr>' \ ' <td align="center">Record Time</td>' \ ' <td align="center">' \ "{10}" \ ' </td>' \ ' </tr>' \ ' </table>' \ ' </td>' \ ' <td valign="top">' \ '<center><h2>' \ '</h2>' \ '<a href="cid:5">' \ '<img src="cid:0" alt="Greenhouse Camera Image - Animated GIF file" height="240" width="320" border="5">' \ '<br>Click for high resolution image</a><br>' \ '<h2 align="center">Graphical Environmental Record<br>(24 Hours)</h2>' \ '<table align="center">' \ '<thead>' \ ' <tr>' \ ' <th>Luminosity</th>' \ ' <td><img src="cid:1" alt="Greenhouse Luminosity (Last 24 Hours)" height="240" width="320"></td>' \ ' </tr>' \ ' <tr>' \ ' <th>Temperature</th>' \ ' <td><img src="cid:2" alt="Greenhouse Temperature (Last 24 Hours)" height="240" width="320"></td>' \ ' </tr>' \ ' <tr>' \ ' <th>Humidity</th>' \ ' <td><img src="cid:3" alt="Greenhouse Humidity (Last 24 Hours)" height="240" width="320"></td>' \ ' </tr>' \ ' <tr>' \ ' <th>Soil Mositure</th>' \ ' <td><img src="cid:4" alt="Greenhouse Soil Moisture (Last 24 Hours)" height="240" width="320"></td>' \ ' </tr>' \ ' </tbody>' \ '</table>' \ "<center><br>This message generated: {11}<br><br>" \ "{12}<br><br>" \ '</center>' \ '</body>' \ '</html>'.format(current_luminosity_sensor_value, current_temperature_value, current_humidity_value, current_soil_moisture_sensor_value, current_solenoid_valve_status_value, current_actuator_extension_status_value, current_output_one_status_value, current_output_two_status_value, current_output_three_status_value, current_record_date_value, current_record_time_value, current_time_stamp, EMAIL_MESSAGE_TAG_LINE) # Create a MIMEMultipart object which will contain both email and attachment data msg = MIMEMultipart("alternative") # Set the sender address msg['From'] = FROM_EMAIL_ADDRESS_VALUE # Set the destination addresses recipients = RECIPIENTS_EMAIL_ADDRESS_VALUES # recipients = ['*****@*****.**'] # Example single recipient # Join the recipients addresses into one string and set the destination values msg['To'] = ", ".join(recipients) # msg['To'] = recipients # Use with only single recipient and no .join() # Define the message subject # email_subject = "Ay-yah's Greenhouse Status Report: {0}".format(current_time_stamp) email_subject = EMAIL_MESSAGE_SUBJECT_LINE msg['Subject'] = Header(email_subject, 'utf-8').encode() # Create the text based message content object that is 'plain' msg_content = MIMEText(email_message_content, 'plain', 'utf-8') msg.attach(msg_content) # Create the html based message content object that is 'html' msg_html_content = MIMEText(email_message_html_content, 'html', 'utf-8') msg.attach(msg_html_content) # Define a tuple containing all of the image files being uploaded image_file_name_and_path_tuple = ( CAMERA_IMAGE_LOW_RESOLUTION_ANIMATED_GIF_FILE_NAME, GRAPH_IMAGE_LUMINOSITY_FILE_NAME, GRAPH_IMAGE_TEMPERATURE_FILE_NAME, GRAPH_IMAGE_HUMIDITY_FILE_NAME, GRAPH_IMAGE_SOIL_MOISTURE_FILE_NAME, CAMERA_IMAGE_HIGH_RESOLUTION_JPG_FILE_NAME) # Define a counter for the loop through the tuple list i = 0 # Loop through the image files listed in the tuple and upload each image as an attachment for image_file_path in image_file_name_and_path_tuple: if (os.path.isfile(image_file_path)): # Open the image file as a readable binary 'rb' image_file = open(image_file_path, 'rb') # Create the MIMEBase object image_part = MIMEImage(image_file.read(), name=image_file_path) image_part.add_header('Content-ID', '<' + str(i) + '>') image_part.add_header("Content-Disposition", "in-line", filename=image_file_path) image_part.add_header('X-Attachment-Id', str(i)) # Attach the MIMEBase object to the email message msg.attach(image_part) image_file.close() image_part_str = image_part.as_string() # print('Attached image file is : ' + image_file_path + ', image part string : ' + image_part_str) i = i + 1 # Send the email via SMTP with SMTP(host=EMAIL_SMTP_SERVER_HOST_NAME, port=587) as smtp_server: try: # Define a secure SSL connection smtp_server.starttls(context=SSLContext(PROTOCOL_TLSv1_2)) # Supply authentication credentials smtp_server.login(user=SMTP_SERVER_LOGIN_NAME, password=SMTP_SERVER_LOGIN_PASSWORD) # Send the email message smtp_server.send_message(msg) except Exception as e: print('Error sending email. Details: {} - {}'.format( e.__class__, e))
#邮箱服务器配置信息 smtp_server = 'smtp.qq.com' #qq邮箱服务器 port = 465 #邮件发送人和收件人信息 send_eamil = '*****@*****.**' # 发送方邮箱 password = '******' # 填入发送方邮箱的授权码 receiver_mail = '*****@*****.**' # 收件人邮箱 # 构建.png图片格式邮件附件 image_file = open(r'E:\GitHub\Python_Study\send_email\tupian.png', 'rb').read() msg = MIMEImage(image_file) msg.add_header('Content-ID', '<image1>') msg["Content-Disposition"] = 'attachment; filename="red_people.png"' #构建邮件主题和邮件头部发送人、收件人信息 subject = "python邮件测试" msg['Subject'] = subject msg['From'] = send_eamil msg['To'] = receiver_mail try: server = smtplib.SMTP_SSL(smtp_server, port) #链接服务器 server.login(send_eamil, password) #登录邮箱 server.sendmail(send_eamil, receiver_mail, msg.as_string()) #发送邮件 print('success') except Exception: print('fail') finally: server.quit()
smtp_gmail.ehlo() smtp_gmail.starttls() smtp_gmail.login("logicsolutionjimmy", "qwert231") except: print("connect ERROR") exit() from_addr = "*****@*****.**" to_addr = "*****@*****.**" # message = "Subject:FirstTest\nHello World" img = cv2.imread("car.jpg") img_encode = cv2.imencode(".jpg", img)[1] # 將 img 編碼為 jpg 格式,[1]返回資料, [0]返回是否成功 img_bytes = img_encode.tobytes() message = MIMEImage(img_bytes) message["Content-type"] = "application/octet-stream" message["Content-Disposition"] = 'attachment; filename="handsome.jpg"' message["Subject"] = "測試的主旨" message["From"] = "粉絲" message["To"] = "偉大的站長" # message["Cc"] = "副本" status = smtp_gmail.sendmail(from_addr, to_addr, message.as_string()) if not status: print("Success") else: print("Fail") print(status) smtp_gmail.quit()
# msg = MIMEText("ssss") # html = "<h1>fujian</h1> 我是 通过Python发送的1 <i>作者</i>" # msg = MIMEText(html, 'html') # msg = MIMEMultipart() # img["Content-Disposition"] = 'attachment,filename="hdrCount.txt"' # 这行改为: # img["Content-Disposition"] = 'attachment,filename=%s' % string.encode("utf-8") # 也就是说对你发送的内容需要进行utf - 8编码 fileimage = open("e:/123.jpg", 'rb') msg = MIMEImage(fileimage.read(), "image") fileimage.close() msg["from"] = sender msg["to"] = receiver msg["subject"] = "测试邮件3" string = "abc.jpg" msg["Content-Disposition"] = 'attachment,filename=%s' % string.encode( "utf-8") smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() except Exception as e: print(e) # # 文件附件 # filedoc = open("d:/zzy.docx", "rb") # msgfile = MIMEText(filedoc.read(), "base64", "utf-8") # filedoc.close() # msgfile["Content-Type"] = 'application/octet-stream' # # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 # msgfile["Content-Disposition"] = 'attachment; filename="test.docx"' # message.attach(msgfile)