1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| import smtplib
import subprocess
from os.path import basename
import email.message
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
#import base64
# OK
# https://forwardemail.net/en/guides/smtp-integration#python-integration
# Email configuration
sender_email = "smtp.free.fr"
receiver_email = "xxxx@free.fr"
password = "PWDxxxx"
NbFic=subprocess.run("ls -l /root/FS_NTFS/SAVE/Bookmarks | grep bookmarks | wc -l", shell=True, capture_output=True, text=True)
chaine = "ATTENTION il y a {} fichiers Bookmarks".format(NbFic.stdout.strip());
#bchaine = base64.b64encode(bytes(chaine, 'utf-8')) # bytes
#base64_str = bchaine.decode('utf-8') # convert bytes to string
# Create message
message = MIMEMultipart()
message["Subject"] = chaine
message["From"] = "xxxx@free.fr"
message["To"] = "xxxx@free.fr"
# Create the plain-text and HTML version of your message
text = "Hello world! This is a test email sent using Python and Forward Email SMTP TXT."
html = "<html><body><b>Hello world!</b> This is a test email sent using Python and Forward Email SMTP HTML.</body></html>"
# We assume that the image file is in the same directory that you run your Python script from
# fp = open('Spinoza.bmp', 'rb')
# image = MIMEImage(fp.read())
# fp.close()
# Specify the ID according to the img src in the HTML part
# image.add_header('Content-ID', '<Mailtrapimage>')
# message.attach(image)
message.attach(MIMEText(html, 'html'))
# for attchement
# part = MIMEApplication(open("/root/FS_NTFS/Mime/Spinoza.bmp", "rb").read())
# part['Content-Disposition'] = 'attachment; filename='+basename("/root/FS_NTFS/Mime/")
# message.attach(part)
# Turn these into plain/html MIMEText objects
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
# Add HTML/plain-text parts to MIMEMultipart message
# Commenter (part2) pour n'avoir que le TXT
message.attach(part1)
message.attach(part2)
# Send email
try:
server = smtplib.SMTP_SSL("smtp.free.fr", 465)
server.login("xxxx@free.fr", "PWDxxxx")
server.sendmail("xxxx@free.fr", "xxxx@free.fr", message.as_string())
server.quit()
print("Email sent successfully!")
except Exception as e:
print(f"Error sending email: {e}") |
Partager