ruby on rails ActionMailer error - ruby-on-rails

I'm trying to use ActionMailer for the first time.
My mailer looks like this:
class RequestMailer < ActionMailer::Base
default from: "fred#ameipro.com"
def request_email(worequest)
#worequest = worequest
mail(:to => "dave#ameipro.com", :subject => "New Service Request")
end
end
But, I get the following error:
Net::SMTPFatalError in WorequestsController#create
550 Cannot receive from specified address <fred#ameipro.com>: Unauthenticated senders not allowed
How do I correct?
Thanks!

This is an error from the mail server - it is refusing to deliver email from your address. Thats the part you need to diagnose. To help you, we'd need more information about the server you're trying to send through. To troubleshoot, I would try configuring a different client to send through your server, then try to send an email from fred#ameipro.com to dave#ameipro.com and see what happens.

Related

How to debug Mailboxer gem?

I believe I setup mailboxer gem correctly. Messages work. In my User.rb model I have:
def mailboxer_email(object)
email
end
However no email gets sent to the receiving user when said user receives a message (I believe by default mailboxer should send this email).
How should I begin debugging this?
Currently, in my messages_controller.rb I have tried manually sending the email:
Mailboxer::MessageMailer.new_message_email("You have received a new message.", recipients)
But I'm not sure if it's working...

Why won't my Twilio SMS send in Sidekiq background worker?

I am having issues sending a text with Twilio through my SideKiq background worker. The worker is supposed to send a text and then send an email (with Mandrill).
The email works fine.
The text never happens.
I haven't had issues with other jobs (including ones that also involve use
of environment variables).
I haven't had issues sending Twilio texts outside of a background worker.
I'm using Foreman to start my application.
In console, running UserNotifier.new.perform(1) works fine - both the email and the text are sent.
Here is some of the code in question:
This is the class that I'm using to send my SMS:
(My Gemfile includes the twilio-ruby gem)
class SendSMS
def initialize
#twilio_client = Twilio::REST::Client.new "#{ENV['TWILIO_SID']}", "#{ENV['TWILIO_TOKEN']}"
end
def send(message, user)
#twilio_client.account.sms.messages.create(
:from => "+1#{ENV['TWILIO_PHONE_NUMBER']}",
:to => user.phone_number,
:body => message
)
end
end
My worker looks like this:
class UserNotifier
include Sidekiq::Worker
sidekiq_options queue: :immediate
def perform(user_id)
user = User.find(user_id)
message = "Hi #{user.name}!"
SendSMS.new.send(message, user)
UserMailer.send(message, user).deliver
end
end
Can anyone see an issue with my code? Please let me know if there is anything else I should post or if there is any clarification that I could make.
My server was displaying the worker logs and it didn't seem to be running into an error.
Any suggestions on how to debug background workers would be appreciated as well.
Try catching REST errors from Twilio and logging some debug info. You can discover errors that might prevent messages from sending, or investigate sent messages in your Twilio account logs via their sid (Twilio's internal id). Also updated your create call to use the messages resource instead of deprecated sms/messages.
begin
message = #twilio_client.account.messages.create(
:from => "+1#{ENV['TWILIO_PHONE_NUMBER']}",
:to => user.phone_number,
:body => message
)
logger.debug "sent #{message.sid}"
rescue Twilio::REST::RequestError => e
logger.debug "error: #{e.message}"
end

How can I make my mailer deliver a message to an address containing umlauts under Rails 3?

This is my mailer:
class MyUserMailer < ActionMailer::Base
default from: "me#mail.com",
:content_transfer_encoding => "7bit"
def build_email(user)
mail(:to => user.email,:subject => "Welcome")
end
end
I'm testing with an address containing umlauts: äardvark#mail.com, but I keep receiving the following error, when I try to deliver the email:
'to' parameter is not a valid address. please check documentation
Documentation says that the mailer will use UTF-8 to encode all the fields. How can I make this work?
EDIT: I'm using Rails 3.2.5
Problem was caused by my usage of the mailgun Rails gem. After I switched to using smtp directly, problem went away.

Rails 3: Contact form to send messages from User Profiles

I worked through some basic tutorials on Rails 3. The goal is a community-website on abilities and activities. I am using Devise for authentication. The creation of user profiles with avatars worked well (thanks to paperclip).
As a next step, I want to enable registered users to send an e-mail to a user from his (or her) profile page. I found a great tutorial on creating a contact form using Google Apps:
http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/
The mailer class in this tutorial looks like:
class NotificationsMailer < ActionMailer::Base
default :from => "noreply#youdomain.dev"
default :to => "you#youremail.dev"
def new_message(message)
#message = message
mail(:subject => "[YourWebsite.tld] #{message.subject}")
end
end
My question: What is the best way to replace you#youremail.dev with the receivers E-Mail-Address? (from the User-Model)
Thanks in advance!
You can modify the new_message to accept the user (or list of users) to whom you want to send the email. Or an array of email addresses if you want to. Then pass the receiver's email address to the mail method as the :to option.
def new_message(receiver, message)
#message = message
mail(:subject => "[YourWebsite.tld] #{message.subject}",
:to => receiver.email_address) # or something similar
end
Then you can invoke your mailer like this
NotificationEmail.new_message(a_user, a_message).deliver
To read the API see here or here (I prefer APIdock).
Also a more comprehensive guide on ActionMailer is available here. If you are new to Rails, you can find more guides here.

Sending emails with attachments using Ruby on Rails via SendGrid

I am trying to send email with attachments using Ruby on Rails.
I followed the instructions from the ActionMailer site.
def welcome(recipient)
#account = recipient
attachments['file.csv'] = File.read('/path/to/users.csv')
mail(:to => recipient,
:bcc => ["email#example.com", "email2#example.com"],
:subject => "Sending attachment")
end
I am able to receive emails but without the attachment, I am trying to attach csv file but I am getting a file called "noname" as attachment
I just had this problem. I was not providing a body for the email, and was sending the attachment only. Unfortunately, it appears that SendGrid gets confused by this, sending an empty email (which is expected) with an empty attachment (which is neither expected nor desired).
Therefore, the solution: provide a body for the email. In your specific case, an /app/views/application_mailer/welcome.text.erb template with a simple text, saying "See attached" or whatever appropriate.
SendGrid is an SMTP service, and thus should function just as any other outbound SMTP service. Are you sure your syntax and filepaths are correct?
class ApplicationMailer < ActionMailer::Base
def welcome(recipient)
attachments['free_book.pdf'] = File.read('path/to/file.pdf')
mail(:to => recipient, :subject => "New account information")
end
end
Verify correct syntax
Verify correct filepath
Verify permissions on file are set correctly
Check your logs

Resources