First, install postfix (an SMTP server) on your VPS (in my case, a Linode node or Rackspace Cloud server).
sudo apt-get install postfix
Enter "
yourdomain.com" when it asks you for it. There, now you have your own SMTP server.
Add something like this to your settings.py so that outgoing mail comes from postfix.
DEFAULT_FROM_EMAIL = 'Your Site <
yourdomain-noreply@yourdomain.com>'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'localhost'
EMAIL_HOST_USER = '
yourdomain-noreply@yourdomain.com'
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = 25
EMAIL_SUBJECT_PREFIX = '[Your Site] '
If your site uses Django but not Pinax, you're done. To test it, restart Apache or touch your wsgi file, then enter the following 2 lines in a "python manage.py shell" (I hope you're in your virtualenv) at the prompt:
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', '
from@example.com', ['
to@example.com'], fail_silently=False)
...and if you got an email in your "
to@example.com" account, you're all set.
Additional Pinax steps
If your site uses Pinax, that last step probably didn't send you an email.
Pinax has an app in it called django-mailer that replaces Django's send_mail with its own queuing version. Mail gets queued up (you can see it in your admin section's Home > Mailer > Messages) until you or a cron job run the command "python manage.py send_mail".
Try running "python manage.py send_mail" (still in your virtualenv). You should get an email.
Make sure you have all the Pinax apps' email settings in your settings.py. These depend on your particular desired configuration, but the ones that should be True for sure are ACCOUNT_REQUIRED_EMAIL and ACCOUNT_EMAIL_VERIFICATION.
ACCOUNT_OPEN_SIGNUP = True
ACCOUNT_REQUIRED_EMAIL = True
ACCOUNT_EMAIL_VERIFICATION = True
ACCOUNT_EMAIL_AUTHENTICATION = False
ACCOUNT_UNIQUE_EMAIL = EMAIL_CONFIRMATION_UNIQUE_EMAIL = False
EMAIL_CONFIRMATION_DAYS = 2
EMAIL_DEBUG = DEBUG
Now, the last thing you need is a cronjob to send the queued mail every minute, and to retry the deferred mail every 20 min. How do you create this cronjob?
"crontab -e" opens up an editor. Enter something like the following 2 lines. "env" is your virtualenv, "username" is the username you use for sshing into the VPS, and "myproject" is your Pinax project directory. I have a blank line at the end of mine, which you might need.
* * * * * (cd /home/username/myproject; ../env/bin/python manage.py send_mail >> ../cron_mail.log 2>&1)
0,20,40 * * * * (cd /home/username/myproject; ../env/bin/python manage.py retry_deferred >> ../cron_mail_deferred.log 2>&1)
If you haven't picked a default editor yet, it'll give you a choice. I like nano for this kind of thing because it behaves like a normal text editor. Save the file and exit. The cronjob should be automatically installed.
Now restart Apache and try signing up for an account on your Pinax site. You should receive an account verification email within a minute or two. Check your Spam and All Mail folders. You're done.
If you didn't get one, it's probably an issue with the paths specified in your cronjob or access permissions. Make sure your virtualenv's set up properly and that the path to its Python is correct. And make sure ../cron_mail.log and ../cron_mail_deferred.log are writeable. Try "touch cron_mail.log" from the appropriate directory.
Other notes
You can set up your project to use Gmail's SMTP server, but Postfix is easier. Gmail uses a different port for SMTP than the usual port 25. I think it's port 587. But this only works for addresses like
yourname@gmail.com. I believe. I don't think you can do this with Google Apps for Domains.
If your Pinax site is live, don't do "python manage.py send_mail" until you've made sure the message queue is clear except for your own email address. You could accidentally send out emails to all your users, like I did a couple of hours ago. Those delayed confirmation emails will all have broken links and confuse your users.
Comments [3]