Jaymon / sendgrid-python

SendGrid Python Library

Home Page:http://sendgrid.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SendGrid-Python

This library allows you to quickly and easily send emails through SendGrid using Python.

Build Status

Warning! This library was recently updated to bring it up to date with all of our other libraries. It behaves completely different from the previous release. Also, SMTP has been deprecated in support for the Web API.

Install

pip install sendgrid
# or
easy_install sendgrid

Example

import sendgrid

sg = sendgrid.SendGridClient('YOUR_SENDGRID_USERNAME', 'YOUR_SENDGRID_PASSWORD')

message = sendgrid.Mail()
message.add_to('John Doe <john@email.com>')
message.set_subject('Example')
message.set_html('Body')
message.set_text('Body')
message.set_from('Doe John <doe@email.com>')
status, msg = sg.send(message)

#or

message = sendgrid.Mail(to='john@email.com', subject='Example', html='Body', text='Body', from_email='doe@email.com')
status, msg = sg.send(message)

Adding Recipients

message = sendgrid.Mail()
message.add_to('example@sendgrid.com')
# or
message.add_to('Example Dude <example@email.com>')
# or
message.add_to(['Example Dude <example@email.com>', 'john@email.com'])

Adding BCC Recipients

message = sendgrid.Mail()
message.add_bcc('example@email.com')
# or
message.add_bcc(['Example Dude <example@email.com>', 'john@email.com'])

Setting the Subject

message = sendgrid.Mail()
message.set_subject('Example')

Set Text or HTML

message = sendgrid.Mail()
message.set_text('Body')
# or
message.set_html('<html><body>Stuff, you know?</body></html>')

Set From

message = sendgrid.Mail()
message.set_from('example@email.com')

Set ReplyTo

message = sendgrid.Mail()
message.set_replyto('example@email.com')

Set File Attachments

message = sendgrid.Mail()
message.add_attachment('./stuff.txt')
# or
message.add_attachment_stream('filename', 'somerandomcontentyouwant')
# strings, unicode, or BytesIO streams

SendGrid's X-SMTPAPI

If you wish to use the X-SMTPAPI on your own app, you can use the SMTPAPI Python library.

There are implementations for setter methods too.

message = sendgrid.Mail()
message.add_substitution("key", "value")
message = sendgrid.Mail()
message.add_section("section", "value")
message = sendgrid.Mail()
message.add_category("category")
message = sendgrid.Mail()
message.add_unique_arg("key", "value")
message = sendgrid.Mail()
message.add_filter("filter", "setting", "value")

SMTP

SMTP support has been deprecated from all of our libs. But for those whom still want to use it, here is an example:

import smtplib
from email.mime.text import MIMEText

email = MIMEText("this is a text/plain email") # you can make this html too.

email['Subject'] = 'This will be the subject'
email['From'] = 'yamil@sendgrid.com'
email['To'] = 'example@email.com'
email['Cc'] = 'yamil.asusta@sendgrid.com, jose@sendgrid.com' # this is comma separated field 

s = smtplib.SMTP('smtp.sendgrid.net', 587)
s.login('SENDGRID_USER', 'SENDGRID_PASSWORD')
s.sendmail(email['From'], [email['To']], email.as_string())

TODO:

  • Add support for CID

Tests

python test/__init__.py

MIT License

About

SendGrid Python Library

http://sendgrid.com


Languages

Language:Python 100.0%