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

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

mail sending program using ssl using gmail account

// sending mail using ruby through ssl

require 'rubygems'
require 'action_mailer'
require "net/smtp"
require "tlsmail"

class MailSent < ActionMailer::Base
  def message(r = "", m = "", s = "", f = "", c = nil)
    begin
      fail StandardError, "No Recipient" if r.empty? #and b.empty?
      fail StandardError, "No Message" if m.empty? 
      fail StandardError, "No Subject" if s.empty?
      fail StandardError, "No From" if s.empty?
      from f
      recipients r
      cc c if c and !c.empty?
      subject s
      body m
    rescue Exception => e
      puts e
    end
  end
end

class SentMail
  def get_value(to,msg,sub,from,pass=nil,cc=nil)
    begin

      Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
      ActionMailer::Base.smtp_settings = {
        :address => "smtp.gmail.com",
        :port => "587",
        :domain => "gmail.com",
        :user_name => "example.com,
        :password => "youraccountpassword",
        :authentication => :plain
      }
      MailSent.deliver_message(to,msg,sub,from,cc)
    rescue Exception=>e
      puts e
    end
  end
end

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).

#!/usr/bin/env ruby

require 'net/pop'
require File.dirname(__FILE__) + '/../config/environment'

logger = RAILS_DEFAULT_LOGGER

logger.info "Running Mail Importer..." 
Net::POP3.start("localhost", nil, "username", "password") do |pop|
  if pop.mails.empty?
    logger.info "NO MAIL" 
  else
    pop.mails.each do |email|
      begin
        logger.info "receiving mail..." 
        Notifier.receive(email.pop)
        email.delete
      rescue Exception => e
        logger.error "Error receiving email at " + Time.now.to_s + "::: " + e.message
      end
    end
  end
end
logger.info "Finished Mail Importer." 

Simplifying ActionMailer development in Ruby on Rails

Sick of writing almost the same thing over and over in your ActionMailer classes? Skip all that, and use something like this.

class Mailer < ActionMailer::Base

  helper ActionView::Helpers::UrlHelper

  def generic_mailer(options)
    @recipients = options[:recipients] || "me@privacy.net"
    @from = options[:from] || "me@privacy.net"
    @cc = options[:cc] || ""
    @bcc = options[:bcc] || ""
    @subject = options[:subject] || ""
    @body = options[:body] || {}
    @headers = options[:headers] || {}
    @charset = options[:charset] || "utf-8"
  end
  
  # Create placeholders for whichever e-mails you need to deal with.
  # Override mail elements where necessary
  
  def contact_us(options)
    self.generic_mailer(options)
  end

  ...

end


(If you have a configuration loaded into a constant, you could just replace the defaults above and use your app's defaults to make it all cleaner, of course)

And then from your controller you can do stuff like this:

Mailer.deliver_contact_us(
   :recipients => "x@x.com",
   :body => { 
               :name => params[:name],
               :phone => params[:phone],
               :email => params[:email],
               :message => params[:message]
             },
   :from => "y@y.com"
)
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS