A simple Python library for sending email invitation with HTML or plain text.
- To install. Run:
pip install git+https://github.com/Phoenix-Chen/py-invitation.git
Edit your HTML template or plain text to include variables
in the format of {{VARIABLE_NAME}}
:
Dear {{receiver_name}}:
<a href="{{callback_url}}"> RSVP Now </a>
from py_invitation import Template
template = Template(EMAIL_SUBJECT, SENDER_EMAIL, RECEIVER_EMAIL)
# Create confirmation token (recommand to use a random token)
token = base64.b32encode(os.urandom(10)).decode('utf-8') # You probably want to save the token into your database for later verification
# Make variables
variables = {
'callback_url': "www.your_site.com/verify?code=" + token,
'receiver_name': RECEIVER_NAME
}
# Load into template
template.load_template_from_file("./templates/invitation2.html", variables)
# Add attachment
template.add_attachment("./invitation.png")
from py_invitation import Invitation
invitation = Invitation()
invitation.set_smtp(EMAIL_HOST, EMAIL_PORT, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
invitation.send(RECEIVER_EMAIL, template)
invitation.close()
Invitation(self)
Handler for invitation.
- smtp_conn (
object
) : A smtplib.SMTP object. - email_addr (
str
) : SMTP host email.
Invitation.set_smtp(self, host, port, host_user, host_password, use_tls=True)
Setup SMTP connection.
- host (
str
) : SMTP server host. - port (
int
) : SMTP port. - host_user (
str
) : SMTP host email. - host_password (
str
) : SMTP host password. - use_tls (
boolean
, optional) : Whether the SMTP uses TLS. Defaults to True.
Invitation.send(self, receiver_email, template)
Send the invitation.
- receiver_email (
str
) : The email address of receiver. - template (
object
) : A py_invitation.Template object.
Invitation.close(self)
Close the smtp connection.
Template(self, subject, sender, receiver)
Template contains all the email info.
- subject (
str
) : Subject of the email. - sender (
str
) : The email address of sender. - receiver (
str
) : The email address of receiver.
- msg (
object
) : A email.mime.multipart.MIMEMultipart object.
Template.load_template_from_str(self, template_str, variables, is_html=False)
Load email body from string.
- template_str (
str
) : HTML string or plain text as email body. - variables (
dict(str)
) : A dictionary containing all the variables to be replaced in template. - is_html (
boolean
, optional) : Whether the string is HTML. Defaults to False.
Template.load_template_from_file(self, template_path, variables, is_html=False)
Load email body from a file.
- template_path (
str
) : Path to the html or text message as email body. - variables (
dict(str)
) : A dictionary containing all the variables to be replaced in template. - is_html (
boolean
, optional) : Whether the file is HTML. Defaults to False.
Template.add_attachment(self, file_path)
Add an attachment to the email.
- file_path (
str
) : Path to the attachment.
Template.to_string(self)
Returns email info as a string.
str
: Info about email as a string.