I'm following a paid tutorial exactly and setting up a contact page where when a visitors fills out the form and submits, I, the website owner, get an email with their name, email, and comment. The goal is to allow me to easily hit reply and respond to them.
This seems weird to me, because it's odd that ActionMailer gives you the capability to send from someone else's email account for which there are no SMTP settings defined. In fact, following this tutorial, I don't need to declare any SMTP settings.
But it's not working... would love some troubleshooting help.
My mailer code:
class UserMailer < ActionMailer::Base
def contact_email(contact)
#contact = contact
mail(to: jdong8#gmail.com, from: #contact.email, :subject => "New message at JamesDong.com")
end
end
Controller code snippet:
def create
#contact= Contact.new(secure_params)
if #contact.save
UserMailer.contact_email(#contact).deliver
You can clone the git repo at
https://github.com/RailsApps/learn-rails
to get the code from the book Learn Ruby on Rails. You'll see that the code works as implemented.
If you look at the example code, the SMTP settings are configured in the file config/environments/development.rb and config/environments/production.rb.
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
The GMAIL_USERNAME and GMAIL_PASSWORD set up the SMTP origin for the mail.
The UserMailer code only creates (part of) the header and body of the email message. The "from" and "to" will be displayed, for appearances only. Have a look at the raw email message and you will see the full set of headers, that show the real origin of the email.
So, in short, the UserMailer code sets a fake "from" and the real "from" is set when the email is sent from the Gmail account.
I could be wrong here, but based off of my experience in Rails this isn't possible, you would need to have the SMTP settings for the account you are sending the mail from.
There are a ton of questions on here referring to setting up ActionMailer correctly, but it seems like you're trying to send mail without telling it where to send the mail from.
This question and the best answer on it might be of some help if that's your issue:
How to configure action mailer (should I register domain)?
Related
So my client has reported that many of the emails are going to the wrong person, and I would like to write some feature tests to find and make sure that they are receiving the email and what it says in my specs.
I have mandrill_mailer which uses mandril api, and before it sends out I would like to see what the message is.
For example. Create a new user account -> creates the user, and then sends out a welcome email. in devise it calls RegistrationMailer.new_registration(resource).deliver
which then sends a email to the user:
def new_registration(user)
user = User.find_by_email(user["email"])
mandrill_mail template: 'new-registration',
subject: 'Welcome to ContentBlvd!',
to: { email: user["email"], name: user["full_name"] },
vars: {
'first_name' => user["full_name"],
'unsubscribe' => "#{CONFIG[:protocol]}#{CONFIG[:host]}/unsubscribe?email=#{user.email}"
}
end
In my mailer how do I test this mail object?
I tried ActionMailer::Base.deliveries, but it returns nil (Since I'm using mandrill mailer...)
So I tried - MandrillMailer::TemplateMailer.message
But no luck.... Thanks for the help.
At this point, MandrillMailer appears to support a robust offline testing set of options. In particular, MandrillMailer.deliveries can now be examined for expectations or debugging purposes.
As per wiki you need to mock Excon
# spec/rails_helper.rb
config.before(:all) do
Excon.defaults[:mock] = true
Excon.stub({}, {body: "{\"html\":\"RESULT\"}", status: 200})
end
You don't have to use the Mandrill gem to send emails using Mandrill. Just use the Rails default Mailer and add the configuration to application.rb or production.rb to use your Mandrill's account.
http://help.mandrill.com/entries/21738467-Using-Mandrill-s-SMTP-integration-with-Web-Frameworks
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.
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.
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
I'm using my Gmail Apps for Domain account to send email within my rails application for standard automated emails (user signup, forgot password, notify admin of new comment, etc), but I'm worried about the 500 emails per day limit set by Google.
Google suggests one way to overcome the limit is to use multiple user accounts.
So, I've setup 10 additional gmail user accounts (noreply1, noreply2, noreply3, etc) - I'd like to track when any of these accounts has sent 500 emails in a 24 hour period and use the idle account accordingly.
How do I dynamically set the :user_name value in ActionMailer::Base.smtp_settings?
Here's my current setup - NOTE: this sends from "noreply1" every time, even though i'm explicitly setting :user_name and :from to "noreply2":
--- development.rb ---
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "mydomain.com",
:authentication => :plain,
:user_name => "noreply1#mydomain.com",
:password => "password"
}
--- account.rb --- (MODEL, called via a callback)
after_create :send_welcome_email
...
def send_welcome_email
#ActionMailer::Base.smtp_settings[:user_name] = 'noreply2#mydomain.com'
ActionMailer::Base.smtp_settings.merge!({:user_name => "noreply2#mydomain.com"})
SubscriptionNotifier.deliver_welcome(self)
end
--- subscription_notifier.rb --- (MODEL)
class SubscriptionNotifier < ActionMailer::Base
def welcome(account)
#sent_on = Time.now
#subject = "Welcome to the App"
#recipients = account.email
#from = "noreply2#mydomain.com"
#body = { :account => account }
end
end
You could also set up an MTA on your server and use that for sending the mail.
That's what we do.
You have to add your server's IP as a valid one for sending email in your domain's SPF record to avoid getting marked as spam.
Another benefit of this is that if you do this, you can set the From: address of the email to be one of your users, which you cannot do with GMail.
Store the available usernames in a table in the database, along with a 'last-modified', 'last-reset' and a sent count. You can then query this when sending an email to find the least used email address currently. Then increment the sent count and last-modified account. The 'last-reset' value can be used for your cleanup code so that you reset the counts each 24 hour period.
This also makes it easy to add new email accounts, retire accounts you aren't using anymore, implement in a different app, etc. as it's all just in a database table that you can change when required.
You should be able to set the :user_name element in the hash in the mailer in the same fashion as in the configuration, namely by doing:
ActionMailer::Base.smtp_settings[:user_name] = 'new_user_name'
Although this may require some extra code to force a reload of any internal action mailer config (not tested this myself)
The comment box was becoming too restrictive for my questions. Changing the ActionMailer::Base.smtp_settings hash dynamically works as expected for me, so I suspect there is some other factor at play here. Some things to try:
Are you using a TLS plugin? I used action_mailer_optional_tls with Rails 2.3.2 and Ruby 1.8.6.
What is being writing to the log/console?
You're changing the username but not the password: do all the noreply accounts have the same password?
Edit: more things to try
I'd have a good look at that smtp_tls.rb file mentioned in the comments to make sure nothing's hard-coded. Or remove it and try the plugin I linked above. To use it, you just need to add :tls => true to the smtp_settings hash.