def send_mail(self, user): """ Sends this note as an email """ mailto = self.mailto() # Only send the same note once if self.has_sent_message(mailto): raise ValueError(_('Already sent message to %s') % mailto) config = Configuration.conf() smtp_host = config.get('smtp_host').split(':') settings.EMAIL_HOST = smtp_host[0] if len(smtp_host) > 1: settings.EMAIL_PORT = int(smtp_host[1]) settings.EMAIL_USE_TLS = config.get('smtp_ssl') settings.EMAIL_HOST_USER = str(config.get('smtp_user')) settings.EMAIL_HOST_PASSWORD = str(config.get('smtp_password')) headers = {} headers['Reply-To'] = self.sender headers['References'] = '%s.%s' % (self.code, self.sender) subject = u'%s (SRO#%s)' % (self.subject, self.code) if self.order: # Encode the SO code so that we can match replies to the SO # even if the original note has been deleted subject = u'%s (SRO#%s/%s)' % (self.subject, self.code, self.order.url_code) recipients = mailto.split(',') msg = EmailMessage(subject, self.body, self.sender, recipients, headers=headers) for f in self.attachments.all(): msg.attach_file(f.content.path) msg.send() for r in recipients: msg = Message(note=self, recipient=r, created_by=user, body=self.body) msg.sent_at = timezone.now() msg.sender = self.sender msg.status = 'SENT' msg.save() message = _(u'Message sent to %s') % mailto self.notify('email_sent', message, user) return message
def send_mail(self, user): """Sends this note as an email""" mailto = self.mailto() # Only send the same note once if self.has_sent_message(mailto): raise ValueError(_('Already sent message to %s') % mailto) config = Configuration.conf() smtp_host = config.get('smtp_host').split(':') settings.EMAIL_HOST = smtp_host[0] if len(smtp_host) > 1: settings.EMAIL_PORT = int(smtp_host[1]) if config.get('SMTP_ENCRYPTION') == 'TLS': settings.EMAIL_USE_TLS = True if config.get('SMTP_ENCRYPTION') == 'SSL': settings.EMAIL_USE_SSL = True if config.get('SMTP_ENCRYPTION') == 'OFF': settings.EMAIL_USE_SSL = False settings.EMAIL_USE_TLS = False settings.EMAIL_HOST_USER = str(config.get('smtp_user')) settings.EMAIL_HOST_PASSWORD = str(config.get('smtp_password')) headers = {} headers['Reply-To'] = self.sender headers['References'] = '%s.%s' % (self.code, self.sender) subject = u'%s (SRO#%s)' % (self.subject, self.code) if self.order: # Encode the SO code so that we can match replies to the SO # even if the original note has been deleted subject = u'%s (SRO#%s/%s)' % (self.subject, self.code, self.order.url_code) recipients = mailto.split(',') msg = EmailMessage(subject, self.body, self.sender, recipients, headers=headers) for f in self.attachments.all(): msg.attach_file(f.content.path) msg.send() for r in recipients: msg = Message(note=self, recipient=r, created_by=user, body=self.body) msg.sent_at = timezone.now() msg.sender = self.sender msg.status = 'SENT' msg.save() message = _(u'Message sent to %s') % mailto self.notify('email_sent', message, user) return message