How do I test a custom email program?

I am trying to test the email tracking functionality across rails that I want to send after the user sends the email to the mailgun email address (which I tested and works). I'm not sure if my code is configured correctly for the mail sender, but no email is sent to mailgun when I send email.

Here is my custom mailer:

<!DOCTYPE html>
<html>
  <head>
    <meta content= 'text/html; charset=UTF-8' http-equiv='Content-Type' />
      </head>
      <body>
        <h1>Hi <%= @user.name %>,</h1>
        <p>
          This is just a friendly email to let you know the following bookmark was saved:
        </p>
        <p>
          <%= @bookmark.url %>
        </p>
      <p>
        Cheers! <br/>
        Bookmarks.com
      </p>
    </body>
    </html>

      

Here is my controller code:

class IncomingController < ApplicationController
skip_before_filter :verify_authenticity_token
  def create
    Rails.logger.info "INCOMING PARAMS HERE: #{params}"
    Rails.logger.info "Topics: #{params[:subject]}"
    Rails.logger.info "Url: #{params['stripped-text']}"
    Rails.logger.info "User email: #{params[:sender]}"
    user = User.find_by_email(params[:sender])
    Rails.logger.info "@@@@@@@@@@@@@@@@ User: #{user.inspect}"
    if user
      @bookmark = Bookmark.new(url: params['stripped-text'])
      topics = []
      topic_names = params[:subject].split(' ')
      topic_names.each do |topic_name|
        name = topic_name.sub(/#/, '')
        topics << Topic.find_or_create_by_name(name)
      end
      if @bookmark.save
        topics.each do |topic| 
          @bookmark.topics << topic
        end
        user.bookmarks << @bookmark
        FollowUpMailer.notify(@bookmark).deliver
      end
    end    
    head 200 
  end
end

      

And here is my url generating code:

def create
    @bookmark = Bookmark.new(bookmark_params)
    @bookmark.user = current_user
    if @bookmark.save
      topic_names = params[:topic_names]
      topic_names = topic_names.split(' ')
      topic_names.each do |topic_name|
        name = topic_name.sub(/#/, '')
        topics << Topic.find_or_create_by_name(name)
      end
      respond_to do |format|
        format.html { redirect_to @bookmark, notice: 'Bookmark was successfully created.' }
        format.json { render action: 'show', status: :created, location: @bookmark }
      end
    else
      respond_to do |format|
        format.html { render action: 'new' }
        format.json { render json: @bookmark.errors, status: :unprocessable_entity }
      end
    end   
  end

      

The only way I have tested this is to send an email via mailgun through production and see my local rails server logs (which show no sign of action) and so I cannot figure out how else to test the mailer in development since I am all still new to programming. Can I get some ideas on how to check to work with some errors? Thank you!

Edit question: I forgot to add my follow_up_mailer.rb code:

class FollowUpMailer < ActionMailer::Base
  default from: "valeriemettler@gmail.com"

  def notify(bookmark)
    @bookmark = bookmark

    mail(to: user.email, subject: 'Your bookmark was saved!')
  end
end

      

This is the error I get after testing with FollowUpMailer.notify (.first bookmark) .deliver:

2.0.0-p481 :001 > FollowUpMailer.notify(Bookmark.first)deliver
SyntaxError: (irb):1: syntax error, unexpected tIDENTIFIER, expecting end-of-input
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p481/bin/irb:12:in `<main>'

      

Any thoughts?

+3


source to share


1 answer


A quick note, before the rest of the answer. Your local server MUST NOT show anything when you send / receive messages in the Production section. You should look in your production server logs ( heroku logs

) to see what works / error. However, I don't think there is a better way to test this, so my full answer is below ...

Rails requires several parts. You have most of them, but you seem to be missing the Mailer class, i.e. FollowUpMailer

...

This file should be in app/mailers/follow_up_mailer.rb

, and it should define a method notify

, which is what is called when you run this line of code in your controller.

Check out the Rails Guide on this, what this method should look like, how you pass variables to the view, and what the view should be named and where it is.

More generally, I would suggest trying to move some of this logic into the bookmark model. This may sound impractical, but it will make your life a lot easier. If you reject the mail behavior from the controller, you can more easily test the mailing directly.

Perhaps create a method save_and_notify

in the Bookmark class and throw an error to the controller if it doesn't return true. This method will cover the last few lines of your action:



    topics.each do |topic| 
      @bookmark.topics << topic
    end
    user.bookmarks << @bookmark
    FollowUpMailer.notify(@bookmark).deliver

      

And this should also be taken topics

and user

as arguments.

There are several ways to move some of this logic into the model. This is only one. Once you've done that, you can easily test saving bookmarks and sending messages from the Rails console, or write tests for it.

Note that you can also check your mailing list right in the console right now. Just do the following: FollowUpMailer.notify(Bookmark.first).deliver

. Any errors that occur when submitting should be raised in the console. Remember to configure your message interceptor, although otherwise you'll be sending live messages whenever you experience this.


To fix one bug in your mailer, you need to define a variable @user

in your method notify

. This variable is used in the mailer's view, but is not defined in the associated method (basically a controller action for the mailer). Alternatively, you can extrapolate the user from the bookmark in the view @bookmark.user.name

. Anyway, try testing this on your local console and you will get immediate feedback on whether it works.

+1


source







All Articles