def create_profile(sender, instance, created, **kwargs): email = get_email() if created and email: instance.email = email instance.save() # name = instance.name.replace(' ', '') service = gmail.init_service() gmail.setup_alias(service, instance) else: service = gmail.init_service() if gmail.my_labels(service, instance) == None: gmail.setup_alias(service, instance) print('Email setup done!')
def notify_created(user): sub = 'Successful regitstraion' html_content = render_to_string('users/created_user_mail.html', {'user': user}) sender = 'Events Portal<*****@*****.**>' to = user.email service = gmail.init_service() message = gmail.create_message(sender, to, sub, html_content) gmail.send_message(service, message)
def mail_reader_thread(): service = gmail.init_service() username = f'service.events' email = f'{username}@gmail.com' author, created = User.objects.get_or_create(username=username, email=email, password='******') while True: print('Reading mail...') for c in Company.objects.all(): # company = Company.objects.filter(email=c.email).first() labels = gmail.my_labels(service, c) q = 'from:[email protected]' msg_obj = service.users().messages() lbs_m = [labels['main']] lbs_s = [labels['success']] lbs_f = [labels['fail']] results = msg_obj.list(userId='me', labelIds=lbs_m, q=q).execute() messages = results.get('messages', []) for message in messages: msg = msg_obj.get(userId='me', id=message['id']).execute() payload = msg['payload'] msg_text = None if 'parts' in payload: for p in payload['parts']: if p['mimeType'] == 'text/plain': data = p['body']['data'] msg_text = base64.b64decode(data).decode('UTF-8') break else: data = payload['body']['data'] msg_text = base64.b64decode(data).decode('UTF-8') if msg_text and parse_mail(author, c, msg_text): print('Successfully parsed the mail') msg_labels = { 'removeLabelIds': lbs_m, 'addLabelIds': lbs_s } service.users().messages().modify( userId='me', id=message['id'], body=msg_labels).execute() else: print('Failed to parse the mail') msg_labels = { 'removeLabelIds': lbs_m, 'addLabelIds': lbs_f } service.users().messages().modify( userId='me', id=message['id'], body=msg_labels).execute() # break time.sleep(15)
def handle(self, *args, **options): self.stdout.write("Running sendmail command!") sub = 'User created' user = User.objects.get(username='******') msg = render_to_string('users/created_user_mail.html', {'user': user}) sender = 'Events Portal<*****@*****.**>' to = '*****@*****.**' # send_mail(sub, msg, sender, to, fail_silently=False,) service = gmail.init_service() message = gmail.create_message(sender, to, sub, msg) print(message) gmail.send_message(service, message)