def send_mail_sendgrid(description, agenda: Agenda): c = Calendar() e = Event() e.name = 'Atendimento Aset Terapias' str_begin = '-'.join(reversed(agenda.date.split('/'))) str_begin = f'{str_begin} {agenda.time}' e.begin = (datetime.strptime(str_begin, '%Y-%m-%d %H:%M') + timedelta(hours=3)) e.end = (datetime.strptime(str_begin, '%Y-%m-%d %H:%M') + timedelta(hours=4)) e.attendees = [agenda.appointment.customer.email, agenda.therapist.email] e.description = description c.events.add(e) curdir = os.getcwd() with open(f'{curdir}{os.sep}go.ics', 'w') as f: f.writelines(c) with open( f'{curdir}{os.sep}apps{os.sep}agenda{os.sep}email_templates{os.sep}appointment-confirmation.html', encoding='utf-8') as templ: body = ''.join(templ.readlines()) body = body.replace('{{nome}}', agenda.appointment.customer.name) body = body.replace('{{nome_terapia}}', agenda.appointment.specialty) body = body.replace('{{nome_terapeuta}}', agenda.therapist.name) body = body.replace('{{data}}', agenda.date) body = body.replace('{{hora}}', agenda.time) body = body.replace('{{api}}', os.getenv("API_ENDPOINT")) body = body.replace('{{calendar}}', agenda.calendar.name) body = body.replace('{{therapist_mail}}', agenda.therapist.email) body = body.replace('{{date}}', agenda.date.replace('/', '-')) body = body.replace('{{hour}}', agenda.time) body = body.replace('{{text}}', agenda.appointment.text) message = Mail(from_email=os.getenv("EMAIL_SENDER"), to_emails=agenda.appointment.customer.email, subject='Aset Terapias : Confirmação de consulta', html_content=body) try: message.add_bcc(agenda.therapist.email) with open(f'{curdir}{os.sep}go.ics', 'rb') as f: data = f.read() f.close() encoded = base64.b64encode(data).decode() attachment = Attachment() attachment.file_content = FileContent(encoded) attachment.file_name = FileName('go.ics') attachment.disposition = Disposition('attachment') attachment.content_id = ContentId('Unique Content ID') message.attachment = attachment sg = SendGridAPIClient(api_key=os.getenv('EMAIL_TOKEN')) sg.send(message=message) except Exception as e: print('Erro no envio de e-mail') print(e)
def make_event(title=None, location=None, description=None, begin_date=None, begin_time=None, end_date=None, end_time=None, organizer=None, attendees=[]): if title is None: raise Exception("make_event: a title parameter is required") if begin_date is None: raise Exception("make_event: a begin_date parameter is required") if begin_time is None: raise Exception("make_event: a begin_time parameter is required") if end_date is None: raise Exception("make_event: an end_date parameter is required") if end_time is None: raise Exception("make_event: an end_time parameter is required") c = Calendar() e = Event() if organizer is not None: e.organizer = Organizer(common_name=organizer.name.full(), email=organizer.email) if len(attendees) > 0: e.attendees = [ Attendee(common_name=attendee.name.full(), email=attendee.email) for attendee in attendees ] e.name = str(title) e.begin = as_datetime( begin_date.replace_time(begin_time), timezone='UTC').format_datetime('yyyy-MM-dd hh:mm:ss') e.end = as_datetime(end_date.replace_time(end_time), timezone='UTC').format_datetime('yyyy-MM-dd hh:mm:ss') if location not in (None, ''): e.location = str(location) if description not in (None, ''): e.description = str(description) c.events.add(e) c.events ics_file = DAFile('ics_file') ics_file.set_random_instance_name() ics_file.initialize(filename="event.ics", mimetype="text/calendar") with open(ics_file.path(), 'w') as f: f.write(str(c)) ics_file.commit() return ics_file
def send_mail(description, agenda: Agenda): c = Calendar() e = Event() e.name = 'Atendimento Aset Terapias' str_begin = '-'.join(reversed(agenda.date.split('/'))) str_begin = f'{str_begin} {agenda.time}' e.begin = (datetime.strptime(str_begin, '%Y-%m-%d %H:%M') + timedelta(hours=3)) e.end = (datetime.strptime(str_begin, '%Y-%m-%d %H:%M') + timedelta(hours=4)) e.attendees = [agenda.appointment.customer.email, agenda.therapist.email] e.description = description c.events.add(e) curdir = os.getcwd() with open(f'{curdir}{os.sep}go.ics', 'w') as f: f.writelines(c) with open( f'{curdir}{os.sep}apps{os.sep}agenda{os.sep}email_templates{os.sep}appointment-confirmation.html' ) as templ: body = ''.join(templ.readlines()) body = body.replace('{{nome}}', agenda.appointment.customer.name) body = body.replace('{{nome_terapia}}', agenda.appointment.specialty) body = body.replace('{{nome_terapeuta}}', agenda.therapist.name) body = body.replace('{{data}}', agenda.date) body = body.replace('{{hora}}', agenda.time) body = body.replace('{{api}}', os.getenv("API_ENDPOINT")) body = body.replace('{{calendar}}', agenda.calendar.name) body = body.replace('{{therapist_mail}}', agenda.therapist.email) body = body.replace('{{date}}', agenda.date.replace('/', '-')) body = body.replace('{{hour}}', agenda.time) yag = yagmail.SMTP(os.getenv("EMAIL_SENDER"), os.getenv("EMAIL_PWD")) yag.send( to=agenda.appointment.customer.email, bcc=agenda.therapist.email, subject="Aset Terapias : Confirmação de consulta", contents=body, attachments=[f'{curdir}{os.sep}go.ics'], newline_to_break=False, )
"%Y-%m-%d %H%M%z") + timedelta( days.index(i['day'].lower())) date_end = datetime.strptime(date_end_str, "%Y-%m-%d %H%M%z") + timedelta( days.index(i['day'].lower())) e.name = i['name'] e.begin = date_begin.strftime("%Y-%m-%d %H:%M:%S%z") e.end = date_end.strftime("%Y-%m-%d %H:%M:%S%z") e.description = i['desc'] e.alarms = [ DisplayAlarm(trigger=timedelta(minutes=-5)), DisplayAlarm(trigger=date_begin) ] if i['name'] != "Economics B": e.attendees = ['*****@*****.**'] c.events.add(e) print( f'Created {i["name"]} {datetime.strftime(date_begin, "%Y-%m-%d %H:%M:%S")} - {datetime.strftime(date_end, "%Y-%m-%d %H:%M:%S")}' ) with open('out.ics', 'w') as my_file: my_file.writelines(c) print() print("Exported out.ics") system('open out.ics')