2

I realized that when I send mails(letters) on localhost site via local postfix server, they were sent successfully, but each time I need to include the letter link to my logo stored in:

public/castle.jpg

app/mailers/review_mailer.rb:

class ReviewMailer < ApplicationMailer
  default from: 'no-reply@kalinin.ru'

  def welcome_email(review)
    @review = review
    mail(to: 'bla@bla.ru', subject: 'New review create')
  end  
end

views/review_mailer/welcome_email.html.erb:

<!DOCTYPE html>
<html>
  ...........................
    <p>logo: <%= image_tag 'public/castle.jpg' %></p>
  </body>
</html>

application.rb:

config.action_mailer.default_url_options = { host: 'localhost' }

development.rb:

  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.delivery_method = :sendmail
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

reviews_controller:

  def create
    @review = current_user.reviews.build(review_params)
    if @review.save
      ReviewMailer.welcome_email(@review).deliver_now
      redirect_to root_path
  end

after creating a new review, here is the console output:

ReviewMailer#welcome_email: processed outbound mail in 205.9ms

Sent mail to bla@bla.ru (73.5ms)
Date: Tue, 22 Sep 2015 12:47:11 +0300
From: no-reply@kalinin.ru
To: prozaik81-2@yandex.ru
Message-ID: <5601239fdd0d3_18b43f823a7394382836b@kalinin.mail>
Subject: New review create
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5601239fdbc22_18b43f823a73943828272";
 charset=UTF-8
Content-Transfer-Encoding: 7bit

----==_mimepart_5601239fdbc22_18b43f823a73943828272
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<html>
  <body>
    <!DOCTYPE html>
<html>
....................
    <p>logo: <img src="/images/public/castle.jpg" alt="Castle" /></p>
  </body>
</html>
  </body>
</html>

----==_mimepart_5601239fdbc22_18b43f823a73943828272--

Redirected to http://localhost:3000/
Completed 302 Found in 601ms (ActiveRecord: 301.4ms)

Mail(letter) is not being sent, what can possibly be wrong?

soltex
  • 2,993
  • 1
  • 18
  • 29
stackow1
  • 301
  • 1
  • 3
  • 10

2 Answers2

1

I think you should write this in your environment file:

config.action_mailer.asset_host = "http://localhost:3000"

or

config.asset_host = 'http://localhost:3000'

This will implicity set next configs:

config.action_controller.asset_host = 'http://localhost:3000'
config.action_mailer.asset_host = 'http://localhost:3000'
Alexander Shlenchack
  • 3,779
  • 6
  • 32
  • 46
1

You don't need to refer to public in your paths. You should be able to reference castle.jpg directly...

<%= image_tag 'castle.jpg' %>

You have to remember that a Rails app runs from the public directory. Indeed, if you set up an nginx server, it actually recommends pointing to public:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        server_name mydomain.com;
        passenger_enabled on;
        rails_env    production;
        root         /home/deploy/myapp/current/public;

Thus, if you're referencing public, you get a sort-of "inception" type effect where the app is trying to reference itself.

--

In regards the asset host for the mailout, the other answers will be able to help. I've not got the most experience in this area.

Richard Peck
  • 76,116
  • 9
  • 93
  • 147