Sending formatted/html Outlook email with Ruby Win32ole - ruby-on-rails

Is it possible to send formatted email, whether html or otherwise with Ruby's WIN32OLE - I have been able to send a plain text email through automation (as follows), but was hoping to be able to add some formatting. Any help would be greatly appreciated!
require 'win32ole'
#outlook = WIN32OLE.new('Outlook.Application')
email = #outlook.CreateItem(0)
email.Subject = 'Test Subject'
email.Body = 'Test Body'
email.To = 'recipient#example.com'
email.Save
email.Send

Pretty sure you need to use HTMLBody instead of Body as in:
email.HTMLBody = '<h3>Test body in HTML format</h3>'

Related

Rails mailer and sendgrid with heroku

I am working on adding email functionality to an app I am hosting on heroku.
Here are the relavant files:
app/mailers/user_actions_mailer.rb
def add_user_action(action_params)
#action_params = action_params
from = Email.new(email: 'marklocklear#blah.org')
subject = 'You have been notified'
to = Email.new(email: 'my_user#gmail.com')
content = Content.new(type: 'text/html', value: "test")
mail = Mail.new(from, subject, to, content)
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
response = sg.client.mail._('send').post(request_body: mail.to_json)
end
This is working fine and I am able to send emails with it. However, my issue is with the 'content' variable above. I want the content to use rails standard mailer template in app/views/user_actions_mailer/add_user_action.html.erb.
I have this file/folder created, but I'm not sure how to point content to this location. I'm tryed not passing content to Mail.new fuction hoping this might trigger rails default mailer wiring, that doesn't seem to work.
I was not able to come up with a solution for this, using the sendgrid-ruby gem which my example above uses.
I ended up refactoring using THIS PAGE from sendgrids ruby-on-rails specific documentation.

How to parse email message as html format when there is no attachment

I am new in using IMAP. Now I am trying to use IMAP to see my emails in my web page from mail server. I have configured the settings. And I think the configuration is ok. Now I want to show the message body as html format. My code is:
// getting email body text
if($attachments[1]['is_attachment']!=""){ // if attachmentavailable.
$part_number = '1.2';
}
else{
$part_number = '1';
}
$message = quoted_printable_decode(imap_fetchbody($inbox, $email_number, $part_number));
When email has attachment, it is showing as html format. But when there is no attachment the message is showing as plain text. There is no bold or any other formatted font if I give and there is no link also.
If I use part number 1.2 in else part no message is shown.
Now how can I print email message as html format where there is no attachment.
Learn about the BODYSTRUCTURE format. E-mails can come up in about a ton of different structures, and unless your application actually takes a look at the structure of the MIME message, your code is blindly guessing what to do.

Rails not sending (ical) attachments in emails

I'm trying to send an email with an ical attachment, I'm running rails v.3.2.13 and using the icalendar gem (to generate the ical string) see. (In development mode in case that might be a problem).
The relevant mailer code looks like this:
def mailme
ical = Icalendar::Calendar.new
...
attachments["meetings.ics"] = { mime_type: "text/calendar", content: ical.to_ical }
mail(from: email, to: recipient, ...)
end
there is also template file with the same name (mailme.html.erb)
The problem is the mail (html) is send without the attachment.
As usual any help would be much appreciated.
Thank you.
I've gotten them working with something like below:
mail.attachments['meeting.ics'] = { mime_type: 'application/ics',
content: ical.to_ical }
mail(from: email, to: recipient, ...)
So it's possible you need to call it on #attachments on the mail object instead of calling it on the current context. I'm not sure if your mime type needs to be application/ics, but that's worked fine for me in my systems.
In case someone else stumbles upon this.
If you are using delayed_job check out https://github.com/collectiveidea/delayed_job/wiki/Common-problems#wiki-Sending_emails_with_attachments
To fix this, remember to add this line to your mailer:
content_type "multipart/mixed"

Ruby modifying body of a Mail Interceptor using register_interceptor and delivering_email

I've got a working intercept up and running in my Ruby on Rails application. Currently I can modify the subject just fine using the technique below, but for some reason I can not modify the Body.
Inside of my /config/initializers I register the interceptor like so:
ActionMailer::Base.register_interceptor(MailIntercept)
Inside of /lib/ i have a file called mail_intercept like so:
class MailIntercept
def self.delivering_email(message)
message.subject = "Modified Subject"
message.body = "Modified Body"
if message.to.split("#").last == "olddomain.com"
message.subject = "Domain is olddomain.com, safe to display message"
message.body = message.body + "Domain is olddomain.com, safe to display message"
else
message.subject = "Domain is NOT olddomain.com, hide message"
message.body = "Domain is NOT olddomain.com, hide message."
end
end
end
Problem #1: The body doesn't get modified, ever.
Problem #2: The application that is sending emails has multiple email addresses being sent as BCC. If the email is to olddomain.com, I want to display the message body. Otherwise, I want to strip the message body and just display a message.
I'm pretty sure that this delivering_email only gets called once when the email is sent, and not multiple times for each of the BCC's. Even if I did a loop through the TO, CC, and BCC and then modified the body it would modify the body for everyone. Is there a better way to go about this?
I checked the code, it is working fine for me I am using Rails 3.2.6
I guess the problem may be at the action mailer method invoking place

find email and replace with a sentence in PHP

How do I prevent people from entering their email address in the description field by replacing their email to some wordings, for example if user entered the following text:
Please contact me via joe.joey#email.com.
I want the output to be:
Please contact me via <email address is blocked>.
I know of a basic str_replace but the output would simply be:
//output is Please contact me via joe.joey <email address is blocked> email.com
$string = 'Please contact me via joe.joey#email.com.';
$lookfor = '#';
$replacewith = '<email address is blocked>';
$newstring = str_replace($lookfor, $replacewith, $string);
thanks.
This is a perfect time to use preg_replace. I've slightly simplified the requirements for a valid email here (emails can be horridly complex), but something like:
$newstring = preg_replace("/[\w-]+#([\w-]+\.)+[\w-]+/", $replacewith, $string);

Resources