I have built a custom invite app for my site. To activate an invite you must follow a link sent to your email.
The issue then becomes, my email sending function is having trouble sending a string as a message that looks like this:
custom_message = "http://www.something.com%s" % invite.get_absolute_url()
After numerous tests, it seems the issue has to do with the :
, since everything seems to work fine without it.
I don't need the colon, as I could just leave the entirety of http://
out. But I am curious why the function won't work when passing this string to my send_custom_email()
function
For reference, this is my my email sending function:
def send_custom_email(recipient, custom_message):
to = recipient
gmail_user = 'someone@gmail.com'
gmail_pwd = GMAIL_PWD
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:Invite Link \n'
print header
unicoded_custom_message = unicode(custom_message)
msg = header + unicoded_custom_message
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()
A test:
>>> custom_message ="http://www.somesite.com%s"
>>> send_custom_email(recipient='someotherperson@mailinator.com', custom_message=custom_message)
To:someone@mailinator.com
From: someotherperson@gmail.com
Subject:Invite Link
done!
Although the email is sent, the message doesn't render
The email generated violates the format for emails:
There has to be a space after the key of a header and there have to be two newlines so separate the message:
header = 'To: ' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject: Invite Link \n\n'
As you are constructing it, the link is interpreted as email header.
Also you should consider using Django's built-in email function. Your code is vulnerable tp header injections. Please read: https://docs.djangoproject.com/en/dev/topics/email/ !