Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- message_dict = { 'Data':
- 'From: ' + mail_sender + 'n'
- 'To: ' + mail_receivers_list + 'n'
- 'Subject: ' + mail_subject + 'n'
- 'MIME-Version: 1.0n'
- 'Content-Type: text/html;nn' +
- mail_content}
- response = client.send_raw_email(
- Destinations=[
- ],
- FromArn='',
- RawMessage=message_dict,
- ReturnPathArn='',
- Source='',
- SourceArn='',
- )
- from email.mime.application import MIMEApplication
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- import boto.ses
- AWS_ACCESS_KEY = 'HEREYOURACCESSKEY'
- AWS_SECRET_KEY = 'HEREYOURSECRETKEY'
- class Email(object):
- def __init__(self, to, subject):
- self.to = to
- self.subject = subject
- self.text = None
- self.attachment = None
- def text(self, text):
- self.text = text
- def add_attachment(self, attachment):
- self.attachment = attachment
- def send(self, from_addr=None, file_name = None):
- connection = boto.ses.connect_to_region(
- 'us-east-1',
- aws_access_key_id=AWS_ACCESS_KEY,
- aws_secret_access_key=AWS_SECRET_KEY
- )
- msg = MIMEMultipart()
- msg['Subject'] = self.subject
- msg['From'] = from_addr
- msg['To'] = self.to
- part = MIMEApplication(self.attachment)
- part.add_header('Content-Disposition', 'attachment', filename=file_name)
- part.add_header('Content-Type', 'application/vnd.ms-excel; charset=UTF-8')
- msg.attach(part)
- # the message body
- part = MIMEText(self.text)
- msg.attach(part)
- return connection.send_raw_email(msg.as_string(),source=from_addr,destinations=self.to)
- if __name__ == "__main__":
- email = Email(to='toMail@gmail.com', subject='Your subject!')
- email.text('This is a text body.')
- #you could use StringIO.StringIO() to get the file value
- email.add_attachment(yourFileValue)
- email.send(from_addr='from@mail.com',file_name="yourFile.txt")
- import os
- import boto3
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- from email.mime.application import MIMEApplication
- def create_multipart_message(
- sender: str, recipients: list, title: str, text: str=None, html: str=None, attachments: list=None)
- -> MIMEMultipart:
- """
- Creates a MIME multipart message object.
- Uses only the Python `email` standard library.
- Emails, both sender and recipients, can be just the email string or have the format 'The Name <the_email@host.com>'.
- :param sender: The sender.
- :param recipients: List of recipients. Needs to be a list, even if only one recipient.
- :param title: The title of the email.
- :param text: The text version of the email body (optional).
- :param html: The html version of the email body (optional).
- :param attachments: List of files to attach in the email.
- :return: A `MIMEMultipart` to be used to send the email.
- """
- multipart_content_subtype = 'alternative' if text and html else 'mixed'
- msg = MIMEMultipart(multipart_content_subtype)
- msg['Subject'] = title
- msg['From'] = sender
- msg['To'] = ', '.join(recipients)
- # Record the MIME types of both parts - text/plain and text/html.
- # According to RFC 2046, the last part of a multipart message, in this case the HTML message, is best and preferred.
- if text:
- part = MIMEText(text, 'plain')
- msg.attach(part)
- if html:
- part = MIMEText(html, 'html')
- msg.attach(part)
- # Add attachments
- for attachment in attachments or []:
- with open(attachment, 'rb') as f:
- part = MIMEApplication(f.read())
- part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(attachment))
- msg.attach(part)
- return msg
- def send_mail(
- sender: str, recipients: list, title: str, text: str=None, html: str=None, attachments: list=None) -> dict:
- """
- Send email to recipients. Sends one mail to all recipients.
- The sender needs to be a verified email in SES.
- """
- msg = create_multipart_message(sender, recipients, title, text, html, attachments)
- ses_client = boto3.client('ses') # Use your settings here
- return ses_client.send_raw_email(
- Source=sender,
- Destinations=recipients,
- RawMessage={'Data': msg.as_string()}
- )
- if __name__ == '__main__':
- sender_ = 'The Sender <the_sender@email.com>'
- recipients_ = ['Recipient One <recipient_1@email.com>', 'recipient_2@email.com']
- title_ = 'Email title here'
- text_ = 'The text versionnwith multiple lines.'
- body_ = """<html><head></head><body><h1>A header 1</h1><br>Some text."""
- attachments_ = ['/path/to/file1/filename1.txt', '/path/to/file2/filename2.txt']
- response_ = send_mail(sender_, recipients_, title_, text_, body_, attachments_)
- print(response_)
Add Comment
Please, Sign In to add comment