| 125 | |
| 126 | # Create a drafty message with an *in-band* attachment. |
| 127 | def attachment(filename): |
| 128 | try: |
| 129 | f = open(filename, 'rb') |
| 130 | # Try to guess the mime type. |
| 131 | mimetype = mimetypes.guess_type(filename)[0] |
| 132 | data = base64.b64encode(f.read()) |
| 133 | # python3 fix. |
| 134 | if type(data) is not str: |
| 135 | data = data.decode() |
| 136 | result = { |
| 137 | 'fmt': [{'at': -1}], |
| 138 | 'ent': [{'tp': 'EX', 'data':{ |
| 139 | 'val': data, 'mime': mimetype, 'name':os.path.basename(filename) |
| 140 | }}] |
| 141 | } |
| 142 | f.close() |
| 143 | return result |
| 144 | except IOError as err: |
| 145 | stdoutln("Error processing attachment '" + filename + "':", err) |
| 146 | return None |
| 147 | |
| 148 | |
| 149 | # encode_to_bytes converts the 'src' to a byte array. |