Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. ''' Boilerplate for constructing an email with the Python SendGrid v3 API.
  2.  
  3. Example Usage:
  4.  
  5. mail = SendGridEmail(
  6. subject='Test Email',
  7. to_email='customer@email.com',
  8. from_email='test@superdupertest.com',
  9. html_template='test.html'
  10. )
  11.  
  12. mail.apply_subs([
  13. ('-name-', 'jimmy'),
  14. ('-company-', 'JimbosJellyBeans'),
  15. ])
  16.  
  17. mail.attach_item(
  18. file='test_file.txt',
  19. type_='text/plain',
  20. filename='Test File.txt',
  21. disposition='attachment',
  22. cid='TestFile'
  23. )
  24.  
  25. response = mail.send()
  26. print(response)
  27. '''
  28. import base64
  29.  
  30. import sendgrid
  31. from sendgrid.helpers.mail import \
  32. Email, Content, Mail, Substitution, Attachment
  33.  
  34. from secrets import SENDGRID_API_KEY
  35.  
  36.  
  37. class SendGridEmail:
  38. ''' A basic SendGrid email object constructor.
  39.  
  40. Parameters:
  41. subject (str): email title.
  42. from_email (str): address to use as the sender.
  43. to_email (str): address to send the email to.
  44. html_template (str): path to the HTML to use as the email body.
  45.  
  46. Attributes:
  47. mail (Mail): a SendGrid Mail object.
  48. '''
  49.  
  50. def __init__(self, subject, from_email, to_email, html_template):
  51. self.subject = subject
  52. self.to_email = Email(to_email)
  53. self.from_email = Email(from_email)
  54. self.html_template = html_template
  55. self.mail = self._create_mail()
  56.  
  57. def _create_mail(self):
  58. ''' Creates basic SendGrid Mail object.'''
  59. html = open(self.html_template, 'r').read()
  60. content = Content(type_='text/html', value=html)
  61. return Mail(self.from_email, self.subject, self.to_email, content)
  62.  
  63. def _encode_file(self, file):
  64. ''' Return file contents as base64 encoded.'''
  65. with open(file, 'rb') as f:
  66. data = f.read()
  67. f.close()
  68. encoded = base64.b64encode(data).decode()
  69. return encoded
  70.  
  71. def attach_item(self, file, type_, filename, disposition, cid):
  72. ''' Attach an item to the Mail object.'''
  73. attachment = Attachment()
  74. endoded_report = self._encode_file(file)
  75. attachment.content = endoded_report
  76. attachment.type = type_
  77. attachment.filename = filename
  78. attachment.disposition = disposition
  79. attachment.content_id = cid
  80. self.mail.add_attachment(attachment)
  81.  
  82. def apply_subs(self, subs):
  83. ''' Substitute template tags in the HTML with variables.'''
  84. for sub in subs:
  85. tag, value = sub
  86. self.mail.personalizations[0].add_substitution(
  87. Substitution(tag, value)
  88. )
  89.  
  90. def send(self):
  91. sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
  92. response = sg.client.mail.send.post(request_body=self.mail.get())
  93. status = response._status_code
  94. if not 200 <= status < 300:
  95. raise IOError('SendGrid response is {}'.format(status))
  96. return status
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement