Advertisement
J2897

MailSlurp - Send

Jun 15th, 2024 (edited)
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. #!python3
  2. import mailslurp_client
  3. import os
  4.  
  5. """
  6. This Python script uses the MailSlurp service. Here's a brief explanation:
  7.  
  8.    1. It initializes an `InboxControllerApi` object to interact with the MailSlurp API.
  9.    2. It retrieves a specific inbox using the provided `inbox_id`.
  10.    3. It extracts the email address associated with that inbox.
  11.  
  12. The script then sends an email:
  13.  
  14.    1. It creates an email message with a subject and body.
  15.    2. It connects to MailSlurp's SMTP server and logs in with your credentials.
  16.    3. It then sends the email.
  17.  
  18. Note: Sending is blocked on free accounts, and trials cannot email non-mailslurp email addresses.
  19. """
  20.  
  21. # Create a MailSlurp configuration
  22. configuration = mailslurp_client.Configuration()
  23. configuration.api_key['x-api-key'] = os.getenv("MAILSLURP_API_KEY")
  24. inbox_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
  25. password = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  26. to_address = "name@example.com"
  27.  
  28. # Extract the email address associated with the inbox
  29. with mailslurp_client.ApiClient(configuration) as api_client:
  30.     inbox_controller = mailslurp_client.InboxControllerApi(api_client)
  31.     inbox = inbox_controller.get_inbox(inbox_id)
  32.     my_email_address = inbox.email_address
  33.  
  34. print("My email address:", my_email_address)
  35.  
  36. # Send an email to the MailSlurp email address
  37. import smtplib
  38. from email.mime.text import MIMEText
  39.  
  40. msg = MIMEText("Hello from MailSlurp!")
  41. msg['Subject'] = "Test email from MailSlurp"
  42. msg['From'] = my_email_address
  43. msg['To'] = to_address
  44.  
  45. server = smtplib.SMTP('mxslurp.click', 2525)
  46. server.starttls()
  47. server.login(my_email_address, password)
  48. server.sendmail(to_address, my_email_address, msg.as_string())
  49. server.quit()
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement