3
3
from email .mime .nonmultipart import MIMENonMultipart
4
4
from email .utils import formatdate , formataddr
5
5
from email .utils import make_msgid
6
- from email import encoders
6
+ from email import encoders , charset
7
7
from email .header import Header
8
8
9
9
from .models import QueuedMail
@@ -15,7 +15,14 @@ def _encoded_email_header(name, email):
15
15
return email
16
16
17
17
18
- def send_simple_mail (sender , receiver , subject , msgtxt , attachments = None , usergenerated = False , cc = None , replyto = None , sendername = None , receivername = None , messageid = None , suppress_auto_replies = True , is_auto_reply = False ):
18
+ # Default for utf-8 in python is to encode subject with "shortest" and body with "base64". For our texts,
19
+ # make it always quoted printable, for easier reading and testing.
20
+ _utf8_charset = charset .Charset ('utf-8' )
21
+ _utf8_charset .header_encoding = charset .QP
22
+ _utf8_charset .body_encoding = charset .QP
23
+
24
+
25
+ def send_simple_mail (sender , receiver , subject , msgtxt , attachments = None , usergenerated = False , cc = None , replyto = None , sendername = None , receivername = None , messageid = None , suppress_auto_replies = True , is_auto_reply = False , htmlbody = None , headers = {}):
19
26
# attachment format, each is a tuple of (name, mimetype,contents)
20
27
# content should be *binary* and not base64 encoded, since we need to
21
28
# use the base64 routines from the email library to get a properly
@@ -44,14 +51,27 @@ def send_simple_mail(sender, receiver, subject, msgtxt, attachments=None, userge
44
51
elif not usergenerated :
45
52
msg ['Auto-Submitted' ] = 'auto-generated'
46
53
47
- msg .attach (MIMEText (msgtxt , _charset = 'utf-8' ))
54
+ for h in headers .keys ():
55
+ msg [h ] = headers [h ]
56
+
57
+ if htmlbody :
58
+ mpart = MIMEMultipart ("alternative" )
59
+ mpart .attach (MIMEText (msgtxt , _charset = _utf8_charset ))
60
+ mpart .attach (MIMEText (htmlbody , 'html' , _charset = _utf8_charset ))
61
+ msg .attach (mpart )
62
+ else :
63
+ # Just a plaintext body, so append it directly
64
+ msg .attach (MIMEText (msgtxt , _charset = 'utf-8' ))
48
65
49
66
if attachments :
50
- for filename , contenttype , content in attachments :
51
- main , sub = contenttype .split ('/' )
67
+ for a in attachments :
68
+ main , sub = a [ ' contenttype' ] .split ('/' )
52
69
part = MIMENonMultipart (main , sub )
53
- part .set_payload (content )
54
- part .add_header ('Content-Disposition' , 'attachment; filename="%s"' % filename )
70
+ part .set_payload (a ['content' ])
71
+ part .add_header ('Content-Disposition' , a .get ('disposition' , 'attachment; filename="%s"' % a ['filename' ]))
72
+ if 'id' in a :
73
+ part .add_header ('Content-ID' , a ['id' ])
74
+
55
75
encoders .encode_base64 (part )
56
76
msg .attach (part )
57
77
0 commit comments