Is it possible to send emails from the current user email? - ruby-on-rails

I'm developing an internal operations manager for a company, They need to send automatic emails to their costumers, but the email has to be sent by the email address of the company operator that is taking care of that specific costumer.
So basically, I need to configure my mailer to work with N different emails (I have access to their emails passwords too).
config.action_mailer.smtp_settings = {
:address => 'smtp.office365.com',
:port => '587',
:authentication => :login,
:user_name => ENV['SMTP_USERNAME'],
:password => ENV['SMTP_PASSWORD'],
:domain => 'interworldfreight.com',
:enable_starttls_auto => true
}
I've seen that it is possible to send the email address to the mailer config this way:
def new_mail
mail from: "example#example.com", to: "user#example.com"
end
But what about the password?, is this even possible through only rails or should I consider other tool like MailGun ?
Thanks a lot for taking a moment to read this.

Apparently you can send a delivery_options object with the username, host and password.
class FclExwOperationMailer < ApplicationMailer
def info_request (representative, shipper)
#representative = representative
#shipper = shipper
delivery_options = { user_name: representative.smtp_user,
password: representative.smtp_password,
address: representative.smtp_host }
mail(to: shipper.email,
subject: "Please see the Terms and Conditions attached",
delivery_method_options: delivery_options)
end
end

Related

Email - From address is the logged in address and not the address that specified in 'from' attribute

Below is the code for the mailer which sends the verification email:
ApplicationMailer
class ApplicationMailer < ActionMailer::Base
default from: "from#example.com"
layout 'mailer'
end
SendVerificationMailer
class SendVerificationMailer < ApplicationMailer
def send_verification(verification_id)
#verification = Verification.find(verification_id)
#email = #verification.email
#token = #verification.token
mail :from => "Support <support#domain.io>", :to => #email, :subject => "User verification code"
end
end
In production.rb I have configured the action_mailer as follows:
# Mailing preferences
config.action_mailer.default_url_options = { :host => ENV["HOST_NAME"] }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => ENV["SMTP_ADDRESS"],
:port => ENV["SMTP_PORT"],
:domain => ENV["SMTP_DOMAIN"],
:authentication => :login,
:user_name => ENV["EMAIL"],
:password => ENV["PASSWORD"]
}
I used an account info#domain.io for smtp user_name and password. When I send Email from local machine it is working fine and the from address in the received mail is support#domain.io in both development and production mode.
But, when the mail is sent from production machine in AWS the from address displayed as info#domain.io and not support#domain.io what I expect to be.
I am using AWS Elastic Beanstalk for deployment in which I have a web server and a worker, where the worker takes the attributes from SQS queue and send the Email.
Can anyone help me in fixing this.
Thank you..
Some email providers such as GMAIL (which you are using) do not allow overriding the from email address. It must be the email address of the GMAIL account holder.
If you have a requirement to change the from email address you'll need to look at other providers.
You may want to look at MailGun www.mailgun.com which is an email service that's very developer-friendly (even has sample Ruby code in its documentation) and allows up to 10,000 emails per month for free.

Specifying "from" email address when sending mail from Rails app

I'm using gmail to manage the email for my rails app domain.
The google account is, say, account_owner#gmail.com
But the "from" email address should be, say, info#mysite.com
When I configure smtp_settings as below, the email is sent, but the "from" email address is account_owner#gmail.com. I want it to be info#mysite.com, but if I change the :user_name to info#mysite.com and its password, my app seems to send the email, but it is never received. How can I achieve this?
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "account_owner#gmail.com",
:password => "thepassword",
:authentication => "plain",
:enable_starttls_auto => true
}
You cannot change the "from" address when sending mail via Gmail. (Unless maybe if you've configured that account to have other "send as" addresses, though I've never tested it)
I would suggest trying the free hosted Gmail for your domain, or use a third-party service like Mandrill.com
You shouldn't specify it as :user_name in your smtp_settings. Instead you should set the :from option like this:
ActionMailer::Base.default :from => 'info#mysite.com'
Alternatively you can set the from address when you send the email:
mail(:to => 'info#theirsite.com', :from => 'info#mysite.com' :subject => '...')

