If you are a web developer it is a need for you to know how to send email using your own codes. Which is luckily as for being a Ruby on Rails Programmer it is really easy.

The following are the steps on how to send email in ruby on rails.

Step 1.

require ‘rubygems’

require ‘openssl’

require ‘net/smtp’

You need to copy and paste this code on your environment.rb which you can find inside the config folder. This will let you use the openssl and the smtp.

Step 2.

 

@f_message = <<MESSAGE_END

To: #{recipient}

MIME-Version: 1.0

Content-type: text/html

Subject: #{subject}

#{message}<br>

MESSAGE_END

 

smtp = Net::SMTP.new(‘smtp.gmail.com’, 25)

smtp.enable_starttls

smtp.start(‘mail.google.com’,username, password, :login) do |smtp|

smtp.send_message @f_message, ‘[email protected]’,[recipient]

end

Copy and paste the code above and paste it anywhere in your controller

As you can see I used the google mail server as my mail server and the port must be 25.

For you to send email successfully you need first to enable starttls.

Username and password are required for you to send email.

MIME-Version: 1.0

Content-type: text/html

are optional this wll just let you use html tags. If you want to send pure text you may want to delete this lines.

You can now send email everytime you want. Feel free to ask me by writing your question in my comment box.

Share on Google+Tweet about this on TwitterShare on FacebookShare on TumblrShare on StumbleUponPin on PinterestShare on LinkedInDigg thisShare on RedditEmail this to someone