<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Baking Noodles &#187; email</title>
	<atom:link href="http://bakingnoodles.com/tag/email/feed/" rel="self" type="application/rss+xml" />
	<link>http://bakingnoodles.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 26 May 2010 14:18:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Command Line Email with Django and Gmail</title>
		<link>http://bakingnoodles.com/2009/03/command-line-email-with-django-and-gmail/</link>
		<comments>http://bakingnoodles.com/2009/03/command-line-email-with-django-and-gmail/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 12:55:48 +0000</pubDate>
		<dc:creator>Simon Oxley</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://bakingnoodles.com/?p=35</guid>
		<description><![CDATA[I needed to find a way to send email reports from a scheduled cron job. I looked for a simple linux command-line email solution but couldn&#8217;t find anything that fitted the bill. I was already using Django&#8217;s EmailMessage class in an existing app, so I started to think about how I could re-use it for [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to find a way to send email reports from a scheduled cron job. I looked for a simple linux command-line email solution but couldn&#8217;t find anything that fitted the bill. I was already using <a href="http://docs.djangoproject.com/en/dev/topics/email/#the-emailmessage-and-smtpconnection-classes">Django&#8217;s EmailMessage</a> class in an existing app, so I started to think about how I could re-use it for scheduled jobs.</p>
<p>I&#8217;m using linux in this scenario, but it would probably work fine in Windows too.</p>
<p>First I configured my cron job (actually the .sh script that cron job runs) to output to a file called <code>report.txt</code>. This would be overwritten each time the job ran, so would always be the latest version.</p>
<p>An existing Django project <code>settings.py</code> had the following settings to enable email sending via a Gmail (or Google Apps) account. The settings are based on Nathan Ostgard&#8217;s <a href="http://nathanostgard.com/archives/2007/7/2/gmail_and_django/">Gmail and Django</a> article.<a href="http://nathanostgard.com/archives/2007/7/2/gmail_and_django/"><br />
</a></p>
<pre class="brush: python">

EMAIL_HOST = &#039;smtp.gmail.com&#039;
EMAIL_HOST_USER = &#039;user@domain.com&#039;
EMAIL_HOST_PASSWORD = &#039;password&#039;
EMAIL_PORT = 587
EMAIL_USE_TLS = True
</pre>
<p>So I knocked up a standalone python script <code>sendreport.py</code> that would take these settings and email the <code>report.txt</code> file to me.</p>
<pre class="brush: python">
#!/user/bin/python
#
# Get settings from an existing django project
import sys
import os
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), &#039;/..&#039;))
os.environ[&#039;DJANGO_SETTINGS_MODULE&#039;] = &#039;my_django_project.settings&#039;

from django.core.mail import EmailMessage

# Set up email fields
recipient = &#039;myemail@mydomain.com&#039;
subject = &#039;CRON Job Report&#039;
body = &#039;CRON Job Report for blah, blah, blah . . .&#039;

# Create email object, attach file, send
email = EmailMessage(subject, body, to = [recipient])
email.attach_file(&#039;report.txt&#039;)
email.send()
</pre>
<p>The <code>EmailMessage </code>class makes it very easy to attach a file and send the email.  I can tag this on to the end of the .sh script executed by the cron job and jobs-a-good-un <img src='http://bakingnoodles.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Well sort of . .  it&#8217;s functional, but not ideal. It works by reading email settings from an existing Django app&#8217;s <code>setting.py</code>. I wanted it to be more portable, and not dependent on a specific Django configuration/app.</p>
<p>Then I found out that you can specify Django settings from within a standalone python script. Could this method be used to break any dependencies and make the script more portable? Absolutely!</p>
<pre class="brush: python">
#!/user/bin/python
#
# Configure some standalone django settings
# These settings are for a gmail or google apps account
from django.conf import settings
settings.configure (
EMAIL_HOST = &#039;smtp.gmail.com&#039;
EMAIL_HOST_USER = &#039;user@domain.com&#039;
EMAIL_HOST_PASSWORD = &#039;password&#039;
EMAIL_PORT = 587
EMAIL_USE_TLS = True
)

from django.core.mail import EmailMessage
import datetime
import sys
import socket

# Get the hostname and current time to put in the email
hostname = socket.gethostname()
timestamp = datetime.datetime.now().ctime()

# Set up email fields
recipient = &#039;myemail@mydomain.com&#039;
subject = &#039;%s CRON Job Report: %s&#039; % (hostname, timestamp)
body = &#039;Report for %s on %s attached&#039; % (hostname, timestamp)

# Create email object, attach file, send
email = EmailMessage(subject, body, to = [recipient])
email.attach_file(&#039;bkp_report.txt&#039;)
email.send()
</pre>
<p>I also made use of <code>hostname()</code>and <code>datetime()</code> to provide some more detail for the email subject and body.</p>
<p>Now the only dependency is Django itself &#8211; which I&#8217;m happy with, my job reports are archived in email &#8211; which I like, and I get to use more Python &#8211; which is good for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://bakingnoodles.com/2009/03/command-line-email-with-django-and-gmail/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

