Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Tim Morgan http://timmorgan.org

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Using POP3 to Retrieve Email for Rails App

Put this in a file called "inbox" in the scripts/ directory of your Rails app. change the host, username, and password to match.

Set up a cron job to run this script every so often (frequency depends on your needs).

   1  
   2  #!/usr/bin/env ruby
   3  
   4  require 'net/pop'
   5  require File.dirname(__FILE__) + '/../config/environment'
   6  
   7  logger = RAILS_DEFAULT_LOGGER
   8  
   9  logger.info "Running Mail Importer..." 
  10  Net::POP3.start("localhost", nil, "username", "password") do |pop|
  11    if pop.mails.empty?
  12      logger.info "NO MAIL" 
  13    else
  14      pop.mails.each do |email|
  15        begin
  16          logger.info "receiving mail..." 
  17          Notifier.receive(email.pop)
  18          email.delete
  19        rescue Exception => e
  20          logger.error "Error receiving email at " + Time.now.to_s + "::: " + e.message
  21        end
  22      end
  23    end
  24  end
  25  logger.info "Finished Mail Importer." 
  26  

Capture and email a traceback in Python

   1  
   2  import traceback, smtplib, StringIO
   3  fp = StringIO.StringIO()
   4  traceback.print_exc(file=fp)
   5  message = fp.getvalue()
   6  server = smtplib.SMTP(FAILURE_SERVER)
   7  server.sendmail(
   8    FAILURE_FROM,
   9    FAILURE_TO,
  10    FAILURE_MESSAGE + '\n\n' + message,
  11  )
  12  server.quit()

Send email with attachment(s) in Python

Can't remember if I wrote this or found it on the Web or a combination, so I won't take credit per se -- I'm just posting it as reference.

   1  
   2  import smtplib
   3  import os
   4  from email.MIMEMultipart import MIMEMultipart
   5  from email.MIMEBase import MIMEBase
   6  from email.MIMEText import MIMEText
   7  from email.Utils import COMMASPACE, formatdate
   8  from email import Encoders
   9  
  10  def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
  11    assert type(send_to)==list
  12    assert type(files)==list
  13  
  14    msg = MIMEMultipart()
  15    msg['From'] = send_from
  16    msg['To'] = COMMASPACE.join(send_to)
  17    msg['Date'] = formatdate(localtime=True)
  18    msg['Subject'] = subject
  19  
  20    msg.attach( MIMEText(text) )
  21  
  22    for f in files:
  23      part = MIMEBase('application', "octet-stream")
  24      part.set_payload( open(file,"rb").read() )
  25      Encoders.encode_base64(part)
  26      part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
  27      msg.attach(part)
  28  
  29    smtp = smtplib.SMTP(server)
  30    smtp.sendmail(send_from, send_to, msg.as_string())
  31    smtp.close()
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS