You need to send a letter to the owners of the site with the client's mail, when on the page with a subscription to news the client fills in the email field and presses the "subscribe to news" button.

In the mailers / subscribes_mailer.rb controller

class SubscribesMailer < ActionMailer::Base default from: "boss@yandex.ru", template_path: 'mailers/subscribes' def subscribe mail to: 'boss@yandex.ru', subject: "new subscribe" end end 

In the view where the submission form is located:

  <section class="one-half"> <h2>Подписаться на новости</h2> <div id="newsletter-wrap"> <p>После подписки на новостную рассылку на ваш e-mail будут приходить все актуальные новости и акции компании. Вы в любой момент сможете отписаться от рассылки.</p> <div class="notification-box notification-box-success" style="display: none;"> <p>Вы успешно подписаны на новостную рассылку. Проверьте почтовый ящик для подтверждения.</p> <a href="#" class="notification-close notification-close-success">x</a> </div> <div class="notification-box notification-box-error" style="display: none;"> <p>Ваш почтовый ящик не может быть добавлен в список рассылки. Пожалуйста повторите запрос позже.</p> <a href="#" class="notification-close notification-close-error">x</a> </div> <form id="newsletter-form" class="content-form clearfix" action="#" method="post"> <%= button_to "Добавить", SubscribesMailer.subscribe, :class => 'button' %> <input id="newsletter" type="email" name="newsletter" placeholder="Введите свой e-mail адрес &hellip;" class="required"> </form> <p class="tip"><span class="note">&#42;</span>Если письмо не приходит, проверьте папку спам в Вашем почтовом ящике.</p> </div> </section> 

When I refresh the page, I get the error:

 undefined method `SubscribesMailer' for SubscribesMailer:Class 

Please tell me how to properly send a letter from the site.

    2 answers 2

    You are on the absolutely wrong path: mailers / subscribes_mailer.rb is not a controller, it is (all of a sudden!) Mailer. You do not have a clear understanding of the sequence of actions, so the code in your question will not help. How it works:

    The easiest option is that we have one newsletter subscription, so in the users table (or who your recipient is) add the send_news: boolean column. Now we have the concept of subscribers (users whose send_news is true). Checkbox send_news is displayed on the form - a separate or simply in the user profile. Of course, the user has email.

    We create a table with news, we will write news there. The table is better to be called articles, and the model is Article, since news is only in the plural, and new also a service word. The news will have text and title.

    Create a mailer, which will send news.

     class ArticlesMailer < ActionMailer::Base default from: 'newsmaker@dobermann-site.com' def article_mail(subscriber_id, subject, article) @subscriber = User.find subscriber_id @article = article mail(to: @subscriber.email, subject: "We have news! #{subject}") end end 

    Next, create a markup for the letters:

     #app/views/articles_mailerarticles_mail.html.haml !!! 5 %html %body %h1= @article.title %p= @article.text 

    Well, now we need a background task that sends it all after creating a new article. Look in the direction of delayed_job. In short - select users who want to receive news, then sort through them and create a new mail for each, send a delayed-job in the background.

    • I will add only that from version 4.2 the rail is ActiveJob for background tasks. - D-side
    • I do not have users on the site. Simply send a letter to the site owners that such and such an email address has subscribed to the newsletter. The newsletter will be conducted not from the site but from the mail of the owners (commercial proposals are all I understand). - D7na
    • In any case, you need to store these addresses somewhere. In this case, simply create the Subscriber table, store the addresses for distribution in it. When a news item arrives, go through this table and create letters. - Pavel Volzhin

    We create a model controller and a Subscriber view with a single column in the table - email, which will store all email addresses of users who subscribe to the list with the command:

     rails g scaffold Subscriber email:string 

    In the Subscriber model, we add validation when writing values ​​to the database that checks the presence and uniqueness of the email that will be placed in the database:

     validates :email, presence: true, uniqueness: true 

    Create a mailer with the command:

     rails g mailer subscribe_mailer 

    In subscribe_mailer.rb change the address of the sender to the one that is needed:

     default from: "admin@site.com" 

    Add the subscribe_email method, which takes a subscriber as a parameter, which was created earlier and sends an email to the user using the mail method to a subscriber.email address.

     def subscribe_email(subscriber) @subscriber = subscriber mail(to: @subscriber.email, subject: "New subscriber", cc: "admin@site.com" end 

    The "bc" parameter allows you to send a hidden copy of the letter to the owners of the resource.

    In views / subscribe_mailer / we create two files: subscribe_email.html.erb and subscribe_email.text.erb with the contents of the letters sent to users.

    In the create method of the subscribers_controller controller, add the line:

     SubscribeMailer.subscribe_email(@subscriber).deliver 

    which sends the letter if the record is added to the database.

    Add the input form to the view:

     <%= form_for :subscriber, url: subscribers_url do |form| %> <%= form.submit %> <%= form.email_field :email %> <% end %>