is there any possibility to check a normal mail box with rails?
I want to check if there are new incoming mails, I want to open those and read the text to find a special string in it.
Is this possible? and how?
Mail Gem may helps you.
You can configure Mail to receive email using retriever_method within Mail.defaults:
Mail.defaults do
retriever_method :pop3, :address => "pop.gmail.com",
:port => 995,
:user_name => '<username>',
:password => '<password>',
:enable_ssl => true
end
You can access incoming email in a number of ways.
The most recent email:
Mail.all #=> Returns an array of all emails
Mail.first #=> Returns the first unread email
Mail.last #=> Returns the last unread email
Related
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
in my project I will have to write different cron jobs to read emails from different email addresses to do different tasks. I am using mail gem but the problem is, retriever_method is singleton. So when I'll mention the new email address and new password the previous settings of retriever_method will be changed. So, I am not able to retrieve the emails when cron jobs are running at the same time.
Suppose, In my first cron job I have something like the following settings
Mail.defaults do
retriever_method :pop3, :address => "pop.gmail.com",
:port => 995,
:user_name => '<username1>',
:password => '<password1>',
:enable_ssl => true
end
In my second cron job, If I use something like
:user_name => '<username2>',
:password => '<password1>'
In that case both will be changed to username2
is there any workaround.
Or any other suggestion to do this job. I don't want to IMAP for some other reason.
Any suggestion will be appreciated.
Mail retrieve_method is singleton. so I am using Net::IMAP directly with the Mail to retrieve the attachment. One can use Net::Pop3 with Mail as well to resolve this issue.
My rails application uses a Ruby script retrieve an object from my database.
This objet is made of six fields that are:
login
password
server
port
event_type_id
active
Using the mail gem, I'm using these informations to create a new Mail connection using Mail.defaults, like this in my MailConnection model:
def check
begin
puts("Attempting to connect to the specified email server...")
Mail.defaults do
retriever_method :pop3, :address => :server,
:port => :port,
:user_name => :login,
:password => :password,
:enable_ssl => false
end
rescue Exception => msg
puts("Unconnected : #{msg}")
false
else
puts("Connected")
true
end
end
I'm using this method in the Ruby script to check if the connection can be etablished before doing anything, but if I'm using invalid datas, it's still returning 'true' even if the network is unreachable.
I'm all new to Ruby and, I would like to know what's wrong with my function;
Thanks in advance.
I'm a beginner myself but have just been having a look at this.
With net/pop
require 'net/pop'
pop = Net::POP3.new('your.mail.server', optional_port)
pop.start('your_username', 'your_password') #should return an error if it fails
pop.started? #if you want to double check
With the Mail gem
If you'd rather use the Mail gem itself, the connection method in the POP3 retriever lets you get at those same methods by yielding a POP3 object to a block:
Mail.defaults do
retriever_method :pop3, :address => :server,
:port => :port,
:user_name => :login,
:password => :password,
:enable_ssl => false
end
Mail.connection {|pop3| pop3.active? }
You might notice I used started? and active? interchangeably: they are the same POP3 instance method. One is simply aliased to the other.
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 => '...')
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).