Replace email template Variables

Source: Internet
Author: User

The template variable replacement technique is often used when sending mail in batches, that is, a mail template with built-in variable identifier, and then a series of replicas Based on the recipient to generate the corresponding new mail. When the mail template format is fixed (mainly encoding, mail body format), it is easy to implement. Then, it is complicated to allow users to upload mail templates. The Code is as follows:

Import sys, OS, email, base64, email. header, quopri, reclass MailTpl (object): def _ init _ (self, mail_str): self. mail = mail_str self. mail_str = self. mail self. codetype = "UTF-8" self. analyze () def start (self): self. mail_str = self. mail def getmail (self): return self. mail_str def analyze (self): msg = email. message_from_string (self. mail_str) # parse the header self. mailfrom = msg. get ("from") self. mailto = msg. get ("to") self. subj = msg. get ("subject") self. msgid = msg. get ("message-id") # decode self from the header. mailfrom_x = email. header. decode_header (self. mailfrom) self. mailto_x = email. header. decode_header (self. mailto) self. subj_x = email. header. decode_header (self. subj) # parse the body (get the body text) self. text = [] for part in msg. walk (): if part. is_multipart (): continue content_type = part. get_content_type () if "text" in content_type: content_type_all = part ["content-type"] codetype = None if content_type_all: if "charset" in content_type_all: codetype = re. findall (r "charset = (. +) \ B ", content_type_all) [0] self. text. append (part. get_payload (), part ["Content-Transfer-Encoding"], codetype) # decode self. text_x = [] for txt in self. text: content, encoding, codetype = txt content_x = content if encoding = "base64": content_x = base64.decodestring (content) elif encoding = "quoted-printable ": content_x = quopri. decodestring (content) self. text_x.append (content_x, encoding, codetype) def newmsgid (self, msgid): self. mail_str = self. mail_str.replace ("d:" + self. msgid, "d:" + msgid) self. mail_str = self. mail_str.replace ("D:" + self. msgid, "D:" + msgid) def newfrom (self, name, addr): name_codetype = self. mailfrom_x [0] [1] new_name = str (email. header. make_header ([(name, self. codetype)]) new_addr = "<" + addr + ">" new_from = "% s" % (new_name, new_addr) self. mail_str = self. mail_str.replace ("m:" + self. mailfrom, "m:" + new_from) self. mail_str = self. mail_str.replace ("M:" + self. mailfrom, "M:" + new_from) def newto (self, name, addr): name_codetype = self. mailto_x [0] [1] new_name = str (email. header. make_header ([(name, self. codetype)]) new_addr = "<" + addr + ">" new_to = "% s" % (new_name, new_addr) self. mail_str = self. mail_str.replace ("o:" + self. mailto, "o:" + new_to) self. mail_str = self. mail_str.replace ("O:" + self. mailto, "O:" + new_to) def newsubj (self, params): new_subj_arr = [] for s in self. subj_x: content, codetype = s if codetype: content = content. decode (codetype) for p in params: k, v = p content = content. replace (k, v) new_subj_arr.append (content, codetype) new_subj = [] for s in new_subj_arr: content, codetype = s new_subj.append (str (email. header. make_header ([(content, self. codetype)]) new_subj = "". join (new_subj) self. mail_str = self. mail_str.replace ("t:" + self. subj, "t:" + new_subj) self. mail_str = self. mail_str.replace ("T:" + self. subj, "T:" + new_subj) def newbody (self, params): for I, t in enumerate (self. text_x): old_t = self. text [I] content, codetype, codetype = t for p in params: k, v = p content = content. replace (k, v) if codetype: content = content. encode (codetype) if codetype = "base64": content = base64.encodestring (content) elif codetype = "quoted-printable": content = quopri. encodestring (content) self. mail_str = self. mail_str.replace (old_t [0], content)

Knowledge points used:

1. parse the email by email. message_from_string. This method gets the key-value pairs of each email domain.

2. decodes the obtained domain values by email. header. emailHeader to get an array. each item is a tuples. the first item of the tuples is the decoded value, and the second item is encoded.

3. decode the mail body. Here there are two codes: content-type and charset)

4. The two strings in python27 must be converted to the same encoding before interoperation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.