How to send emails in ror application


#1

can anyone tell me abt

how to include email option in ror application.

with code

plz


#2

Nagesh S. wrote:

can anyone tell me abt

how to include email option in ror application.

with code

plz

you generate a model that is derived from ActionMailer, and via that
class, you send mails. something like this

===================

ruby script generate mailer MAILERNAME
===================
in the models/MAILERNAME.rb file you’ll find:

class MAILERNAME < ActionMailer::BASE

#you need to define here a method to send emails:
def some_email
#here you put all of the headers of the email:
@recipients = “removed_email_address@domain.invalid”
@from = “from Nagesh”
@subject = “title of my email”

define type of email to send

@content-type ‘text/html’

here you can pass variables to the email-template

@body[“variable”] = 123
end

end

=================
now you need to write the email to send; this is in the views/MAILERNAME
folder - the name of the template is the name of the method you defined
in the model, so in our case it would be:

views/MAILERNAME/some_email.rhtml

here you can write whatever email you want

Hi, this is the email being sent… and what is here: “<%= @variable %>”
should equal to ‘123’!

==================

now that you’ve set up the files, you need to send the email out: here’s
how to do that, in the controller:



def some_method


#here:
MAILERNAME::deliver_some_email
#when you write down deliver_* you are basically sending the email out

  • ‘deliver’ performs the sending out of the email; walla, you’re done.
    ==============
    important notes however; you need to set up the config settings to some
    email server to send em out - you do this in the environment.rb file:

something like this:

ActionMailer::Base.server_settings = {
:address => “localhost”,
:domain => ‘www.yourdomain.com’,
:port => 25
}
(you need to configure this to your settings)

and try it on production - it may not work in development mode.

=========
that’s it, really. here’s some good documentation on the subject:

http://api.rubyonrails.org/files/vendor/rails/actionmailer/README.html

=======

–shai


#3

is necessary to install stmp server in my system …


#4

http://wiki.rubyonrails.org/rails/pages/HowToSendEmailsWithActionMailer


#5

On 23 Jul 2007, at 14:15, Nagesh S. wrote:

is necessary to install stmp server in my system …

Either install your own or use your ISPs or hosting company’s SMTP
server.

Best regards

Peter De Berdt


#6

HI i am presently new to this ror language can you please send me the
stepwise procedure for sending the email …


#7

On 23 Jul 2007, at 14:59, Nagesh S. wrote:

HI i am presently new to this ror language can you please send me the
stepwise procedure for sending the email …

http://wiki.rubyonrails.org/rails/pages/HowToSendEmailsWithActionMailer

Or even better: buy the Agile Web D. with RoR book at http://
pragmaticprogrammer.com/titles/rails/

Best regards

Peter De Berdt


#8

I had give like this:

ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.smtp_settings = {
:address => “smtp.postoffice.net”,
:port => 25,
:domain => ‘www.mywebsite.com’,
:user_name => “removed_email_address@domain.invalid”,
:password => ‘mypass’,
:authentication => :login
}

But it is not working can any modifications necessary…


#9

i had generated the controller as:

class EmailerController < ApplicationController
def sendmail
email = @params[“email”]
recipient = email[recipient]
subject = email[subject]
message = email[message]
Emailer.deliver_contact(recipient, subject, message)
return if request.xhr?
render :text => ‘Sucessfully sent a mail…’
end
def index
render :file => ‘app\views\emailer\index.rhtml’
end

end

and model as:

class Emailer < ActionMailer::Base
def contact(recipient, subject, message, sent_at = Time.now)
@subject = subject
@recipients = recipient
@from = ‘removed_email_address@domain.invalid’
@sent_on = sent_at
@body[“title”] = ‘This is title’
@body[“email”] = ‘removed_email_address@domain.invalid’
@body[“message”] = message
@headers = {}
end

end

in environment.rb i had given like this:

ActionMailer::Base.delivery_method =:sendmail
ActionMailer::Base.server_settings = {
:address => “localhost”,
:port => 25,
:domain => “gmail.com”,
:authentication => :login,
:user_name => “username”,
:password => "password
}

and in app\views\emailer\contact.rhtml:

Hi!

You are having one email message from <%= @email %> with a tilte

<%= @title %>
and following is the message:
<%= @message %>

Thanks

and in app\views\emailer\index.rhtml:

Send Email

<%= start_form_tag :action => 'sendmail' %>

Subject: <%= text_field 'email', 'subject' %>

Recipient: <%= text_field 'email', 'recipient' %>

Message
<%= text_area 'email', 'message' %>

<%= submit_tag "Send" %> <%= end_form_tag %>

but still i cannot able to send the mails… what are the changes
required please kindly send me code…


#10

On 23 Jul 2007, at 15:16, Nagesh S. wrote:

}
You either use sendmail or smtp:

ActionMailer::Base.delivery_method =:smtp

Best regards

Peter De Berdt


#11

In addition to the other pointers. Ryan Davies has just done a
screencast
on this topic

http://railscasts.com/episodes/61


#12

On 23 Jul 2007, at 15:35, Nagesh S. wrote:

i had kept this also but i cnot able to send email:

can you provide me a stepwise procedure for sending email:

ActionMailer::Base.delivery_method =:smtp

There’s no more to it, you’ll have to use your common sense and the
log files to find out what goes wrong.

Best regards

Peter De Berdt


#13

i had kept this also but i cnot able to send email:

can you provide me a stepwise procedure for sending email:

ActionMailer::Base.delivery_method =:smtp