Can I send email from different addresses using same ActionMailer - ruby-on-rails

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).

Related

Emails are not sent to addresses except verified addresses (Mailgun and Heroku)

I'm building a web app with Rails.
In the production environment, I want to use Mailgun to send account verification emails, but when I register with an address other than the ones I've authenticated and I am using for Heroku, I get the following error and can't send the emails.
Net::SMTPUnknownError (could not get 3xx (421: 421 Domain sandbox3fb706e74ad5432f99a8627384de2867.mailgun.org is not allowed to send: Sandbox subdomains are for test purposes only. Please add your own domain or add the address to authorized recipients in Account Settings.
Here is my production.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'protected-island-35085.herokuapp.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:port => ENV['MAILGUN_SMTP_PORT'],
:address => ENV['MAILGUN_SMTP_SERVER'],
:user_name => ENV['MAILGUN_SMTP_LOGIN'],
:password => ENV['MAILGUN_SMTP_PASSWORD'],
:domain => host,
:authentication => :plain,
}
I'm having trouble with this because I can't send emails to users after releasing the application.
The idea that Email cannot be sent to not-verified address if I use free-plan of Mailgun has come up to me, is that right?
How can I make it so that I can send emails to other addresses properly?
Also, if there is any missing information, please let me know and I will add it.

How do I send emails using Sendgrids API to send emails with rails 4? I can seem to put all the pieces together

I want to send emails with sendgrids API from a google cloud platform instance. They have a short tutorial on how to do this but it has the email message, who its from, who to send to, and other info all in app.rb which isnt the normal way messages are sent in rails apps.
I looked at the sendgrid ruby docs and they also dont have very good information. All the info in in one place and they dont state what files to put them in or even mention any smtp settings to use.
Here is what I have so far
development.rb
config.action_mailer.delivery_method = :smtp
# SMTP settings
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 465,
:domain => "mydomain.com",
:user_name => ENV['sendgrid_username'],
:password => ENV['sendgrid_password'],
:authentication => :plain,
:ssl => true,
:enable_starttls_auto => true
}
gemfile
gem "sendgrid-ruby"
The email views are in app/views, the mailer methods are in app/mailer the same as normal rails 4 apps have it setup.
So I guess here are my main questions:
Where do I call the environment variables holding the sendgrid api key?
Where do I tell Action Mailer to use sendgrid or SendGrid::Mail as I've seen in a couple of places?
Do I even need the sendgrid gem?
Are the smtp settings correct for sending over SSL with sendgrid?
I'm new to sending emails like this in a rails app and would really appreciate the help.
Put apikey as the name and the actual apikey as password:
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
port: 587,
domain: "yourwebsite.com",
authentication: :plain,
user_name: 'apikey',
password: Rails.application.secrets.sendgrid_api_key
}
Source
Use Figaro gem: https://github.com/laserlemon/figaro
It will generate an application.yml file for you, where you can store your sendgrid_username and sendgrid_password.
Have you generated your mailer? For example, generate mailer user_notifier where you can define your a default-from email, and some methods like this:
r
# send a signup email to the user, pass in the user object that contains the
user email address
default :from => 'hello#yourdomain.com'
def send_signup_email(user)
#user = user
mail( :to => #user.email,
:subject => 'Thanks for signing up!'
)
end
No, you don't need it.
https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/smtp_ports.html

Ruby on Rails ActionMailer SMTP Settings

trying to set up my own ActionMailer::Base in RubyRails -> This works:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "anythingworks",
:user_name => "mygmailaddress#gmail.com",
:password => "somePW",
:authentication => "plain",
:enable_starttls_auto => true
}
fine.. but it would be nice if I could use my mail server. but when i am trying this:
ActionMailer::Base.smtp_settings = {
:address => "smtp.myowndomain.de",
:port => 25, (tried 587) also
:domain => "triedanything",
:user_name => "mycorrectuser",
:password => "yesitisthecorretpw",
:authentication => "plain",
:enable_starttls_auto => true
}
Tried lot of different settings / changes. But no E-Mail is sent. Is there any logs avaiable to this ? Think its something because authentication... "login" instead of "plain" doesnt help...
Had some tests with telnet sending some E-mails from command line.
gmail doesnt work from command line (saying STARTTTLS or something). My own domain mails server (which is hosted from some provider) says "relay access denied" (from commandline).
Webmail both working..
Any ideads ? Thanks!
The answer is the "relay access denied" message. Your smtp server needs to be configured to relay emails for this to work. I suspect that your smtp server is configured to accept connections for delivery TO your domain, not to relay emails elsewhere.
The same error message will probably be found in the smtp server's log files. Try sending an email to yourself (which would just be an accept, rather than a relay, for your smtp server). If emails to yourself are delivered, but emails to other domains are not, you will have confirmation that that is the problem.
Your ISP probably has an smtp server you can use for outgoing emails. If you want to use your own email server, it will need to be configured to relay emails.

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 => '...')

Using ActionMailer with a company Gmail account

I'm not sure if this belongs in server fault or here feel free to move it if it makes more sense somewhere else. I've seen the examples for setting up the smtp settings and using ActionMailer with Gmail and confirmed that they work for me.
Basically it looks like this for me:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => '<username>',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
What I need to do now is to send an email to an address that isn't a plain Gmail account and yet, at it's core, is Gmail. My company uses whatever google's email service that allows you to use gmail but for the addresses to be listed as username#my.company.com rather than #gmail.com. I know for a fact that you can't simply log into our mail at the main gmail site so I assume our domain is different. Or something.
At the moment, when I simply use my own company user/pass I get an error message telling me that the user/pass was wrong. But I'm guessing the issue is that I'm trying to mail from the gmail variant of my username.
I have confirmed that our smtp server, as far as Thunderbird is concerned, is the normal gmail smtp, that our port is still 587, and that we are using TLS. What do I need to change here so that I can send an email to one of these addresses? Thanks.
I have my own domain setup at google for mail, the url to log into that directly is
http://mail.google.com/a/my.company.com
My rails app that sends mail through that account, has
:domain => "my.company.com"
as well as all the other fields you have.
"<user_name>" needs to be the whole email address, not just the user name.
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'busiess.com',
:user_name => 'username#company.com',
:password => 'password',
:authentication => 'plain',
:enable_starttls_auto => true

Resources