rails 3: how can my app send from multiple email accounts?

In my environment.rb file I have:
ActionMailer::Base.smtp_settings = {
:address => "smtp.example_host.com",
:port => '25',
:domain => "example_send_from.com",
:authentication => :plain,
:user_name => "send_account_name",
:password => ENV['MY_EMAIL_SEND_PWD']
}
It works fine.
However, now my app has a new class of emails that need to be sent via a different email account... perhaps gmail in some low-volume cases, sendgrid in other high volume cases.
I'm sure it's pretty simple -- but how do I override the default Base.smtp_settings setting on an email-by-email basis?
do you have separate actionmailers for each email account? then you could set the smtp settings per action mailer-class:
class Mailer1 < ....
self.smtp_settings = { .... }
end
etc.

Can I send email from different addresses using same ActionMailer

I'm using ActionMailer for my Rails 2.3.9 application.
When I send an email using:
deliver_user_invite
config:
def user_invite(subject, content)
subject subject
from "User Invite <invite#mydomain.com>"
recipients "invites#mydomain.com"
sent_on Time.now
content_type "text/html"
body :content => content
end
with SMTP configuration
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'mydomain.com',
:authentication => :plain,
:user_name => 'user#mydomain.com',
:password => 'password'
}
However when the email is sent, the sender email shows as user#mydomain.com instead of invite#mydomain.com.
Can I have different SMTP configuration for different email addresses? or Is there a way to set the sender email address from ActionMailer config?
This is a Gmail SMTP limitation. It will always make the sender of the e-mail be the username/login you use for your smtp settings, and will ignore your from address.
A possible workaround might be to change the smtp settings dynamically when you need to send as someone else.
Edit: You might be able to go into the settings for your own Gmail account and use the "Add another email address you own" option to allow your account to send through those e-mail addresses. I haven't tested it, but it might work. (See http://www.mobileread.com/forums/showpost.php?p=21093&postcount=1).

Rails: Runtime configuration of ActionMailer?

I would like to send a small amount of email from my app through Gmail. Now, the SMTP settings will be determined at runtime (ie: from the db), can this be done?
--- edit ---
I can set the ActionMailer subclass (named Notifier) smtp settings in one of the class' methods. This way I can set the username and password for sending the email dynamically. The only thing is that you have to set ALL the smtp_settings. Is it possible to set just the username & password settings in the class method?
This is the code I'm using right now, it is sending:
class Notifier < ActionMailer::Base
def call(user)
Notifier.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "mydomain.com",
:authentication => :plain,
:user_name => "fabian#mydomain.com",
:password => "password"
}
recipients user.email
subject "Test test"
body "Test"
end
end
I would like to just set the username and pw here.
(Rails 3)
Since I call mailer like this:
CustomerMailer.customer_auto_inform(#form).deliver
In CustomerMailer class I have private method:
def init_email_account(shop_mail)
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:address => shop_mail.address,
:port => shop_mail.port,
:domain => shop_mail.domain,
:user_name => shop_mail.user_name,
:password => shop_mail.password,
:authentication => shop_mail.authentication.name,
:enable_starttls_auto => shop_mail.enable_starttls_auto
}
end
Before calling mail() which sends email you need to call private method init_email_account to populate smtp_settings from database. shop_mail is model which stores the data about mail account settings.
HTH
Since the configuration files are all Ruby, then the settings can easily be fetched from a configuration file or the like at runtime.
Here's a post I wrote a while back on getting ActionMailer working with GMail SMTP.
NOTE: If you're using rails 2.3 and Ruby 1.87, you don't need the plugin and can simply use the settings in this comment
Assuming you've configured the smtp_settings in your environment or initializers, you can just set the username like so:
Notifier.smpt_settings.merge!({:user_name => "x", :password=> "y"})
You can do that in the controller:
class ApplicationController < ActionController::Base
...
private
def set_mailer_settings
ActionMailer::Base.smtp_settings.merge!({
username: 'username',
password: 'yoursupersecretpassword'
})
end
end

Resources