Guest User

Untitled

a guest
Nov 5th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. message_dict = { 'Data':
  2. 'From: ' + mail_sender + 'n'
  3. 'To: ' + mail_receivers_list + 'n'
  4. 'Subject: ' + mail_subject + 'n'
  5. 'MIME-Version: 1.0n'
  6. 'Content-Type: text/html;nn' +
  7. mail_content}
  8.  
  9. response = client.send_raw_email(
  10. Destinations=[
  11. ],
  12. FromArn='',
  13. RawMessage=message_dict,
  14. ReturnPathArn='',
  15. Source='',
  16. SourceArn='',
  17. )
  18.  
  19. from email.mime.application import MIMEApplication
  20. from email.mime.multipart import MIMEMultipart
  21. from email.mime.text import MIMEText
  22.  
  23. import boto.ses
  24.  
  25.  
  26. AWS_ACCESS_KEY = 'HEREYOURACCESSKEY'
  27. AWS_SECRET_KEY = 'HEREYOURSECRETKEY'
  28.  
  29. class Email(object):
  30.  
  31. def __init__(self, to, subject):
  32. self.to = to
  33. self.subject = subject
  34. self.text = None
  35. self.attachment = None
  36.  
  37.  
  38. def text(self, text):
  39. self.text = text
  40.  
  41. def add_attachment(self, attachment):
  42. self.attachment = attachment
  43.  
  44. def send(self, from_addr=None, file_name = None):
  45.  
  46. connection = boto.ses.connect_to_region(
  47. 'us-east-1',
  48. aws_access_key_id=AWS_ACCESS_KEY,
  49. aws_secret_access_key=AWS_SECRET_KEY
  50. )
  51. msg = MIMEMultipart()
  52. msg['Subject'] = self.subject
  53. msg['From'] = from_addr
  54. msg['To'] = self.to
  55.  
  56. part = MIMEApplication(self.attachment)
  57. part.add_header('Content-Disposition', 'attachment', filename=file_name)
  58. part.add_header('Content-Type', 'application/vnd.ms-excel; charset=UTF-8')
  59.  
  60. msg.attach(part)
  61.  
  62. # the message body
  63. part = MIMEText(self.text)
  64. msg.attach(part)
  65.  
  66. return connection.send_raw_email(msg.as_string(),source=from_addr,destinations=self.to)
  67.  
  68. if __name__ == "__main__":
  69. email = Email(to='toMail@gmail.com', subject='Your subject!')
  70. email.text('This is a text body.')
  71. #you could use StringIO.StringIO() to get the file value
  72. email.add_attachment(yourFileValue)
  73. email.send(from_addr='from@mail.com',file_name="yourFile.txt")
  74.  
  75. import os
  76. import boto3
  77. from email.mime.multipart import MIMEMultipart
  78. from email.mime.text import MIMEText
  79. from email.mime.application import MIMEApplication
  80.  
  81.  
  82. def create_multipart_message(
  83. sender: str, recipients: list, title: str, text: str=None, html: str=None, attachments: list=None)
  84. -> MIMEMultipart:
  85. """
  86. Creates a MIME multipart message object.
  87. Uses only the Python `email` standard library.
  88. Emails, both sender and recipients, can be just the email string or have the format 'The Name <the_email@host.com>'.
  89.  
  90. :param sender: The sender.
  91. :param recipients: List of recipients. Needs to be a list, even if only one recipient.
  92. :param title: The title of the email.
  93. :param text: The text version of the email body (optional).
  94. :param html: The html version of the email body (optional).
  95. :param attachments: List of files to attach in the email.
  96. :return: A `MIMEMultipart` to be used to send the email.
  97. """
  98. multipart_content_subtype = 'alternative' if text and html else 'mixed'
  99. msg = MIMEMultipart(multipart_content_subtype)
  100. msg['Subject'] = title
  101. msg['From'] = sender
  102. msg['To'] = ', '.join(recipients)
  103.  
  104. # Record the MIME types of both parts - text/plain and text/html.
  105. # According to RFC 2046, the last part of a multipart message, in this case the HTML message, is best and preferred.
  106. if text:
  107. part = MIMEText(text, 'plain')
  108. msg.attach(part)
  109. if html:
  110. part = MIMEText(html, 'html')
  111. msg.attach(part)
  112.  
  113. # Add attachments
  114. for attachment in attachments or []:
  115. with open(attachment, 'rb') as f:
  116. part = MIMEApplication(f.read())
  117. part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(attachment))
  118. msg.attach(part)
  119.  
  120. return msg
  121.  
  122.  
  123. def send_mail(
  124. sender: str, recipients: list, title: str, text: str=None, html: str=None, attachments: list=None) -> dict:
  125. """
  126. Send email to recipients. Sends one mail to all recipients.
  127. The sender needs to be a verified email in SES.
  128. """
  129. msg = create_multipart_message(sender, recipients, title, text, html, attachments)
  130. ses_client = boto3.client('ses') # Use your settings here
  131. return ses_client.send_raw_email(
  132. Source=sender,
  133. Destinations=recipients,
  134. RawMessage={'Data': msg.as_string()}
  135. )
  136.  
  137.  
  138. if __name__ == '__main__':
  139. sender_ = 'The Sender <the_sender@email.com>'
  140. recipients_ = ['Recipient One <recipient_1@email.com>', 'recipient_2@email.com']
  141. title_ = 'Email title here'
  142. text_ = 'The text versionnwith multiple lines.'
  143. body_ = """<html><head></head><body><h1>A header 1</h1><br>Some text."""
  144. attachments_ = ['/path/to/file1/filename1.txt', '/path/to/file2/filename2.txt']
  145.  
  146. response_ = send_mail(sender_, recipients_, title_, text_, body_, attachments_)
  147. print(response_)
Add Comment
Please, Sign In to add comment