I am playing around with action mailer, and I don't understand a couple of things
class MYMailer < ActionMailer::Base
default from: "bar#gmail.com"
....
end
in my production.rb file:
#config.action_mailer.default_url_options = { :host => 'www.example.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 111,
domain: 'gmail.com',
user_name: 'foo#gmail.com',
password: 'foobar',
authentication: 'plain',
enable_starttls_auto: true }
1) First of all what is this default from: "bar#gmail.com" ?
The from value is read from my production file, and I receive emails from foo#gmail.com. So what is the point of default from: "bar#gmail.com"
2) Second what is the point of #config.action_mailer.default_url_options = { :host => 'www.example.com' } ? I read something on the official guides but I didn't get it. My app still sends email without it..
Thanks
1) Gmail Api Do not support sent email from different adress(protect from spam/spoof)
2) Rails can send email from self application and this options tell him what host use.
Related
I am using devise gem,when i click the link from email Confirm my account after confirmable from email.
My sign in page is not working.
Put this:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
In development.rb file?
I have a Ruby web app that sends email via Mailgun.
My Mailgun account & gem are properly set up and I can send emails manually (via curl, for instance).
The API key and the API base URL (https sandbox domain) are stored in environment variables.
When I attempt to send emails from the app like this:
def initialize(mailer: nil)
#mailer = mailer || Mailgun::Client.new(ENV['MAILGUN_API_KEY'])
end
then:
def call(user)
mailer.send_message(ENV['MAILGUN_SANDBOX'], {from: '...',
to: user.email,
subject: '...',
text: "..."})
end
When I run the app with Sinatra via localhost:xxxx, I get a Mailgun::CommunicationError at /.../... 301 Moved Permanently: ... nginx pointing to this line:
mailer.send_message(ENV['MAILGUN_SANDBOX'], ...
Any idea why that happens? I've researched the issue for hours but couldn't find a clue on what to do next.
Thanks!
I ran into this same issue. If you have already fixed this then hopefully this can help someone else.
I switched over to message builder for ease of use and being able to render my html but I'm pretty sure it will still send with the format you have setup with :text
When I switched over to the proper domain in the .env file I believe it solved my issue. You'll need 2 different domains to use Mailgun. The first is the full domain for your sandbox. ENV['MAILGUN_DOMAIN'] it is the sandbox domain with the full https://api.mailgun.net/v3/sandboxXXXXxxxXXXXXX.mailgun.org to send most of the mail formats.
You'll also need the last half of the full domain to send messages. That's just the sandboxXXXXxxxXXXXXX.mailgun.org which is passed into the MessageBuilder or other message .send_message method. When I had them mixed up or both the same I kept on getting this error. When I switched over to separate the two in my development.rb and some_mailer.rb is when I could send the mail without a problem.
Below is my file setup, for reference. I'm pretty new to all of this but this is how I'm setup and it's working for me so hopefully it helps.
# .env
MAILGUN_DOMAIN='https://api.mailgun.net/v3/sandboxXXXXxxxXXXXXX.mailgun.org'
MAILGUN_SEND_DOMAIN='sandboxXXXXxxxXXXXXX.mailgun.org'
# development.rb
ActionMailer::Base.smtp_settings = {
:authentication => :plain,
:address => "smtp.mailgun.org",
:port => 587,
:domain => "ENV['MAILGUN_DOMAIN']",
:user_name => "ENV['MAILGUN_USERNAME']",
:password => "ENV['MAILGUN_PASSWORD']"
}
# some_mailer.rb
def some_mail_notification(user)
#user = user
mg_client = Mailgun::Client.new ENV['MAILGUN_KEY']
mb_obj = Mailgun::MessageBuilder.new
mb_obj.from "email#testing.com", {'first' => 'Customer', 'last' => 'Support'}
mb_obj.add_recipient :to, #user.email, { 'first' => #user.first_name, 'last' => #user.last_name }
mb_obj.subject "Your Recent Purchase on Some Site"
mb_obj.body_html ("#{render 'some_mail_notification.html.erb'}")
mg_client.send_message("sandboxXXXXxxxXXXXXX.mailgun.org", mb_obj)
end
I left the send_message above to the sandbox domain but you can set that as an environment variable in the .env file.
I am trying to send an email to example#gmail.com with Action Mailer (Ruby on Rails). The method sendactivation is correctly executed and the message "Email sent" is displayed. However, I never receive any email. Actually, the output "Test" is never printed. My webapp is hosted on Heroku Cedar-10.
class UsersController < ApplicationController
def sendactivation
UserMailer.welcome_email()
render :json => {
:result => "Email sent"
}
end
end
end
class UserMailer < ActionMailer::Base
def welcome_email()
mail(to: "example#gmail.com",
body: "Hello",
content_type: "text/html",
subject: "Already rendered!")
puts "Test"
end
end
This is the configuration I have on my config/environment/production.rb. Actually I wanted to send it with Office 365 but I suppose it is easier to debug with a Gmail account.
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
user_name: 'example#gmail.com',
password: '########',
authentication: 'plain',
enable_starttls_auto: true }
What am I doing wrong? Is there anything I need to change on my Gmail configuration?
ANSWER: In addition to the marked answer, I needed to set the "from" address in the welcome_email method.
Call method deliver:
UserMailer.welcome_email.deliver
UserMailer.welcome_email.deliver_now
From the Rails Guide: See section 2.1.4
class SendWeeklySummary
def run
User.find_each do |user|
UserMailer.weekly_summary(user).deliver_now
end
end
end
I am in need of a script which automatically sends particular data from my corporate webmail email id .
Till now I am able to send automatic emails from a gmail id. But Iam unable to configure it for my webmail id.
Please let me know if any configuration changes are needed or I need to setup a server for this.(if possible also help me how to configure the server)
This is the Ruby function which I am using
def send_mail(to_recepient,data,mailSubject,extraBodyText,sender_info)
options = { :address => "smtp.gmail.com",
:port => 587,
:domain => 'mail.gmail.com',
:user_name => sender_info[:senderName],
:password => sender_info[:senderPassword],
:authentication => 'plain',
:enable_starttls_auto => true }
Mail.defaults do
delivery_method :smtp, options
end
Mail.deliver do
to "#{to_recepient}"
from 'mailtest20152#gmail.com'
subject mailSubject
body stringData
fh=File.open('attachment_file',"w")
fh.puts data
add_file :filename => 'attachment_file', :content => data
end
File.unlink('attachment_file')
end
I faced similar problem. You can configure mailer for particular smtp server like this:
options = { :address => "smtp.yourdomain.com", #address can differ
:port => 25 }
Don't forget to add:
config.action_mailer.delivery_method = :smtp
Don't need to provide password and username, but remember to specify from field in your email message (as you already did).
I recently installed ActiveAdmin and I am working on the User model. After I created the initial AdminUser I tried adding another AdminUser and its supposed to send an email to set up the password but it fails to send the email.
I have this code in my config/development folder
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
and this in my AdminUser model
after_create { |admin| admin.send_reset_password_instructions }
def password_required?
new_record? ? false : super
end
Not sure why its not sending the email for me to change my password.
You are getting problem because you didn't configure any server to go out an emails.
You are on right path. just add following things.
Please add following line to app/Gemfile and run bundle install.
gem "letter_opener"
and then add following line to config/enviornments/development.rb
config.action_mailer.delivery_method = :letter_opener
Above code will help you to see the result in the browser itself, doesn't actually sends the email.
To send an actual email you need to change following line and need to add smtp code.(smtp server)
config.action_mailer.delivery_method = :smtp
Then add following lines below above line:
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 587,
:domain => 'gmail',
:user_name => 'gmail username',
:password => 'gmail password',
:authentication => 'plain',
:enable_starttls_auto => true
}