I've got the following code sending email by django, but all emails are being filters by the receivers SPAM filters. It's my understanding that you can configure django to use an external SMTP server, and I've got an account setup on Google Apps that I hope to use for that purpose.
Is anyone able to help on how to implement this?
# conding=utf8
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
from subprocess import Popen, PIPE
import os
from pictures.models import S5Picture
from django.core.management.base import NoArgsCommand
class Command(NoArgsCommand):
def send_mail(self, send_from, send_to, subject, text, files=[]):
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for f in files:
part = MIMEBase('image', "jpeg")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="samsung_gs5_photo.jpg"')
msg.attach(part)
part = MIMEBase('application', 'pdf')
part.set_payload(open('/usr/share/nginx/www/upload/static/BELL_OFFER_WITH_GS5_V3.pdf', 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="MMVA_Bell_Offer.pdf"')
msg.attach(part)
p = Popen(['/usr/sbin/sendmail', '-t'], stdin=PIPE)
p.communicate(msg.as_string())
p.stdin.close()
def handle(self, *args, **kwargs):
for photo in S5Picture.objects.filter(sent=False):
try:
print(photo.email, photo.picture)
self.send_mail('photo@vps73224.ovh.net', photo.email, 'Your Samsung GS5 Photo!', '', ['/usr/share/nginx/www/upload/static/' + photo.picture])
photo.sent = True
photo.save()
except Exception as e:
print(e)
pass