Search code examples
ruby-on-railsmailer

Mailer Generating Syntax Error: unexpected '\n', expecting :: or '[' or '.'


I am trying to create a mailer (ContactMailer) from my Contact model, which saves information from a basic form to the database. It is correctly saving to the database, but I'm getting an error for my mailer method.

It's calling syntax error, unexpected '\n', expecting :: or '[' or '.', highlighting the first end of my model code:

class Contact < ActiveRecord::Base
  after_create :send_contact_emails

  private

  def send_contact_emails
    for contacts do |contact|
      ContactMailer.new_contact(name, email, message).deliver_now
    end
  end
end

Here's my contact_mailer.rb code:

class ContactMailer < ApplicationMailer
  default from: "geneticgolf@gmail.com"

  def new_contact(name, email, comment)
    @contact.name = name
    @contact.email = email
    @contact.message = message

    mail(to: "EMAILADDRESS@gmail.com", subject: "New Contact Us Submission from #{name}")
  end
end

Any help getting this working would be appreciated, as my extensive googling has not done the trick.


Solution

  • Try this:

    class Contact < ActiveRecord::Base
     after_create :send_contact_emails
    
    private
    
     def send_contact_emails
        ContactMailer.new_contact(self).deliver_now 
     end
    end
    

    and:

     class ContactMailer < ApplicationMailer
      default from: "geneticgolf@gmail.com"
    
     def new_contact(contact)
      @contact = contact
      mail(to: "EMAILADDRESS@gmail.com", subject: "New Contact Us Submission from #{@contact.name}")
     end
    end