ActionMailer acting different on production server - ruby-on-rails

I have a action mailer method as something like this:
def mail
#receiver = User.where(status: 2).pluck(:email)
mail(bcc:#receiver, to: "username#gmail.com")
end
application.yml looks like this:
SMTP_ADDRESS: 'smtp.gmail.com'
SMTP_PORT: 587
SMTP_HOST: 'localhost:3000'
SMTP_DOMAIN: 'localhost:3000'
SMTP_USERNAME: 'user#gmail.com'
SMTP_PASSWORD: 'xxxxx'
SUPER_ADMIN_EMAIL: 'super_admin#mailinator.com'
developmet.rb looks like this:
config.action_mailer.asset_host = ENV["SMTP_HOST"]
# config.action_mailer.delivery_method = :letter_opener
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
#Enter the smtp provider here ex: smtp.mandrillapp.com
address: ENV["SMTP_ADDRESS"],
port: ENV['SMTP_PORT'].to_i,
#Enter the smtp domain here ex: vendaxo.com
domain: ENV["SMTP_DOMAIN"],
#Enter the user name for smtp provider here
user_name: ENV["SMTP_USERNAME"],
#Enter the password for smtp provider here
password: ENV["SMTP_PASSWORD"],
authentication: 'plain',
enable_starttls_auto: true
Where receivers are kept in BCC but "username#gmail.com" will be able to see the BCCd receivers. This is working fine when I send the mails from my local host. When receivers are sent emails, all of them are in BCC and one 'username#gmail.com' is able to see all the receivers.
But when I do the same thing on production server with similar application.yml configuration changing the host and port the username#gmail.com is NOT GETTING the emails of BCCd receivers.

I think if you are running your production server on AWS EC2 (may be other providers too), every time you do some changes to your application.yml or any other shared file, you need to restart the app server in order to apply the changed configurations . In my case I had to restart the PUMA, and the one default email started to get BCCd email ids.

Related

ArgumentError (SMTP-AUTH requested but missing user name)

I am working on a basic app called PhotoApp, where I am learning how to set up SMTP with rails. I have used basic Ruby with SMTP sometime ago, and I know my email works, just the emails sent gets to spam folder.
But when I am trying to configure SMTP on Rails, I am getting ArgumentError (SMTP-AUTH requested but missing user name):, and that's the primary reason why the app, when pushed to heroku, crashed every time a user signs up.
Here's my config/environments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost:8080' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.net',
authentication: 'plain',
enable_starttls_auto: true,
user_name: 'gmail_email',
password: '...'
}
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
I am running the server on localhost:8080, in development mode, so my host is set to localhost:8080.
Apart from that I am not able to get my app working with my gmail username with SMTP in rails.
Edit, this ruby code works:
#!/usr/bin/ruby -w
require 'net/smtp'
message = <<~EOF
From: mygmail#gmail.com
To: someone#protonmail.com
Subject: Hello!
Hello!
#{"HELLO ".*(50).delete_suffix(?\s)}
EOF
Net::SMTP.new('smtp.gmail.com', 587).tap(&:enable_starttls_auto).start('gmail.com', 'mygmail#gmail.com', 'mygmailpass', :plain) do |smtp|
smtp.send_message message, 'mygmail#gmail.com', 'someone#protonmail.com'
end
But as a newbie to rails, I can't get actionmailer working when the same email is used in the config file...

RefineryCMS inquiries not sending email

