While fetching email, TMail appears to parse the email body twice,when I use this code.All the other parameters are fine(from_email,email_subject).
Any ideas?
def get_mail
Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
Net::POP3.start('pop.gmail.com', 995, "uname","pass") do |pop|
mail_header=[];mail_subject=[];mail_body=[];mail_from=[]
unless pop.mails.empty?
pop.each_mail do |mail|
email = TMail::Mail.parse(mail.pop)
mail_subject = email.subject
mail_body = email.body
mail_from = email.from
email_obj=EmailedQueries.new
email_obj.save_email(mail_from, mail_subject, mail_body)
end
end
end
end
No idea ;-)
I've no clue what your real problem is. But have you tried the (new?) mail gem - it's used in Rails3: http://github.com/mikel/mail .
Related
I am using SendGrid to send emails from my application. I want to send sender name along with sender email e.g. from: 'Maxcreet <contact#maxcreet.com>'
It is working fine using Letter Opener and MailTrap but SendGrid only show sender email.
Here is my SendGrid functionto send emails.
def deliver!(mail)
email.from = SendGrid::Email.new(email: mail[:from].addrs.first.address)
email.subject = mail.subject
email.add_personalization(build_personalization(mail))
build_content(mail)
send!(email)
end
I have checked mail[:from] values using puts it gives following values:
puts mail[:from] => Maxcreet <support#maxcreet.com>
puts mail[:from].addrs => Maxcreet <support#maxcreet.com>
puts mail[:from].addrs.first => Maxcreet <support#maxcreet.com>
puts mail[:from].addrs.first.address => support#maxcreet.com
Above 3 seems OK for me but when I use any of them in
email.from = SendGrid::Email.new(email: mail[:from].addrs.first.address)
It does not sent my email and even I do not find my email in sendgrid dashboard.
Following this also tried email.fromname but this even did not work.
SendGrid's ruby API has this option with name name and not fromname.
So the following should solve your problem.
email.from = SendGrid::Email.new(
email: mail[:from].addrs.first.address,
name: mail[:from].addrs.first.name
)
I concluded this by trying it from this doc.
It looks like you're mixing gems or objects. SendGrid::Email.new just needs a from String, and might support a name String. You're extracting the address from your mail[:from] Hash with mail[:from].addrs.first.address, but you need to provide the name as a distinct Key.
In the SendGrid Ruby gem 'kitchen sink" example, they don't show a name on the From argument, but they do on the personalization.
Try: email.from = SendGrid::Email.new(email: 'support#maxcreet.com', name: 'Maxcreet'))
or dynamically: email.from = SendGrid::Email.new(email: mail[:from].addrs.first.address, name: mail[:from].addrs.first.name))
if your mail Object recognizes that Key.
Otherwise, you'll need to look at your mail gem's documentation for how to extract a Friendly/Display Name from that mail[:from].addrs Object.
I only want official email addresses such as xyz#company.com to sign up on my service rather than other generic email addresses such as gmail.com or Yahoo mail.com
Is there a ruby gem to achieve this kind of email validation? If not, how to make this happen?
You could write a custom validation in the appropriate model as shown here: http://www.rails-dev.com/custom-validators-in-ruby-on-rails-4
The basic idea in the article is as follows:
Make your validation method, and put it in a new directory called 'validators'
# app/validators/email_validator.rb
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /\A([^#\s]+)+#yourdomain.com\z/i
record.errors[attribute] << (options[:message] || "wrong email address")
end
end
end
(I have not tested this regex! Please use something like http://rubular.com/ and plug in your own email domain pattern to make sure it's working correctly.)
Then make sure Rails knows to load the new validators directory:
# config/application.rb
config.autoload_paths += %W["#{config.root}/app/validators/"]
Then add the new validation (email) to the appropriate model:
#MyModel.rb
validates :my_email_field, email: true
There is a free MailboxValidator web service that you can perform real-time email address validation in Ruby.
https://github.com/MailboxValidator/mailboxvalidator-ruby
require "mailboxvalidator_ruby"
apikey = "MY_API_KEY"
email = "example#example.com"
mbv = MailboxValidator::MBV.new()
mbv.apikey = apikey
mbv.query_single(email)
if mbv.error != nil
puts "Error: #{mbv.error}"
elsif mbv.result != nil
puts "email_address: #{mbv.result.email_address}"
puts "domain: #{mbv.result.domain}"
puts "is_free: #{mbv.result.is_free}"
puts "is_syntax: #{mbv.result.is_syntax}"
puts "is_domain: #{mbv.result.is_domain}"
puts "is_smtp: #{mbv.result.is_smtp}"
puts "is_verified: #{mbv.result.is_verified}"
puts "is_server_down: #{mbv.result.is_server_down}"
puts "is_greylisted: #{mbv.result.is_greylisted}"
puts "is_disposable: #{mbv.result.is_disposable}"
puts "is_suppressed: #{mbv.result.is_suppressed}"
puts "is_role: #{mbv.result.is_role}"
puts "is_high_risk: #{mbv.result.is_high_risk}"
puts "is_catchall: #{mbv.result.is_catchall}"
puts "mailboxvalidator_score: #{mbv.result.mailboxvalidator_score}"
puts "time_taken: #{mbv.result.time_taken}"
puts "status: #{mbv.result.status}"
puts "credits_available: #{mbv.result.credits_available}"
puts "error_code: #{mbv.result.error_code}"
puts "error_message: #{mbv.result.error_message}"
end
I am working on rails 2 application with sending email functionality. Now, I need to send inline image with the email.
I am using Mailer to send email. I tried lots of time using different ways but not succeed to send image inline in email. Below code i am using to send email.
# Controller
Mailer.delivery_my_opinion_reply(user, my_opinion, answer)
# Model / Mailer.rb
def my_opinion_reply(user, my_opinion, answer)
#subject = "My opinion"
#from = "#{Settings.site_name}"
#recipients = user.email
#content_type = "multipart/alternative"
#attachments.inline['test.jpg'] = File.read(RAILS_ROOT + "/public/system/att_images/728/original/ball1.jpg")
#body = {:question => my_question, :user => user}
end
I got error "undefined method inline for nil class"
try this way
#attachments.inline['image.png'] = File.read("app/assets/images/image.png")
mail(to: email, subject: "subject", content_type: "text/html")
i have code like this
Mailman::Application.run do
to 'email#local.com' do
## how to get subject, from and body in here
end
end
how to get subject, from and body from email in rails?
Use mail gem https://github.com/mikel/mail/
Mailman::Application.run do
default do
mail = Mail.new(message)
from = message.from.first
content = mail.parts[1].body.decoded
subject = message.subject
//your other code
end
end
I'm trying to attach a file to an outgoing email but the attachment size ends up being 1 byte. It doesn't matter what attachment I'm forwarding it always ends up in the email 1 byte in size (corrupt). Everything else looks ok to me.
The email information is pulled from an IMAP account and stored in the database for browsing purposes. Attachments are stored on the file system and it's file name stored as an associated record for the Email.
In the view there's an option to forward the email to another recipient. It worked in Rails 2.3.8 but for Rails 3 I've had to change the attachment part of the method so now it looks like...
def forward_email(email_id, from_address, to_address)
#email = Email.find(email_id)
#recipients = to_address
#from = from_address
#subject = #email.subject
#sent_on = Time.now
#body = #email.body + "\n\n"
#email.attachments.each do |file|
if File.exist?(file.full_path)
attachment :filename => file.file_name, :body => File.read(file.full_path)
else
#body += "ATTACHMENT NOT FOUND: #{file.file_name}\n\n"
end
end
end
I've also tried it with...
attachments[file.file_name] = File.read(file.full_path)
and adding :mime_type and :content_type to no avail.
Any help would be a appreciated.
Thanks!
This is what I tried and worked for me
attachments.each do |file|
attachment :content_type => MIME::Types.type_for(file.path).first.content_type, :body => File.read(file.path)
end
Is the file readable? Can you debug the issue by placing something like this?
logger.debug "File: #{file.full_path.inspect} : #{File.read(file.full_path).inspect[0..100]}"
Is there anything in your development.log?
Well, someone from the rails team answered my question. The problem lies with adding body content (#body) other than the attachment inside the method. If you're going to attach files you have to use a view template.