Ver código fonte

improve mail

tsi 3 anos atrás
pai
commit
1034536697
1 arquivos alterados com 25 adições e 1 exclusões
  1. 25 1
      send_email.py

+ 25 - 1
send_email.py

@@ -1,6 +1,7 @@
 import urllib.request, tempfile, smtplib, email, time
 from datetime import datetime, timedelta
 
+
 tmpdir = tempfile.TemporaryDirectory() 
 beginning = str((1000*int(time.time()))-(17*3600000))
 end = str(1000*int(time.time()))
@@ -18,8 +19,13 @@ def get_uptime():
 
 get_boot_time()
 
+
 # Send an HTML email with an embedded image and a plain text message for
 # email clients that don't want to display the HTML.
+urllib.request.urlretrieve("http://localhost/cacti/graph_image.php?local_graph_id=109&graph_start=1657999980&graph_end=1658086115&graph_width=700&graph_height=200", tmpdir.name + "/ping.png")
+
+urllib.request.urlretrieve("http://localhost/cacti/graph_image.php?local_graph_id=103&graph_start=1658000546&graph_end=1658086946&graph_width=695&graph_height=198", tmpdir.name + "/traffic.png")
+
 
 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
@@ -49,9 +55,27 @@ bt = get_boot_time()
 bt_str = bt.strftime("%d.%m.%Y, %H.%M")
 
 # We reference the image in the IMG SRC attribute by the ID we give it dasdasdbelow
-msgText = MIMEText('<br><p>Letzter Start: ' + bt_str + ' Uhr</p>', 'html')
+msgText = MIMEText('<img src="cid:image1"><br><img src="cid:image2"><br><br><p>Letzter Start: ' + bt_str + ' Uhr</p>', 'html')
 msgAlternative.attach(msgText)
 
+
+# This example assumes the image is in the current directory
+fp = open(tmpdir.name + '/traffic.png', 'rb')
+msgImage1 = MIMEImage(fp.read())
+fp.close()
+
+fp = open(tmpdir.name + '/ping.png', 'rb')
+msgImage2 = MIMEImage(fp.read())
+fp.close()
+
+
+# Define the image's ID as referenced above
+msgImage1.add_header('Content-ID', '<image1>')
+msgRoot.attach(msgImage1)
+msgImage2.add_header('Content-ID', '<image2>')
+msgRoot.attach(msgImage2)
+
+
 # Send the email (this example assumes SMTP authentication is required)
 import smtplib, ssl
 context = ssl.create_default_context()