def render_email_and_send(context=None, message_template='', subject='', recipients=None, sender=None): """ Sends an e-mail based on the given parameters. Takes a context (as a dictionary), a template path (as a string), a subject (as a string) and recipients (as a list or tuple). E-mail templates SHOULD (see RFC 2119 for the meaning of SHOULD) be plain text. (I have no idea what will happen if you pass HTML.) """ t = loader.get_template(message_template) c = Context(context) message = t.render(c) # Not sure why, but there's a random bug somewhere that causes e-mail # socket errors. It's puzzled some other people, but this line seems to # more or less fix it: # http://mail.python.org/pipermail/python-list/2006-December/420357.html mimetools._prefix = 'x' if not sender: sender = settings.SERVER_EMAIL # send_mail(subject, message, sender, recipients) connection = SMTPConnection(fail_silently=True) email_to_send = EmailMessage( subject = subject, body = message, from_email = sender, to = recipients, connection = connection ) email_to_send.send(fail_silently=True) connection.close()
def send_verification_email(email, task): domain = get_metro()['short_name'] + '.' + settings.EB_DOMAIN url = 'http://%s%s' % (domain, verification_url(email, task)) template_name = { CREATE_TASK: 'register', RESET_TASK: 'password_reset' }[task] text_content = render_to_string('accounts/%s_email.txt' % template_name, { 'url': url, 'email': email }) html_content = render_to_string('accounts/%s_email.html' % template_name, { 'url': url, 'email': email }) if settings.DEBUG: print text_content print html_content else: subject = { CREATE_TASK: 'Please confirm account', RESET_TASK: 'Password reset request' }[task] conn = SMTPConnection() # Use default settings. message = EmailMultiAlternatives(subject, text_content, settings.GENERIC_EMAIL_SENDER, [email], connection=conn) message.attach_alternative(html_content, 'text/html') message.send()
def mass_mail_sending_view(request): m1 = EmailMessage('First Test message', 'This is the first test email', '*****@*****.**', ['*****@*****.**', '*****@*****.**']) m2 = EmailMessage('Second Test message', 'This is the second test email', '*****@*****.**', ['*****@*****.**', '*****@*****.**']) c = SMTPConnection() c.send_messages([m1, m2]) return HttpResponse("Mail sent")
def handle_noargs(self, **options): dummy_request = HttpRequest() dummy_request.GET['for_email'] = 'true' self.set_content(dummy_request) self.subject = self.get_subject() self.sent_str = self.get_sent_str() self.connection = SMTPConnection() self.errors = {} print 'starting: ' + datetime.datetime.now().strftime("%c") # try up to four times, if necessary for i in range(4): not_sent = self.subscriber_base.exclude(last_sent=self.sent_str) if len(not_sent): # intentionally resolve the query if i != 0: print datetime.datetime.now().strftime('%c'), print '- sleeping before retrying %s emails...' % len( not_sent) time.sleep(self.WAIT_TIME) for subscriber in not_sent: self.try_sending_to_subscriber(subscriber) # any that *still* didn't work? error_output = [] for sub in self.subscriber_base.exclude(last_sent=self.sent_str): errors = [ '%d %s' % (len(data), kind) for kind, data in self.errors[sub.email].items() ] error_output.append('errors with %s: %s' % (sub.email, '\t'.join(errors))) if error_output: print '\n', '\n'.join(error_output), '\n' mail_admins( 'Errors sending out', "Got the following errors while sending the email:\n" + '\n'.join(error_output) + '\n\nMore information is available by loading the pickled log file.' ) if self.errors: print 'more info available in the pickle file' pickle.dump(self.errors, sys.stderr) print 'finished: ' + datetime.datetime.now().strftime("%c")
def renew_connection(self, subscriber, retries=5, sleep_time_base=2): # close the old one self.connection.fail_silently = True self.connection.close() # get a new one for i in range(retries): try: self.connection = SMTPConnection() self.connection.open() except socket.timeout: self.add_error(subscriber, 'timeout while opening connection') time.sleep(sleep_time_base**i) else: return
def _send_email(runinfo): "Send the email for a specific runinfo entry" subject = runinfo.subject translation.activate(subject.language) tmpl = loader.get_template(settings.QUESTIONNAIRE_EMAIL_TEMPLATE) c = Context() c['surname'] = subject.surname c['givenname'] = subject.givenname c['gender'] = subject.gender c['email'] = subject.email c['random'] = runinfo.random c['runid'] = runinfo.runid c['created'] = runinfo.created c['site'] = getattr(settings, 'QUESTIONNAIRE_URL', '(settings.QUESTIONNAIRE_URL not set)') email = tmpl.render(c) emailFrom = settings.QUESTIONNAIRE_EMAIL_FROM emailSubject, email = email.split("\n", 1) # subject must be on first line emailSubject = emailSubject.strip() emailFrom = emailFrom.replace("$RUNINFO", runinfo.random) emailTo = '"%s, %s" <%s>' % (subject.surname, subject.givenname, subject.email) emailTo = encode_emailaddress(emailTo) emailFrom = encode_emailaddress(emailFrom) try: conn = SMTPConnection() msg = EmailMessage(emailSubject, email, emailFrom, [emailTo], connection=conn) msg.send() runinfo.emailcount = 1 + runinfo.emailcount runinfo.emailsent = datetime.now() runinfo.lastemailerror = "OK, accepted by server" runinfo.save() return True except smtplib.SMTPRecipientsRefused: runinfo.lastemailerror = "SMTP Recipient Refused" except smtplib.SMTPHeloError: runinfo.lastemailerror = "SMTP Helo Error" except smtplib.SMTPSenderRefused: runinfo.lastemailerror = "SMTP Sender Refused" except smtplib.SMTPDataError: runinfo.lastemailerror = "SMTP Data Error" runinfo.save() return False
def forward_mail_to_subscribers(self, mail, subscribers, mylist): # actually forward it try: connection = SMTPConnection( ) # we use the django one, configured with stuff from settings connection.open() if not connection.connection: raise SMTPException, "Fail to create SMTPConnection.connection" connection.connection.sendmail( mylist.list_address_extended( 'owner'), # from, will be set in Return-Path subscribers, # to mail.as_string()) # content except SMTPException, detail: err('SMTP Error while sending the email: %s' % detail) return # TODO : prevent loops if smtp fails : the mail stays in incoming and will be tried again in loop
def save(self, subject=None): if subject is None: subject = '%s wants you to learn more about out this Vibha Project' % self.cleaned_data[ 'name'] message = self.cleaned_data['body'] + self.cleaned_data['project_info'] from_email = settings.DEFAULT_FROM_EMAIL # Split into several messages so recipients dont see the other recipients msg = [] for recipient in self.cleaned_data['to_email']: m = EmailMessage(subject, message, from_email, [recipient]) m.content_subtype = "html" msg.append(m) connection = SMTPConnection() connection.send_messages(msg)
def render_to_mail(template_path, params, subject, from_email=None, recipient_list=[], auth_user=None, auth_password=None, fail_silently=False, content_subtype=None, attachments=[], **kwargs): """ Sends a e-mail message with content parsed from a template. The syntax is the same of render_to_response function. Returns then boolean of mail sending, using core.mail.send_mail function. """ content_subtype = content_subtype or getattr( settings, 'DEFAULT_EMAIL_CONTENT_SUBTYPE', 'html') from_email = from_email or getattr(settings, 'DEFAULT_FROM_EMAIL', None) if not recipient_list: return False # Loads template for mail content t = get_template(template_path) # Render content message = t.render(Context(params)) # SMTP connection connection = SMTPConnection(username=auth_user, password=auth_password, fail_silently=fail_silently) # Email object email_obj = EmailMessage(subject, message, from_email, [r.strip() for r in recipient_list], connection=connection) email_obj.content_subtype = content_subtype if attachments: for att in attachments: email_obj.attach_file(att) return email_obj.send()
def send_all(frequency): """ Sends an e-mail to all subscribers in the system with data with the given frequency. """ conn = SMTPConnection() # Use default settings. count = 0 start_date = datetime.date.today() - datetime.timedelta(days=frequency) for alert in EmailAlert.active_objects.filter(frequency=frequency): try: place_name, text_content, html_content = email_for_subscription(alert, start_date, frequency) except NoNews: continue subject = 'Update: %s' % place_name message = EmailMultiAlternatives(subject, text_content, settings.GENERIC_EMAIL_SENDER, [alert.user.email], connection=conn) message.attach_alternative(html_content, 'text/html') message.send() count += 1
def bcc_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None): from django.core.mail import SMTPConnection, EmailMessage ''' Sends mail with recipient_list BCC'ed. Follows BCC convention and sets "to" to from_email. ''' conn = SMTPConnection(username=auth_user, password=auth_password, fail_silently=fail_silently) return EmailMessage(subject, message, from_email, [from_email], recipient_list, conn).send()
def handle_trial_account(document, pdf_file_contents): """ Special handling for trial accounts: Instead of creating a PDF assert we're sending the PDF file as attachment in email. The document (and all dependent assets) are deleted """ try: # use is_active flag to determine the trial account if document.owner.is_active: return False subject = render_to_string('core/trial_return_email_subject.txt') # Email subject *must not* contain newlines subject = ''.join(subject.splitlines()) body = render_to_string('core/trial_return_email.txt', { 'user': document.owner.email, 'free_pages': "100" }) #TODO parametrize message = EmailMessage(subject, body, settings.UPLOAD_NOTIFICATION_EMAIL, [document.owner.email], connection=SMTPConnection()) # walk up the tree of asset dependencies, find the original asset that stores # the filename page_original_asset = document.pages.all()[0].get_asset( models.AssetClass.PAGE_ORIGINAL) original = page_original_asset.parent message.attach(original.orig_file_name + '-digitised.pdf', pdf_file_contents) message.send() return True except Exception, e: logging.error(e) import traceback logging.error(traceback.format_stack())
def refuse(self, m, mylist, reason): deepdbg('Refuse mail for %s' % unicode(mylist)) msgfrom = mylist.list_address_extended('owner') msgto = m['From'] reason = u"\n\nYour message to the list %s has been refused\n\n" % unicode( mylist) + reason # create mail mail = MIMEMultipart() mail[ 'Subject'] = u"Your message to the list %s has been refused " % unicode( mylist) mail['From'] = msgfrom mail['To'] = msgto mail.attach(MIMEText(reason, 'plain', 'utf8')) # error msg mail.attach(m) # previous mail # ugly hack we need to add headers at the TOP # items = m.items() # for key,val in items: m.__delitem__(key) # delete them all # for key,val in items: m.add_header(key,val) # re-add them all # mail.attach(m) # add original mail # send it try: connection = SMTPConnection( ) # we use the django one, configured with stuff from settings connection.open() if not connection.connection: raise SMTPException, "Fail to create SMTPConnection.connection" connection.connection.sendmail( mylist.list_address_extended( 'owner'), # from, will be set in Return-Path [msgto], # to mail.as_string()) # content except SMTPException, detail: err('refuse: SMTP Error while sending the email: %s' % detail) return # TODO : prevent loops if smtp fails : the mail stays in incoming and will be tried again in loop
def handle(self, *args, **options): if not options["group_slug"]: sys.exit(__doc__) self._set_language(options["lang"]) group = self._get_group(options["group_slug"]) accounts = self._get_accounts(group) emails = self._build_emails(accounts) if not options["yes"] and not options["debug"]: answer = input("Send emails y/N ") if answer.lower() != "y": print("Canceled sending") sys.exit(0) if emails: if options["debug"]: self._print_debug(emails) else: connection = SMTPConnection() connection.send_messages(emails)
def handle(self, *args, **options): if not options['group_slug']: sys.exit(__doc__) self._set_language(options['lang']) group = self._get_group(options['group_slug']) accounts = self._get_accounts(group) emails = self._build_emails(accounts) if not options['yes'] and not options['debug']: answer = raw_input(u"Send emails y/N ".encode('utf-8')) if answer.lower() != 'y': print "Canceled sending" sys.exit(0) if emails: if options['debug']: self._print_debug(emails) else: connection = SMTPConnection() connection.send_messages(emails)
import smtplib import logging from lockfile import FileLock, AlreadyLocked, LockTimeout from socket import error as socket_error from django.conf import settings from django.core.mail import send_mail as core_send_mail from django.core.mail import mail_admins try: # Django 1.2 from django.core.mail import get_connection except ImportError: # ImportError: cannot import name get_connection from django.core.mail import SMTPConnection get_connection = lambda backend=None, fail_silently=False, **kwds: SMTPConnection( fail_silently=fail_silently) from mailer.models import Message, DontSendEntry, MessageLog # when queue is empty, how long to wait (in seconds) before checking again EMPTY_QUEUE_SLEEP = getattr(settings, "MAILER_EMPTY_QUEUE_SLEEP", 30) # lock timeout value. how long to wait for the lock to become available. # default behavior is to never wait for the lock to be available. LOCK_WAIT_TIMEOUT = getattr(settings, "MAILER_LOCK_WAIT_TIMEOUT", -1) # The actual backend to use for sending, defaulting to the Django default. EMAIL_BACKEND = getattr(settings, "MAILER_EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend")
def handle(self, *args, **options): if len(args) != 0: raise CommandError(_("Command doesn't accept any arguments")) by_domain = options.get('by_domain') domain_name = options.get('domain_name') copy_admin = options.get('copy_admin') period = options.get('period') include_daily = options.get('include_daily') enddate = None period_re = re.compile(r"(?P<num>(\d+))\s+(?P<period>(day|week|month))(?:s)?") if period: match = period_re.match(period) if not match: raise CommandError(_("The period you specified is invalid")) num = match.group('num') ptype = match.group('period') if not ptype.endswith('s'): ptype = ptype + 's' delta = datetime.timedelta(**{ptype: int(num)}) enddate = datetime.date.today() - delta from django.db.models import Count, Sum, Q from django.template.defaultfilters import filesizeformat from django.core.mail import EmailMessage, SMTPConnection from django.contrib.auth.models import User from django.conf import settings from django.template.loader import render_to_string from baruwa.accounts.models import UserProfile, UserAddresses from baruwa.messages.models import Message from baruwa.messages.templatetags.messages_extras import tds_trunc from baruwa.messages.models import MessageTotals from baruwa.utils.graphs import PieChart, PIE_CHART_COLORS, BarChart from reportlab.lib import colors from reportlab.lib.styles import getSampleStyleSheet from reportlab.lib.units import inch from reportlab.platypus import SimpleDocTemplate, Spacer, Table, \ TableStyle, Paragraph, Image, PageBreak try: from django.forms.fields import email_re except ImportError: from django.core.validators import email_re try: from cStringIO import StringIO except ImportError: from StringIO import StringIO table_style = TableStyle([ ('FONT', (0, 0), (-1, -1), 'Helvetica'), ('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'), ('FONTSIZE', (0, 0), (-1, -1), 8), ('GRID', (0, 0), (-1, -1), 0.15, colors.black), ('ALIGN', (0, 0), (-1, 0), 'CENTER'), ('ALIGN', (4, 1), (-1, -1), 'CENTER'), ('ALIGN', (0, 0), (0, -1), 'CENTER'), ('VALIGN', (4, 1), (-1, -1), 'MIDDLE'), ('SPAN', (4, 1), (-1, -1)), ]) styles = getSampleStyleSheet() reports = [ [ 'from_address', {'from_address__exact': ""}, 'num_count', 'Top senders by quantity'], [ 'from_address', {'from_address__exact': ""}, 'total_size', 'Top senders by volume'], [ 'from_domain', {'from_domain__exact': ""}, 'num_count', 'Top sender domains by quantity'], [ 'from_domain', {'from_domain__exact': ""}, 'total_size', 'Top sender domains by volume'], [ 'to_address', {'to_address__exact': ""}, 'num_count', 'Top recipients by quantity'], [ 'to_address', {'to_address__exact': ""}, 'total_size', 'Top recipients by volume'], [ 'to_domain', {'to_domain__exact': "", 'to_domain__isnull': False}, 'num_count', 'Top recipient domains by quantity'], [ 'to_domain', {'to_domain__exact': "", 'to_domain__isnull': False}, 'total_size', 'Top recipient domains by volume'], ] emails = [] admin_addrs = [] if copy_admin: mails = User.objects.values('email').filter(is_superuser=True) admin_addrs = [mail['email'] for mail in mails] from_email = getattr(settings, 'DEFAULT_FROM_EMAIL', 'postmaster@localhost') url = getattr(settings, 'QUARANTINE_REPORT_HOSTURL', '') logo_dir = getattr(settings, 'MEDIA_ROOT', '') img = Image(logo_dir + '/imgs/css/logo.jpg') def build_chart(data, column, order, title): "build chart" headings = [('', _('Address'), _('Count'), _('Volume'), '')] rows = [[draw_square(PIE_CHART_COLORS[index]), tds_trunc(row[column], 45), row['num_count'], filesizeformat(row['total_size']), ''] for index, row in enumerate(data)] if len(rows) != 10: missing = 10 - len(rows) add_rows = [ ('', '', '', '', '') for ind in range(missing) ] rows.extend(add_rows) headings.extend(rows) dat = [row[order] for row in data] total = sum(dat) labels = [ ("%.1f%%" % ((1.0 * row[order] / total) * 100)) for row in data ] pie = PieChart() pie.chart.labels = labels pie.chart.data = dat headings[1][4] = pie table_with_style = Table(headings, [0.2 * inch, 2.8 * inch, 0.5 * inch, 0.7 * inch, 3.2 * inch]) table_with_style.setStyle(table_style) paragraph = Paragraph(title, styles['Heading1']) return [paragraph, table_with_style] def build_parts(account, enddate, isdom=None): "build parts" parts = [] sentry = 0 for report in reports: column = report[0] exclude_kwargs = report[1] order_by = "-%s" % report[2] order = report[2] title = report[3] if isdom: #dom data = Message.objects.values(column).\ filter(Q(from_domain=account.address) | \ Q(to_domain=account.address)).\ exclude(**exclude_kwargs).annotate( num_count=Count(column), total_size=Sum('size') ).order_by(order_by) if enddate: data.filter(date__gt=enddate) data = data[:10] else: #all users data = Message.report.all(user, enddate).values(column).\ exclude(**exclude_kwargs).annotate( num_count=Count(column), total_size=Sum('size') ).order_by(order_by) data = data[:10] if data: sentry += 1 pgraphs = build_chart(data, column, order, title) parts.extend(pgraphs) parts.append(Spacer(1, 70)) if (sentry % 2) == 0: parts.append(PageBreak()) parts.append(Paragraph(_('Message Totals'), styles['Heading1'])) if isdom: #doms msg_totals = MessageTotals.objects.doms(account.address, enddate) else: #norm filters = [] addrs = [ addr.address for addr in UserAddresses.objects.filter( user=account ).exclude(enabled__exact=0)] if enddate: efilter = { 'filter': 3, 'field': 'date', 'value': str(enddate) } filters.append(efilter) msg_totals = MessageTotals.objects.all( account, filters, addrs, profile.account_type) mail_total = [] spam_total = [] virus_total = [] dates = [] if include_daily: rows = [( Table([[draw_square(colors.white), Paragraph('Date', styles["Heading6"])]], [0.35 * inch, 1.50 * inch,]), Table([[draw_square(colors.green), Paragraph('Mail totals', styles["Heading6"])]], [0.35 * inch, 1.50 * inch,]), Table([[draw_square(colors.pink), Paragraph('Spam totals', styles["Heading6"])]], [0.35 * inch, 1.50 * inch,]), Table([[draw_square(colors.red), Paragraph('Virus totals', styles["Heading6"])]], [0.35 * inch, 1.50 * inch,]), )] for ind, msgt in enumerate(msg_totals): if ind % 10: dates.append('') else: dates.append(str(msgt.date)) mail_total.append(int(msgt.mail_total)) spam_total.append(int(msgt.spam_total)) virus_total.append(int(msgt.virus_total)) if include_daily: rows.append((str(msgt.date), msgt.mail_total, msgt.spam_total, msgt.virus_total)) graph = BarChart() graph.chart.data = [ tuple(mail_total), tuple(spam_total), tuple(virus_total) ] graph.chart.categoryAxis.categoryNames = dates graph_table = Table([[graph]], [7.4 * inch]) parts.append(graph_table) if include_daily: rows.append(('Totals', sum(mail_total), sum(spam_total), sum(virus_total))) parts.append(Spacer(1, 20)) graph_table = Table(rows, [1.85 * inch, 1.85 * inch, 1.85 * inch, 1.85 * inch,]) graph_table.setStyle(TableStyle([ ('FONTSIZE', (0, 0), (-1, -1), 8), ('FONT', (0, 0), (-1, -1), 'Helvetica'), ('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'), ('GRID', (0, 0), (-1, -1), 0.15, colors.black), ('FONT', (0, -1), (-1, -1), 'Helvetica-Bold'), #('BACKGROUND', (0, -1), (-1, -1), colors.green), ])) parts.append(graph_table) return parts def build_pdf(charts): "Build a PDF" pdf = StringIO() doc = SimpleDocTemplate(pdf, topMargin=50, bottomMargin=18) logo = [(img, _('Baruwa mail report'))] logo_table = Table(logo, [2.0 * inch, 5.4 * inch]) logo_table.setStyle(TableStyle([ ('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'), ('ALIGN', (0, 0), (-1, 0), 'LEFT'), ('ALIGN', (1, 0), (-1, 0), 'RIGHT'), ('FONTSIZE', (1, 0), (-1, 0), 10), ('LINEBELOW', (0, 0), (-1, -1), 0.15, colors.black), ])) parts = [logo_table] parts.append(Spacer(1, 20)) parts.extend(charts) try: doc.build(parts) except IndexError: pass return pdf def gen_email(pdf, user, owner): "generate and return email" text_content = render_to_string('reports/pdf_report.txt', {'user': user, 'url': url}) subject = _('Baruwa usage report for: %(user)s') % { 'user': owner} if email_re.match(user.username): toaddr = user.username if email_re.match(user.email): toaddr = user.email if admin_addrs: msg = EmailMessage(subject, text_content, from_email, [toaddr], admin_addrs) else: msg = EmailMessage(subject, text_content, from_email, [toaddr]) msg.attach('baruwa.pdf', pdf.getvalue(), "application/pdf") print _("* Queue %(user)s's report to: %(addr)s") % { 'user': owner, 'addr': toaddr} pdf.close() return msg print _("=================== Processing reports ======================") if by_domain: #do domain query #print "camacamlilone" domains = UserAddresses.objects.filter(Q(enabled=1), Q(address_type=1)) if domain_name != 'all': domains = domains.filter(address=domain_name) if not domains: print _("========== domain name %(dom)s does not exist ==========") % { 'dom': domain_name } for domain in domains: if email_re.match(domain.user.email) or email_re.match(domain.user.username): parts = build_parts(domain, enddate, True) if parts: pdf = build_pdf(parts) email = gen_email(pdf, domain.user, domain.address) emails.append(email) else: #do normal query profiles = UserProfile.objects.filter(send_report=1) for profile in profiles: try: user = profile.user if email_re.match(user.email) or email_re.match(user.username): parts = build_parts(user, enddate, False) if parts: pdf = build_pdf(parts) email = gen_email(pdf, user, user.username) emails.append(email) except User.DoesNotExist: pass if emails: try: conn = SMTPConnection() conn.send_messages(emails) print _("====== sending %(num)s messages =======") % { 'num': str(len(emails))} except Exception, exception: print _("Sending failed ERROR: %(error)s") % {'error': str(exception)}
def _send_verify_email(request, preferences, db_entry, rnd_hash, new_entry): """ Send a verify email """ location = reverse("KursAnmeldung-verify_email", kwargs={"hash": rnd_hash}) verify_link = request.build_absolute_uri(location) # FIXME: convert to users local time. now = datetime.datetime.utcnow() email_context = { "verify_link": verify_link, "db_entry": db_entry, "now": now, } # Render the internal page emailtext = render_to_string("kurs_anmeldung/verify_mailtext.txt", email_context) # Get the preferences from the database: raw_notify_list = preferences["notify"] notify_list = raw_notify_list.splitlines() notify_list = [i.strip() for i in notify_list if i] email_kwargs = { "from_email": preferences["from_email"], "subject": preferences["email_subject"], "body": emailtext, "to": [db_entry.email], "bcc": notify_list, } if MAIL_DEBUG == True: msg = u"MAIL_DEBUG is on: No Email was sended!" request.page_msg(msg) db_entry.log(request, msg) db_entry.mail_sended = False request.page_msg("django.core.mail.EmailMessage kwargs:") request.page_msg(email_kwargs) request.page_msg("debug mail text:") request.page_msg(mark_safe("<pre>%s</pre>" % emailtext)) return # We can't use django.core.mail.send_mail, because all members # of the recipient list will see the others in the 'To' field. # But we would like to notify the admins via 'Bcc' field. connection = SMTPConnection(fail_silently=False) email = EmailMessage(**email_kwargs) try: sended = email.send(fail_silently=False) except Exception, err: msg = "Error sending mail: %s" % err LogEntry.objects.log_action(app_label="kurs_anmeldung", action="error", message=msg) db_entry.log(request, msg) db_entry.mail_sended = False if settings.DEBUG or request.user.is_staff: db_entry.save() raise
def create_campaign_message(request): if request.method == 'POST': if not 'project' in request.session: project = Project.objects.get(id=int(request.POST['pid'])) funding_detail = project.projectfundingdetail_set.latest( 'end_date') funding_detail_value = long(funding_detail.budget) to_session = {'project': project, 'funding_detail': funding_detail} request.session.update(to_session) else: project = request.session['project'] funding_detail = request.session['funding_detail'] funding_detail_value = long(funding_detail.budget) ''' if request.POST.has_key('confirmed'): form = DonationAmountForm(data=request.POST,request=request,total_amount=request.session['donating_amount']) if not form.is_valid(): return render_to_response('donorportal/create_campaign2.html',{'form':form},RequestContext(request)) assert form.cleaned_data['amount'] == request.session['donating_amount'] request.session['donated_amount'] = form.cleaned_data['amount'] ''' #Message form post request form = MessageForm() if request.POST.get('form-type') == 'message-form': form = MessageForm(request.POST) if form.is_valid(): values = {} values.update(request.session) values.update(form.cleaned_data) camp_values = { 'user': request.user, 'amount': int(values['campaign_amount']), #This is 0, but the added donation, auto adds that value to donated. 'amount_collected': 0, 'description': values['description'], 'project': values['project'], 'end_date': values['campaign_end_date'] } del request.session['project'] camp = Campaign.objects.create(**camp_values) #donation = Donation() #donation.campaign = camp #donation.user = request.user #donation.amount = long(values['donated_amount']) #donation.save() extra_context = { 'site': Site.objects.get_current(), 'campaign': camp } extra_context.update(values) mail_context = RequestContext(request, extra_context) mail_message = loader.render_to_string( 'email_templates/campaign_mail.txt', mail_context) subject = '%s is raising funds for %s project on Vibha' % ( request.user.get_full_name(), values['project'].name) from_email = settings.DEFAULT_FROM_EMAIL # Split into several messages so recipients dont see the other recipients msg = [] for recipient in form.cleaned_data['to_email_field']: m = EmailMessage(subject, mail_message, from_email, [recipient]) m.content_subtype = "html" msg.append(m) connection = SMTPConnection() connection.send_messages(msg) return HttpResponseRedirect('../campaign/%s/' % (camp.id)) payload = locals() return render_to_response('donorportal/create_campaign3.html', payload, RequestContext(request)) else: return HttpResponseRedirect(reverse('list_projects'))