def sendemail2(self, subject, message, from_email="", recipient_list="", cc_email=""): # backend = EmailBackend(host='mail.ims.cn', port=25, username='******', password='******') from django.core.mail import message as msg from_email = from_email or '*****@*****.**' recipient_list = recipient_list or ['*****@*****.**'] cc_email = cc_email or ['*****@*****.**'] with mail.get_connection(host='mail.ims.cn', port=25, username='******', password='******') as connection: msg = mail.EmailMessage( subject=subject, body=message, from_email=from_email, to=recipient_list, cc=cc_email, connection=connection, headers={ "X-Priority": "1 (High)", #'X-MSMail-Priority':'High', # 'Importance':'High' } # headers={"Message-ID":msg.make_msgid(idstring='yangry',domain='wosign.com')} ) msg.content_subtype = "html" msg.send()
def reset_password_email(request): if request.is_ajax(): email = request.POST.get('email', None) check_email = User.objects.filter(email=email).exists() if check_email == False: response = {'error': 'Email does not exists.'} return JsonResponse(response) # Email exists if the above error is not been throw # Let us send the email to the user user = User.objects.get(email=email) # Let's call the generate function to generate our token token = gen_token() first_name = user.first_name last_name = user.last_name site_name = settings.SITE_NAME password_link = settings.SITE_URL + 'forgot-password/reset_password/?signature=' + token # Let's setup variable's to add to our template subject_file = os.path.join(settings.BASE_DIR, "mail/reset_password/subject.txt") subject = render_to_string(subject_file, { 'name': first_name, 'site_name': site_name }) from_email = settings.DEFAULT_EMAIL_SENDER to_email = [email] password_message_file = os.path.join(settings.BASE_DIR, "mail/reset_password/body.txt") password_message = render_to_string( password_message_file, { 'first_name': first_name, 'last_name': last_name, 'password_link': password_link, 'site_name': site_name, }) message = EmailMultiAlternatives(subject=subject, body=password_message, from_email=from_email, to=to_email) html_template = os.path.join(settings.BASE_DIR, "mail/reset_password/body.html") template = render_to_string( html_template, { 'first_name': first_name, 'last_name': last_name, 'password_link': password_link, 'site_name': site_name, }) message.attach_alternative(template, "text/html") message.send() UserPassToken.objects.create(user=user, token=token, sent=True) response = {'success': 'Check your email for instructions'} return JsonResponse(response)
def notify(self, state): """ Send email notifications based on current message state @param state : a valid value for Message.status """ if not self.author.organization.settings.notify_message_status \ or not self.author.organization.contact_email: return if state == Message.SENDING: message = NotificationMessage( subject=render_to_string( 'campaigns/sending_notice_email_subject.txt', { 'product_name': settings.PRODUCT_NAME, 'name': self.name }).strip(), template='campaigns/sending_notice_email.txt', render_context={'message': self}, to=self.author.organization.contact_email, ) message.add_html_part_if_exists( 'campaigns/sending_notice_email.html') message.send() elif state == Message.SENT: stats = self.mk_stats() message = NotificationMessage( subject=render_to_string( 'campaigns/sending_done_notice_email_subject.txt', { 'product_name': settings.PRODUCT_NAME, 'name': self.name }).strip(), template='campaigns/sending_done_notice_email.txt', render_context={ 'message': self, 'stats': stats }, to=self.author.organization.contact_email, ) message.add_html_part_if_exists( 'campaigns/sending_done_notice_email.html') message.send()
def contact(request): if request.is_ajax(): name = request.POST.get('name', None) email = request.POST.get('email', None) phone = request.POST.get('mobile', None) subject = request.POST.get('subject', None) message= request.POST.get('message', None) site_name = settings.SITE_NAME # Let's setup variable's to add to our template subject_file = os.path.join(settings.BASE_DIR, "mail/contact/subject.txt") subject_1 = render_to_string(subject_file, {'name': name, 'site_name': site_name}) from_email = email to_email = [settings.DEFAULT_EMAIL_SENDER] contact_message_file = os.path.join(settings.BASE_DIR, "mail/contact/body.txt") contact_message = render_to_string(contact_message_file, { 'name': name, 'email': email, 'phone': phone, 'subject': subject, 'message': message, 'site_name': site_name, }) message_1 = EmailMultiAlternatives(subject=subject_1, body=contact_message, from_email=from_email, to=to_email) html_template = os.path.join(settings.BASE_DIR, "mail/contact/body.html") template = render_to_string(html_template, { 'name': name, 'email': email, 'phone': phone, 'subject': subject, 'message': message, 'site_name': site_name, }) message_1.attach_alternative(template, "text/html") message_1.send() if message.send(): # contactinfo = Contactinfo(name=name,email=email,phone=phone,subject=subject,message=message) # contactinfo.save() response = {"success": "Your message has been sent successfully, we will get back to you soon!"} else: response = {"error": "Sorry, try again please."} return JsonResponse(response)
def register_ajax(request): if request.is_ajax(): form = SignUpForm(request.POST) if form.is_valid(): form.save() obj = form.save() otp_code = generateOTP() site_name = settings.SITE_NAME full_name = obj.first_name + ' ' + obj.last_name subject_file = os.path.join(settings.BASE_DIR, "mail/register/subject.txt") subject = render_to_string(subject_file, { 'name': obj.first_name, 'site_name': site_name }) from_email = settings.DEFAULT_EMAIL_SENDER to_email = [obj.email] register_message_file = os.path.join(settings.BASE_DIR, "mail/register/body.txt") register_message = render_to_string( register_message_file, { 'first_name': obj.first_name, 'last_name': obj.last_name, 'otp_code': otp_code, 'site_name': site_name, }) message = EmailMultiAlternatives(subject=subject, body=register_message, from_email=from_email, to=to_email) html_template = os.path.join(settings.BASE_DIR, "mail/register/body.html") template = render_to_string( html_template, { 'first_name': obj.first_name, 'last_name': obj.last_name, 'otp_code': otp_code, 'site_name': site_name, }) message.attach_alternative(template, "text/html") message.send() UserSettings.objects.create(user=obj, verified_code=otp_code) response = { 'success': 'Registration successful. Kindly enter the OTP sent to your email address. [' + obj.email + ']' } return JsonResponse(response) else: response = { 'error': 'We could not process your request. Try again.' } return JsonResponse(response) else: response = {'error': 'Please check all fields and try again.'} return JsonResponse(response)
def send_async_email(message): message.send()