By using ActionMailer you can send email using ruby. ActionMailer is a component that send and
receive emails.
Action Mailer - Configuration
1)
if you are using linux os you can use sendmail option to send and receive emails by editing your rails project environement.rb file.
ActionMailer::Base.delivery_method = :sendmail
or else you can use smtp settings
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
:address => "smtp.somesite.com",
:port => 25,
:domain => "somesite.com",
:authentication => :login,
:user_name => "username",
:password => "password",
}
# following line for to use a view for our email format.
ActionMailer::Base.default_content_type = "text/html"
2)
Generate email model by typing :
script/generate mailer Emailer
3) Edit the model
class Emailer < ActionMailer::Base
def contact(recipient, subject, message, sent_at = Time.now)
@subject = subject
@recipients = recipient
@from = 'no-reply@somedomail.com'
@sent_on = sent_at
@body["title"] = 'This is title'
@body["email"] = 'sender@somedomain.com'
@body["message"] = message
@headers = {}
end
end
4)
create a file called app/views/contact.rhtml
add the following. lines
Hi!
You are having one email message from <%= @email %> with a tilte
<%= @title %>
and following is the message:
<%= @message %>
Thanks
5)
now create a controller as follows
script/generate controller Emailer
then edit the EmailController as :
class EmailerController < ApplicationController
def sendmail
recipient = "gayanvirajith@gmail.com"
subject = "new subject"
message = "Hi Email"
Emailer.deliver_contact(recipient, subject, message)
return if request.xhr?
render :text => 'Message sent successfully'
end
end
6) That's it. done. now you can run script/server and go to url :
http://localhost:3000/email/sendmail/
Good luck ~!
Nice article, when you are adding a code phrases in between your posts better to differentiate the code block as you can see in other web site
ReplyDelete