I'm trying to create a simple blog (using rails 5) and want to send email updates when to users when I post something new. I'm trying to use actionmailer and I'm using devise to register users. I'm trying to set it up with just the first user initially.
When I look at my local server the email appears to be sending but it's not received. Any advice would be very welcome I've been stuck a while.
UserNotifierMailer#sample_email: processed outbound mail in 410.4ms
Sent mail to myemail#gmail.com (853.2ms)
Date: Wed, 27 Jul 2016 12:05:33 +0100
From: from#example.com
To:myemail#gmail.com
Message-ID: <5798957d9de31_ae813ff962abfe1466438#Unknown-7c-d1-c3-78-04-d2.home.mail>
Subject: Sample Email
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_5798957d951e2_ae813ff962abfe1466312";
charset=UTF-8
Content-Transfer-Encoding: 7bit
My posts controller looks like this:
def create
#user = User.first
#post = current_user.posts.build(post_params)
if #post.save
flash[:success] = "Your post has been created!"
UserNotifierMailer.sample_email(#user).deliver
redirect_to posts_path
else
flash[:alert] = "Your new post couldn't be created! Please check the form."
render :new
end
end
My mailer looks like this:
class UserNotifierMailer < ApplicationMailer
default from: "from#example.com"
def sample_email(user)
#user = user
#url = 'http://www.google.com'
mail(to: #user.email, subject: 'Sample Email')
end
end
and in my development.rb I have these settings:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: "my_gmail_username",
password: "my_gmail_password"
}
In your configured Gmail account:
Go to Account Settings Than
Go to Sign-in & security, followed by Connected apps & sites
At last in Allow less secure apps ... Check it.
Check Once And Try
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
...
config.action_mailer.delivery_method = :smtp
I don't config any of the above and it still works.
config.action_mailer.default_url_options = { host: 'localhost:3000' }
and have to .deliver_later or .deliver_now, .deliver not work
Related
I'm working on a Rails app which should send a confirmation email when a new user registers.
My mailer looks like this:
def notify_email(volunteer)
#volunteer= volunteer
#admin = Volunteer.first
mail(to: #admin.email, subject: 'New Application from ' + volunteer.first_name+ '!', body:
'You have new applicants, please go check on your admin dashboard!
You can contact ' + #volunteer.first_name+ ' at: ' +#volunteer.email+ '.')
end
end
In my production.rb file I have email configurations set as follows(gmail username/password replaced with x's):
config.action_mailer.perform_caching = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
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 => "gmail.com",
:user_name => 'xxxxxx',
:password => 'xxxxxxx',
:authentication => "plain",
:enable_starttls_auto => true
}
My controller has this code:
def create
#admin= Volunteer.first
#volunteer = Volunteer.create(volunteer_params)
#volunteer.save
if #volunteer.save
flash[:success] = "Successfully Registered!"
#email notifes admin of new registration
NotifyMailer.notify_email(#volunteer).deliver_now
redirect_to '/volunteer'
end
And finally when I test the app on the local server the console gives me this information(emails x'ed out again):
NotifyMailer#notify_email: processed outbound mail in 1.5ms
Sent mail to xxxx#xxxx.org (2004.5ms)
Date: Sun, 06 Jan 2019 16:04:49 -0600
From: xxxxxxx#gmail.com
To: xxxxx#xxxxx.org
Message-ID: <5c327b817c32e_3de83ca21e89202#LAPTOP-N2MQ7EL0.mail>
Subject: New Application from Rachel!
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Can anyone give me insight into what I'm missing? I've checked several forums and threads and it seems like I'm following the right steps.
Thanks in advance.
Unable to comment, but what happens if you call
NotifyMailer.notify_email(#volunteer).deliver
instead of
NotifyMailer.notify_email(#volunteer).deliver_now
I believe deliver_now relays on a background worker like sidekiq,
plus try the
gem "letter_opener" which will open a new tab in your browser to check your emails.
I'm currently working on an application with a mailer system. It was working fine, sending a welcome email and sending instructions to reset password, but now and only when I try to send reset instructions I have this error.
ArgumentError (SMTP From address may not be blank: nil):
I'm using a custom domain like so noreply#mycustomdomain.com
And here is my configuration
development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
config.action_mailer.default_url_options = { host: 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: '587',
domain: 'gmail.com',
authentication: :plain,
enable_starttls_auto: true,
user_name: Rails.application.secrets.mailer_username,
password: Rails.application.secrets.mailer_password
}
Any idea ?
Edit
class UserMailer < ApplicationMailer
default from: 'noreply#mycustomdomain.com'
def welcome_email(user)
#user = user
#url = 'http://localhost:3000/users/sign_in'
mail(to: #user.email, subject: 'Bienvenue')
end
def generate_new_password_email
user = User.find(params[:user_id])
user.send_reset_password_instructions
end
def reset_password; end
end
You could try setting :from in your config, using the default_option like this,
config.action_mailer.default_options = { from: 'noreply#mycustomdomain.com' }
It looks like you don't have a From header in your email. A good practice would be to put the following line into your ApplicationMailer:
class ApplicationMailer
default from: 'noreply#mycustomdomain.com'
# ...
end
To override this in your inheriting mailers, simply declare the same statement. To override it in individual mail methods, put it into the mail call like so:
def new_message(user, message)
mail(
to: user.email,
subject: "New message from #{message.sender.name}",
from: message.sender.email
)
end
Hope that helps
In devise.rb config.mailer_sender = 'please-change-me-at-config-initializers-devise#example.com' had been commented.
Config.mailer_sender was never initialized and so always nil even if I set it with default from:
I had the same problem, and the reason behind that was i working in development but my mailer was searching the smtp_settings in the production, so to solve the issue you can change the mailer settings or you can copy the same smtp_settings into production.
I stumbled upon the same problem. My solution was, I edited config/initializers/devise.rb changed a line from config.mailer_sender = 'please-change-me-at-config-initializers-devise#example.com'
to config.mailer_sender = ENV["default_from_email"]
I'm trying to send an email using ActionMailer on my local development environment. Everything's working fine, except the attachments.
In the following code block I attach the JPG file to an email, "Testbild.jpg", which has a size of 6,19KB. However, the receiver sees an email with a file of the same name that has a size of only 96 bytes.
def welcome_email(user)
#user = user
#url = 'http://example.com/login'
attachments['Testbild.jpg'] = File.read("#{Rails.root}/public/images/Testbild.jpg")
mail(to: #user.kontakt.email, subject: 'Welcome to My Awesome Site')
end
Here is my ActionMailer Config in the development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
config.action_mailer.default_url_options = { host: 'localhost', port: 3001 }
config.action_mailer.smtp_settings = {
user_name: "christian.henschel#xclirion.de",
password: "************",
domain: "xclirion.de",
authentication: :plain,
address: "smtp.xclirion.de",
openssl_verify_mode: OpenSSL::SSL::VERIFY_NONE,
enable_starttls_auto: true,
port: 587
}
I found out that the 96 bytes of the received file are exactly the same as the first 96 bytes of my source file. The rest seems to be lost.
What goes wrong here? How can I force RoR to send the whole file?
Try to change your attachments hash as shown below:
def welcome_email(user)
#user = user
#url = 'http://example.com/login'
attachments['Testbild.jpg'] = File.open("#{Rails.root}/public/images/Testbild.jpg", 'rb'){|f| f.read}
mail(to: #user.kontakt.email, subject: 'Welcome to My Awesome Site')
end
I didn't figure out how I send mail from gmail in development environment.It didn't send email. I didn't understand the rails guide, and also I wonder if the production env is the same ?
config/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'something.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'mail.google.com',
user_name: 'myusername#gmail.com',
password: 'mypassword',
authentication: 'plain',
enable_starttls_auto: true }
mailer/user_mailer.rb
default :from => 'something.com'
def welcome_email(user)
#user = user
#url = 'http://something.com'
mail(to: #user.email, subject: 'Welcome')
end
edit
where I call, in users create method,
UserMailer.welcome_email(#user).deliver_now
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
Try this in development.rb It will either send mail or raise delivery error in console.
You can call your mailer methods in two way to send email,
In UsersController's create action
def create
#your codes and logics to save user...
UserMailer.welcome_email(#user).deliver_now
#your codes goes here ...
end
In User model after_create callback
class User < ActiveRecord::Base
after_create :send_email
def send_email
UserMailer.welcome_email(self).deliver_now
end
end
I would prefer second one for sending email, use model callback rather in controller.
First in my project, I can receive the registration confirmation email sent by devise gem and mandrill service. So I assume my email config settings are correct. However, my own send_mail function just cannot send email successfully.
My mailer settings:
config.action_mailer.smtp_settings = {
address: "smtp.mandrillapp.com",
port: 587,
domain: Rails.application.secrets.domain_name,
authentication: "plain",
enable_starttls_auto: true,
user_name: Rails.application.secrets.email_provider_username,
password: Rails.application.secrets.email_provider_password
}
# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_controller.asset_host = 'localhost:3000'
config.action_mailer.asset_host = 'http://localhost:3000'
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
# Send email in development mode?
config.action_mailer.perform_deliveries = true
Mailer Class:
class BidMailer < ActionMailer::Base
default from: "service#xxx.com"
def send_mail(bid)
#bid = bid
mail(to: 'xxxxx#gmail.com', subject: 'Bid Accepted', reply_to: 'service#xxx.com')
end
end
In the controller I simply call BidMailer.send_mail(bid).deliver. I also have the html file for in the view for the email body.
I can see this in my server console log:
Sent mail to xxx#gmail.com (24.0ms)
Date: Wed, 11 Mar 2015 22:39:45 +1100
From: service#xxx.com
Reply-To: service#xxx.com
To: xxx#gmail.com
Message-ID: <550029815f07_782a3fec29fa23c8413c#Shanes-MacBook-Pro.local.mail>
Subject: Bid Accepted
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
And also I can see similar email log when devise sends out registration confirm email.
But I totally dont get why I cannot get any email sent by my send_mail function?
Anyone could give some help? Thanks heaps!