|
| 1 | +import base64 |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import mailtrap as mt |
| 5 | + |
| 6 | +API_TOKEN = "<YOUR_API_TOKEN>" |
| 7 | + |
| 8 | + |
| 9 | +class SendingType: |
| 10 | + DEFAULT = "default" |
| 11 | + BULK = "bulk" |
| 12 | + SANDBOX = "sandbox" |
| 13 | + |
| 14 | + |
| 15 | +def get_client(type_: SendingType) -> mt.MailtrapClient: |
| 16 | + if type_ == SendingType.DEFAULT: |
| 17 | + return mt.MailtrapClient(token=API_TOKEN) |
| 18 | + elif type_ == SendingType.BULK: |
| 19 | + return mt.MailtrapClient(token=API_TOKEN, bulk=True) |
| 20 | + elif type_ == SendingType.SANDBOX: |
| 21 | + return mt.MailtrapClient( |
| 22 | + token=API_TOKEN, |
| 23 | + sandbox=True, |
| 24 | + inbox_id="<YOUR_INBOX_ID>", |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +# Image should be in the same level in directory like this python file. |
| 29 | +welcome_image = Path(__file__).parent.joinpath("welcome.png").read_bytes() |
| 30 | + |
| 31 | +batch_mail = mt.BatchSendEmailParams( |
| 32 | + base=mt.BatchMail( |
| 33 | + sender=mt.Address(email="<SENDER_EMAIL>", name="<SENDER_NAME>"), |
| 34 | + cc=[mt.Address(email="cc@email.com", name="Copy to")], |
| 35 | + bcc=[mt.Address(email="bcc@email.com", name="Hidden Recipient")], |
| 36 | + subject="You are awesome!", |
| 37 | + text="Congrats for sending test email with Mailtrap!", |
| 38 | + html=""" |
| 39 | + <!doctype html> |
| 40 | + <html> |
| 41 | + <head> |
| 42 | + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 43 | + </head> |
| 44 | + <body style="font-family: sans-serif;"> |
| 45 | + <div style="display: block; margin: auto; max-width: 600px;" class="main"> |
| 46 | + <h1 style="font-size: 18px; font-weight: bold; margin-top: 20px"> |
| 47 | + Congrats for sending test email with Mailtrap! |
| 48 | + </h1> |
| 49 | + <p> |
| 50 | + Inspect it using the tabs you see above and |
| 51 | + learn how this email can be improved. |
| 52 | + </p> |
| 53 | + <img alt="Inspect with Tabs" src="cid:welcome.png" style="width: 100%;"> |
| 54 | + <p> |
| 55 | + Now send your email using our fake SMTP server |
| 56 | + and integration of your choice! |
| 57 | + </p> |
| 58 | + <p>Good luck! Hope it works.</p> |
| 59 | + </div> |
| 60 | + <!-- Example of invalid for email html/css, will be detected by Mailtrap: --> |
| 61 | + <style> |
| 62 | + .main { background-color: white; } |
| 63 | + a:hover { border-left-width: 1em; min-height: 2em; } |
| 64 | + </style> |
| 65 | + </body> |
| 66 | + </html> |
| 67 | + """, |
| 68 | + category="Test", |
| 69 | + attachments=[ |
| 70 | + mt.Attachment( |
| 71 | + content=base64.b64encode(welcome_image), |
| 72 | + filename="welcome.png", |
| 73 | + disposition=mt.Disposition.INLINE, |
| 74 | + mimetype="image/png", |
| 75 | + content_id="welcome.png", |
| 76 | + ) |
| 77 | + ], |
| 78 | + headers={"X-MT-Header": "Custom header"}, |
| 79 | + custom_variables={"year": 2023}, |
| 80 | + ), |
| 81 | + requests=[ |
| 82 | + mt.BatchEmailRequest( |
| 83 | + to=[mt.Address(email="<RECEIVER_EMAIL>")], |
| 84 | + ), |
| 85 | + ], |
| 86 | +) |
| 87 | + |
| 88 | + |
| 89 | +def batch_send( |
| 90 | + client: mt.MailtrapClient, |
| 91 | + mail: mt.BatchSendEmailParams, |
| 92 | +) -> mt.BATCH_SEND_ENDPOINT_RESPONSE: |
| 93 | + return client.batch_send(mail) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + client = get_client(SendingType.DEFAULT) |
| 98 | + print(batch_send(client, batch_mail)) |
0 commit comments