Basic message object. A message object is defined as something that has a bunch of RFC 2822 headers and a payload. It may optionally have an envelope header (a.k.a. Unix-From or From_ header). If the message is a container (i.e. a multipart or a message/rfc822), then the payl
| 133 | |
| 134 | |
| 135 | class Message: |
| 136 | """Basic message object. |
| 137 | |
| 138 | A message object is defined as something that has a bunch of RFC 2822 |
| 139 | headers and a payload. It may optionally have an envelope header |
| 140 | (a.k.a. Unix-From or From_ header). If the message is a container (i.e. a |
| 141 | multipart or a message/rfc822), then the payload is a list of Message |
| 142 | objects, otherwise it is a string. |
| 143 | |
| 144 | Message objects implement part of the `mapping' interface, which assumes |
| 145 | there is exactly one occurrence of the header per message. Some headers |
| 146 | do in fact appear multiple times (e.g. Received) and for those headers, |
| 147 | you must use the explicit API to set or get all the headers. Not all of |
| 148 | the mapping methods are implemented. |
| 149 | """ |
| 150 | def __init__(self, policy=compat32): |
| 151 | self.policy = policy |
| 152 | self._headers = [] |
| 153 | self._unixfrom = None |
| 154 | self._payload = None |
| 155 | self._charset = None |
| 156 | # Defaults for multipart messages |
| 157 | self.preamble = self.epilogue = None |
| 158 | self.defects = [] |
| 159 | # Default content type |
| 160 | self._default_type = 'text/plain' |
| 161 | |
| 162 | def __str__(self): |
| 163 | """Return the entire formatted message as a string. |
| 164 | """ |
| 165 | return self.as_string() |
| 166 | |
| 167 | def as_string(self, unixfrom=False, maxheaderlen=0, policy=None): |
| 168 | """Return the entire formatted message as a string. |
| 169 | |
| 170 | Optional 'unixfrom', when true, means include the Unix From_ envelope |
| 171 | header. For backward compatibility reasons, if maxheaderlen is |
| 172 | not specified it defaults to 0, so you must override it explicitly |
| 173 | if you want a different maxheaderlen. 'policy' is passed to the |
| 174 | Generator instance used to serialize the message; if it is not |
| 175 | specified the policy associated with the message instance is used. |
| 176 | |
| 177 | If the message object contains binary data that is not encoded |
| 178 | according to RFC standards, the non-compliant data will be replaced by |
| 179 | unicode "unknown character" code points. |
| 180 | """ |
| 181 | from email.generator import Generator |
| 182 | policy = self.policy if policy is None else policy |
| 183 | fp = StringIO() |
| 184 | g = Generator(fp, |
| 185 | mangle_from_=False, |
| 186 | maxheaderlen=maxheaderlen, |
| 187 | policy=policy) |
| 188 | g.flatten(self, unixfrom=unixfrom) |
| 189 | return fp.getvalue() |
| 190 | |
| 191 | def __bytes__(self): |
| 192 | """Return the entire formatted message as a bytes object. |
no outgoing calls
no test coverage detected