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
Related
Im running this on heroku. I have verified the domain with sparkpost. It says ready to send. I changed The real url to "example".
mailers/order_mailer.rb
class OrderMailer < ActionMailer::Base
default from: "orders#example.com"
def new_order(order)
#order = order
mail(to: #order.user.email, subject: 'Order Created!')
end
end
config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: ENV['SPARKPOST_SMTP_HOST'],
port: ENV['SPARKPOST_SMTP_PORT'],
user_name: ENV['SPARKPOST_SMTP_USERNAME'],
password: ENV['SPARKPOST_SMTP_PASSWORD'],
domain: 'example.com',
authentication: :plain
}
config.action_mailer.default_url_options = {
:host => 'example.com'
}
error
2018-10-10T02:01:02.371812+00:00 app[web.1]: [3054201e-f06a-4acd-bff8-58947353fb72] Net::SMTPFatalError (550 5.7.1 Unconfigured Sending Domain <example.com>
I cant think of anything else, but if you need anything let me know.
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"]
The following is my controller file:
class BiodataController < ApplicationController
def store
#user=params[:username]
#age=params[:age]
#gender=params[:gender]
#mstatus=params[:mstatus]
#language=params[:language]
#email=params[:email]
#mobile=params[:mobile]
if params[:username].present? && params[:age].present? && params[:gender].present? && params[:mstatus].present? && params[:language].present? && params[:email].present? && params[:mobile].present?
Biodatum.create(name: #user, age: #age, gender: #gender, mstatus: #mstatus, language: #language, email: #email, mobile: #mobile)
Infomail.sendmail(#email)
render 'store'
else
render 'Error'
end
end
end
My requirement is to send email to the address stored in #email. So I created the mailer as 'Infomail'. The following is my mailer file.
class Infomail < ApplicationMailer
default from: 'abc#xyz.co.in'
def sendmail(user)
#user = user
mail(to: #user.email, subject: 'sample mail')
end
end
And I also have html file under 'app/views/infomail/sendmail.html.erb'. But it doesn't work. Can any one explain me what is the bug in
my code.
I'll recommend that you take a look at http://guides.rubyonrails.org/action_mailer_basics.html for starters.
For sending mails, you'll have to use the deliver_now (Immediate) or deliver_later (Queue for worker) methods, like so:
InfoMailer.sendmail(#user).deliver_now
Note that I renamed it to InfoMailer, because that's the standard naming, of mailers.
If you want to test you mailer into development environment just open your development.rb file and write below code inside for smtp configuration.
config.action_mailer.default_url_options = { host: 'url of your application' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'address of your mailer',
port: 465,
domain: ENV['YOUR_DOMAIN_NAME'],
user_name: ENV['YOUR_USER_NAME'],
password: ENV['YOUR_PASSWORD'],
authentication: :plain,
ssl: true,
tls: true,
enable_starttls_auto: true
}
please change above configuration as per your mailer smtp settings.
Now create tamp let of your mail as per you requirements. file path as per below:
app -> view -> common_mailer inside of this please make sendmail.html.erb file.
You are sending #email which is a String in params while you are expecting Object on which you can call .email
#bio = Biodatum.create(name: #user, age: #age, gender: #gender, mstatus: #mstatus, language: #language, email: #email, mobile: #mobile)
Infomail.sendmail(#bio)
This will pass Biodatum instance on which you can call .email
Also to avoid confusion you can change your mailer method
def sendmail(bio)
#bio = bio
mail(to: #bio.email, subject: 'sample mail')
end
If you are making these changes, you will also need to change them in sendmail.html
EDIT
Make sure you have configured the settings in /config/environments/development.rb and /config/environments/production.rb
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => ENV['gmail_username'],
:password => ENV['gmail_password'],
:authentication => "plain",
:enable_starttls_auto => true
}
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
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.