I'm working with an old RefineryCMS 1.0.8 project and migrating it to another server. It all works apart from the contact us form.
Here is my setup in the production.rb file
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
config.action_mailer.delivery_method = :sendmail
# Defaults to:
# config.action_mailer.sendmail_settings = {
# location: '/usr/sbin/sendmail',
# arguments: '-i -t'
# }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
if ENV['MAILER_USERNAME'].present? && ENV['MAILER_PASSWORD'].present?
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "my_domain.com.au",
user_name: "#{ENV['MAILER_USERNAME']}",
password: "#{ENV['MAILER_PASSWORD']}",
authentication: 'plain',
enable_starttls_auto: true
}
end
I've set the environment variables in my /etc/environment and /etc/apache2/envvars files like so;
export MAILER_USERNAME="<email address>"
export MAILER_PASSWORD="<password>"
I've also set the tld to a length of 2 as we are in Australia so we are using a .com.au. This is set in my config/application.rb
config.action_dispatch.tld_length = 2
Here is the error I get;
Sent mail to <my gmail address> (2924ms)
There was an error delivering an inquiry confirmation:
555 5.5.2 Syntax error. w20sm33182626pfi.31 - gsmtp
You should wrap your email in <> blocks.
export MAILER_USERNAME="<map#gmail.com>"
export MAILER_PASSWORD="password"
I found the problem was not the brackets around the email it was the fact that I hadn't setup the DNS to that machine yet. Once I did that last night and went to the refinerycms website at it's domain instead of the IP then I could send emails.

how to send mail through the localhost?

please help solve the problem.
i use webrick server on localhost. in development.rb i registered:
config/enviroments/development.rb:
Rails.application.configure do
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end
i use gem devise. and i user 'forgot youre password'-function (devise-recoverable module). the result did not get to the email message. but where can I find a mark(log) that was an attempt to send a letter? whether there is a directory where they are stored?
You will need to edit your config/environments/development.rb file.
Here's something to get you started, add this to that file:
# port 3000 or whatever port you are using for your localhost
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
# this line is what you want to be true, else you won't get messages!
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: 'gmail.com' # or to whatever domain your email is
authentication: "plain",
enable_starttls_auto: true,
user_name: 'my_gmail_or_other_username',
password: 'my_gmail_or_other_password'
}
Now, when you are moving towards production or even just pushing it to a public repository you obviously don't want to hardcode the username and password (for security purposes). For this I suggest you look up how to use environmental variables. The Figaro gem might also come in handy here and is another option.
This is the default 'smtp' way, there are other methods, such as the use of a gem as specified by the earlier answer.
try gem mailcatcher for development purpose.
http://mailcatcher.me/
github: https://github.com/sj26/mailcatcher
This gem will provide you a tab in browser where all your emails will be there unless you delete them.

Actionmailer cannot send using Gmail

With the same config/application.rb (see below), I can send with one Gmail account, but not for a second Gmail account. The second Gmail account is preferred, I verified the login credentials with a browser and have ensured it accepts "less secure apps".
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
user_name: 'accountname',
password: 'secret',
authentication: 'plain',
enable_starttls_auto: true
Also with the second Gmail, I got NET::SMTPAUthenticationError in production, and it just does not send in development mode without complaining.
Finally, I am using Rails console, and my objective is to use a rake task to send email instead of in the web environment.

Rails action mailer didn't work on production

I am trying to send emails via action mailer. I have the same config for production and for development:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'ee.ex.my_domain.net',
port: 465,
domain: 'my_domain',
user_name: 'email#my_mail.com',
password: 'my_pass',
authentication: :ntlm,
enable_starttls_auto: true
}
config.action_mailer.default_url_options = { :host => "localhost:3000" }
The only difference between development and production config is the host. For production I use an existing url, on my local machine it works ok but when I try it on the server I got this error:
Net::SMTPSyntaxError (504 5.7.4 Unrecognized authentication type)
I have had this error before on my local machine but I changed the authentication to :ntml and it works fine.
UPDATE
tried in server console (code from ntlm gem github page)
require 'ntlm/smtp'
smtp = Net::SMTP.new('smtp.example.com')
smtp.start('localhost.localdomain', 'Domain\\User', 'Password', :ntlm) do |smtp|
smtp.send_mail(mail_body, from_addr, to_addr)
end
but have the same error

Resources