Ruby on Rails send mail using Gmail's SMTP

Source: Internet
Author: User
Tags ruby on rails



Refer to Http://guides.rubyonrails.org/action_mailer_basics.html


In the security of your Gmail account, start with two-step verification (Link: https://myaccount.google.com/security) to generate an application-specific password, which you need to use later.

Enter the root of the rails project under the console and execute rails generate mailer Usermailer

Locate the app/mailers/user_mailer.rb file, such as:

In the third step, you can see the addition of the Send_email method in Usermailer, then you need to create the corresponding Mailer view, under the App/views/user_mailer folder (because some mail clients do not support HTML, So create a text at the same time, two send together, and the method name is consistent, when called Send_email, the corresponding view will be used. Usermailer is a controller, Send_email is Action,send_email.html.erb and Send_email.text.erb is the corresponding view.

Send_email.html.erb Content

Send_email.text.erb's content is to remove all the HTML tags in the Send_email.html.erb, leaving only the text, such as:

Now that the Controller#action view is available, because it is just a simple test, the model does not add, just change the action to the corresponding model of the mail or other.

Next, configure the Gmail SMTP server in the Config/application.rb file, such as (because Gmail needs fq to test, so in real development, the configuration of the SMTP server for Gmail can be placed in the Production.rb file, In test.rb and development.rb files with other mailboxes instead, I use the QQ SMTP server, as long as the QQ in the SMTP service, and get QQ client's private password)


SMTP Service configuration for QQ

The final step is to send a message by calling UserMailer.send_email.deliver_now

UserMailer.send_email.deliver_now



Because of the use of the Gmail SMTP server, the test needs FQ, so the local can not be tested. However, I can successfully send the message on my deployment to Heroku.



Ruby on Rails send mail using Gmail's SMTP


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.