Forum: Ruby on Rails how to send emails in ror application

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ba15b74b5b6e757a0a69a4ade447c564?d=identicon&s=25 Nagesh Sangana (nagesh)
on 2007-07-23 06:59
can anyone tell me abt

how to include email option in ror application.

with code


plz
4c3acdc3d93f54cc7a7281780ec8a4ee?d=identicon&s=25 Shai Rosenfeld (shai)
on 2007-07-23 09:35
Nagesh Sangana 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 = "me@some.com"
@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/acti...

=======

--shai
8469aae6bf8fd1db5c047e9de1c4d82c?d=identicon&s=25 João Paulo Lins (Guest)
on 2007-07-23 14:10
(Received via mailing list)
Ba15b74b5b6e757a0a69a4ade447c564?d=identicon&s=25 Nagesh Sangana (nagesh)
on 2007-07-23 14:15
is necessary to install stmp server in my system ....
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2007-07-23 14:52
(Received via mailing list)
On 23 Jul 2007, at 14:15, Nagesh Sangana 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
Ba15b74b5b6e757a0a69a4ade447c564?d=identicon&s=25 Nagesh Sangana (nagesh)
on 2007-07-23 14:59
HI i am presently new to this ror language can you please send me the
stepwise procedure for sending the email ......
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2007-07-23 15:02
(Received via mailing list)
On 23 Jul 2007, at 14:59, Nagesh Sangana 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/HowToSendE...

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


Best regards

Peter De Berdt
Ba15b74b5b6e757a0a69a4ade447c564?d=identicon&s=25 Nagesh Sangana (nagesh)
on 2007-07-23 15:10

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  => "me@postoffice.net",
  :password  => ‘mypass’,
  :authentication  => :login
    }

But it is not working can any modifications necessary....
Ba15b74b5b6e757a0a69a4ade447c564?d=identicon&s=25 Nagesh Sangana (nagesh)
on 2007-07-23 15:16
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 = 'no-reply@yourdomain.com'
      @sent_on = sent_at
    @body["title"] = 'This is title'
      @body["email"] = 'sender@yourdomain.com'
       @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:
-------------------------------------
<h1>Send Email</h1>
<%= start_form_tag :action => 'sendmail' %>
<p><label for="email_subject">Subject</label>:
<%= text_field 'email', 'subject' %></p>
<p><label for="email_recipient">Recipient</label>:
<%= text_field 'email', 'recipient' %></p>
<p><label for="email_message">Message</label><br/>
<%= text_area 'email', 'message' %></p>
<%= 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..............
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2007-07-23 15:27
(Received via mailing list)
On 23 Jul 2007, at 15:16, Nagesh Sangana wrote:

> }
You either use sendmail or smtp:

ActionMailer::Base.delivery_method =:smtp

Best regards

Peter De Berdt
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-07-23 15:30
(Received via mailing list)
In addition to the other pointers.  Ryan Davies has just done a
screencast
on this topic

http://railscasts.com/episodes/61
Ba15b74b5b6e757a0a69a4ade447c564?d=identicon&s=25 Nagesh Sangana (nagesh)
on 2007-07-23 15:35
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
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2007-07-23 15:42
(Received via mailing list)
On 23 Jul 2007, at 15:35, Nagesh Sangana 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
This topic is locked and can not be replied to.