def sendActivationMail(self): from django.contrib.sites.models import Site from django.core.mail import EmailMessage from django.template.loader import render_to_string from django.core.urlresolvers import reverse from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText current_site = Site.objects.get(id=settings.SITE_ID) subject = u"Gostream.com aktivasyon kodunuz" url = reverse('user_activate', kwargs={'actcode':self.activation_code}) print url img_content_id = 'mail_confirmation.jpg' #TODO img_data = open('/Users/yasemenkarakoc/netcen/gostream/static/images/mail_confirmation.jpg', 'rb').read() msg = MIMEMultipart(_subtype='related') body = 'cid:%s' % img_content_id html_content = render_to_string('email/mail_activation.html', {'email': self.email, 'url': url, 'current_site':current_site, 'body':body}) msg = EmailMessage(subject, html_content, settings.DEFAULT_FROM_EMAIL, [self.email]) msg.content_subtype = "html" msg = email_embed_image(msg, img_content_id, img_data) msg.send()
def set_password_link(email, subdomain, org, auth_token, member_name): from_email = settings.EMAIL_HOST_USER to = [email] subject = "Welcome Email" msg = MIMEMultipart('alternative') html_content = render_to_string( 'TFC/setpassword_link.html', { 'org': org, 'subdomain': subdomain, 'auth_token': auth_token, 'member_name': member_name }) msg = EmailMultiAlternatives(subject, html_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send(fail_silently=True)
def form_valid(self, form): users = form.cleaned_data['users'] subject = form.cleaned_data['subject'] message = form.cleaned_data['message'] from_email = 'HackForChange Team<*****@*****.**>' headers = {'Reply-To': '*****@*****.**'} for user in users: msg = MIMEMultipart('alternative') html_content = render_to_string('HFC/invitation_email.html', { 'message': message, 'name': user.name }) msg = EmailMessage(subject, html_content, from_email, [ user.email, ], headers=headers) msg.content_subtype = "html" msg.send(fail_silently=True) return super(SendUserEmails, self).form_valid(form)
def screeninglink_mail(email, org, subdomain): volunteer = Candidate.objects.get(email=email) name = volunteer.name screening = Screenings.objects.create(candidate_id=volunteer) screeninguuid = screening.screening_uuid print(screeninguuid) email = volunteer.email from_email = settings.EMAIL_HOST_USER to = [email] subject = "Screening Link" msg = MIMEMultipart('alternative') html_content = render_to_string( 'TFC/email.html', { 'org': org, 'subdomain': subdomain, 'screeninguuid': screeninguuid, 'name': name }) msg = EmailMultiAlternatives(subject, html_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send(fail_silently=True)
def send_email(subject, email_to, body, context): email = MIMEMultipart(_subtype='related') html_content = render_to_string(body, context) email = EmailMessage(subject, html_content, to=[email_to]) email.content_subtype = "html" # Main content is now text/html email.send()
def send(): from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText msg = MIMEMultipart('alternative') if self.subject: msg['Subject'] = self.subject else: msg['Subject'] = 'No Subject' if self.friendly_to: msg[''] # Attach our text message msg.attach(MIMEText('text',self.text)) # Detect if there is HTML if self.html: msg.attach(MIMEText('html',self.html) _mailer(msg) part1 = MIMEText(text,'plain') part2 = MIMEText(html,'html') msg.attach(part1) msg.attach(part2) send(me,you,msg.as_string(),SERVER,USER,PASS) s = smtplib.SMTP(SERVER) s.starttls() s.login(USER,PASS) s.sendmail(me,you,msg) from django.template import Context, Template t = Template(message) c = Context() def send_mail_template(subject, template, addr_from, addr_to, context=None, attachments=None, fail_silently=False): """ Send email rendering text and html versions for the specified template name using the context dictionary passed in. Arguments are as per django's send_mail apart from template which should be the common path and name of the text and html templates without the extension, for example: "email_templates/contact" where both "email_templates/contact.txt" and "email_templates/contact.html" exist. """ from django.core.mail import EmailMultiAlternatives from django.template import loader, Context if context is None: context = {} if attachments is None: attachments = [] # allow for a single address to be passed in if not hasattr(addr_to, "__iter__"): addr_to = [addr_to] # loads a template passing in vars as context render = lambda type: loader.get_template("%s.%s" % (template, type)).render(Context(context)) # create and send email msg = EmailMultiAlternatives(subject, render("txt"), addr_from, addr_to) msg.attach_alternative(render("html"), "text/html") for attachment in attachments: msg.attach(attachment) msg.send(fail_silently=fail_silently)