Using Ruby/rails variables in a mandrill template - ruby-on-rails

Is it possible to use your rails variables on a Mandrill template?
I have an app where a user gets notified via email on certain actions, and right now it's done with action mailer without using Mandrill so it's just a text email with no styling. Obviously, I'd prefer to use a mandrill template I already have to add some dynamic content via variables.. I see a ton of companies using variables in email notifications so i assume it's possible, i just haven't found many useful articles that explain how it's done. If you can point me towards a useful article or just answer the question it'd be really helpful! Right now, I already made the template using Mailchimp, then sent it to mandrill and it's ready for use. My app already has the Mandrill configurations and works properly (i use it for static email that don't include variables). All i really need to do is configure it to allow me to use variables.
Thanks in advance. Happy holidays and war eagle!

One way is to use the merge tags in the Mandrill template, and then either global_merge_vars (all recipients) or merge_vars (per recipients) message options to populate the template.
It is not very exciting approach, but it works fine.
In short, the solution is to put tags like:
*|MYTAG|*
anywhere in the template. Then, the send calls just need to populate the right option. Here for all participants:
mandrill = Mandrill::API.new('my_api_key')
template_name = "my-template-name"
template_content = [] # See doc, not related to the issue at hand.
message = {
to: [{email: 'smith#example.com'}],
headers: {"Reply-To" => "noreply#example.com"},
merge: true,
global_merge_vars: [
{name: 'mytag', content: "Hello, World"}
]
}
mandrill.messages.send_template(template_name, template_content, message)
This should send an email with the tag replaced with the corresponding contents (Hello, World here).

Related

SendGrid API - Cannot use dynamic template data with a legacy template

Trying to follow the sample here https://docs.sendgrid.com/ui/sending-email/how-to-send-an-email-with-dynamic-templates,
I'm sending the following JSON to this url: "https://api.sendgrid.com/v3/mail/send" (and I have sent a simple email before using the same code, now I want to try a template with substitution):
{
"personalizations": [
{
"subject": "Test template email from SendGrid",
"to": [
{
"name": "Neal Walters",
"email": "myRecipient#example.com"
}
],
"dynamic_template_data": {
"Applicant_Name": "John Doe",
"Send_Date": "December 30, 2022"
}
}
],
"from": {
"name": "NealWalters(SendGrid)",
"email": "mySender#example.com"
},
"template_id": "bcab1b12-d922-4cdd-b6f5-9f39b16d9823"
}
and getting this cryptic error:
{"errors":[{"message":"Cannot use dynamic template data with a legacy template
ID","field":"personalizations.0.dynamic_template_data","help":null}]}
I have no idea what is a dynamic nor a legacy template. I copied one of Send Grid's demo templates (Nonprofit Newsletter Email Template) and added some of my own tags.
When I used the template name ("NealDemo Duplicate Nonprofit Newsletter Email Template") instead of the GUID (which I got from the URL when I was editing the template), the message was similar but added this to the above error - which leaves me with the question of can I use the template name or not? It's unclear what a "template_id" is on how to find it, if it is not the name of the template as I saved it.
{"message":"The template_id must be a valid GUID,
you provided 'NealDemo Duplicate Nonprofit Newsletter Email Template'.","field":"template_id","help":"http://sendgrid.
com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.template_id"},{"message":"Unless a valid template_id is
provided, the content parameter is required. There must be at least one defined content block. We typically suggest
both text/plain and text/html blocks are included, but only one block is required.","field":"content","help":"http://s
endgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content"}]}
Disappointed these are not listed on this errors page:
https://docs.sendgrid.com/api-reference/mail-send/errors
Are there two template editors?
I used this one:
https://mc.sendgrid.com/design-library/your-designs/bcab1b12-d922-4cdd-b6f5-9f39b16d9823/editor
I didn't see any other guid/id for the template, so I tried "bcab1b12-d922-4cdd-b6f5-9f39b16d9823" from the URL.
But now I see there is also a dynamic template editor???
https://mc.sendgrid.com/dynamic-templates
As mentioned below, I copied one of your demo templates, which already had some substitution variables in at the bottom ({{Sender_Name}}
{{Sender_Address}}, {{Sender_City}}, {{Sender_State}} {{Sender_Zip}}). I just added two of my own at the top.
I have referenced this: Getting "Cannot use dynamic template data with a legacy template" with non-legacy template
The saga continues. I went to this page: https://mc.sendgrid.com/dynamic-templates and was able to copy over my previous template, so perhaps it was a legacy template. It now gave it a clearly labeled template-id of: d-a35d9d48ce304588bbebf7828811b473 (which looks like a "d-" followed by a guid).
I ran that through the API call, which resulted in no immediate errors, but then I got this email:
Usually when I can't figure things out, it is either the documentation is lacking, or I was speed-reading it.
When I first setup my account, I apparently clicked a link that took me to SendGrid's main site, and I signed up for an account there. Apparently this step was totally unnecessary (also their site requires a 2FA, separate from Azure). So I built my templates on their site, which was under a different account then the one I created in Azure. Thus I was able to send emails with the token from Azure, but any time I tried to send a template from my second account, of course, it was not recognized with my Azure account.
You can create templates inside of Azure, from the links shown below:
Then this:
NOTE 1 - their "pretty" designs are still under the Legacy Templates, and "Dynamic Templates" seem to be the newer preferred way to go. You can build a "legacy template", then apparently copy it's HTML over to a "dynamic template".
NOTE 2 - I only solved this by opening a support ticket with SendGrid.

Searching for multiple labels Gmail API - Rails

I am using this gem in my rails app to interact with the Gmail API: https://github.com/gmailgem/gmail
I am able to search for emails containing one label:
require 'gmail'
gmail = Gmail.connect("email#gmail.com", "testpwd")
gmail.mailbox('Urgent')
But when I try to search for multiple labels, I get an error. How do I find all email that contain two specific labels, such as email that contain both the label "Urgent" and "Priority"?
You could try using intersection...
urgent_priority_emails = gmail.mailbox('Urgent').emails & gmail.mailbox('Priority').emails
However, I have a recollection that this may not work, because the emails are treated as different objects even though they are the same messages.
An alternative that may work...
urgent_email_message_ids = gmail.mailbox('Urgent').emails.map{|email|email.message_id}
urgent_priority_emails = gmail.mailbox('Priority').emails.select{|email| urgent_email_message_ids.include?(email.message_id)}
The gmail gem let's you use the Google search filter, like this:
gmail.mailbox('Urgent').emails(gm: 'label:'Priority')

Saving Rails model as JSON

This seems like a question that would have already been asked, but I couldn't find quite what I was looking for, so I'll just go ahead and ask.
I'm trying to use angular.js to make an instant search function on my site. I want users to be able to search through Posts (by title and content, ideally) instantaneously, so after hearing about angular's ability to do this, I gave it a shot.
I have this going on in my posts.js.coffee file:
#PostListCtrl = ($scope, $http) ->
$http.get("posts.json").success (data) ->
$scope.posts = data
And this going on in the JSON doc it references (just to make sure it was working -- which it is).
data =
[
name: "Blog ex"
content: "This is my example post."
,
name: "Test posting"
content: "A different ex post"
,
name: "Test3"
content: "This has some unusual, unique vocabulary."
]
Now all I have left to do is get Rails to save an object (with name/content attributes) in the above JSON file each time a new Post is created, so that the search actually runs through meaningful data. I'm new to Rails/JSON/computer stuff and don't have a clue how to do this. I'm guessing it's in the posts controller, maybe in one of those respond_to blocks, but if anyone could point me to the right way to do this, I'd appreciate it.
If all you want is an instant search, or autocomplete functionality, you should not use Angular.js or any other JS MVC frameworks. Instead, consider using jQuery as #charliefl suggested, it's easy. A simple Ajax call will do it.
JS MVC frameworks are heavy, and you need to design the architecture from the bottom to suit them. Not worthy for such a single functionality.
To make this function work in jQuery, basically:
Listen the event on search box, say typing one or more characters
Catch the characters and use jQuery Ajax to send a POST request to a method in PostsController in Rails, say "search"
Make this method respond to JSON.
Update DOM according to server response.

Rails faye realtime notification system

Im trying to build a simple faye realtime based notification system so I can execute a certain javascript on certain actions.The idea is relatively simple though Im having problems implementing it, not sure wich road to take on this after reading the documentation of faye.
Current thoughts are
One unique faye channel per logged-in user, so you can push a action (popup, set text) to a certain user only
One div in my app layout that I can write text to
One div in my app that holds layout for a popup
Now I've seen the railscast tutorial about faye but ryan works from a controller/action method create. I don't want to insert stuff in the db, just call the JS function from anywhere ( building an application helper would be a good idea I think ), I would just want to do something like "execute javascript 'set_text'" and execute javascript 'show_popup'
What would be the best way to build such functionality with Faye, Basically I only have to
Execute a javascript function on a certain Faye channel
To accomplish the popup and text message.
A bit lost on this anyone can point me in the right direction or maybe have build such functionality already? thx in advanche!
On the server side, you can just do (this needs eventmachine):
client = Faye::Client.new('http://localhost:9292/faye')
client.publish('/notifications/1', 'alert(1);')
Or via HTTP:
message = {:channel => '/notifications/1', :data => 'alert(1);'}
uri = URI.parse("http://localhost:9292/faye")
Net::HTTP.post_form(uri, :message => message.to_json)
Then on the client side, you can then do anything with the data.
var client = new Faye.Client('http://localhost:9292/faye');
client.subscribe('/notifications/1', function(data) {
eval(data);
});
Just replace alert(1); with whatever JS you want to execute.

Rails 3 ActionMailer attachments incorrectly shown inline by Yahoo! Mail

I am using ri_cal gem to generate ics file.
In my mailer:--
def appointment_book(appointment,recipient,appointment_with)
event = RiCal.Event do
description "MA-6 First US Manned Spaceflight"
dtstart DateTime.parse(appointment.slot.date_on.strftime("%m/%d/%Y").to_s + " " + appointment.slot.start_time.strftime("%I.%M %p").to_s)
dtend DateTime.parse(appointment.slot.date_on.strftime("%m/%d/%Y").to_s + " " + appointment.slot.end_time.strftime("%I.%M %p").to_s)
add_attendee appointment_with.full_name
alarm do
description "Segment 51"
end
end
attachments['event.ics'] = {
:mime_type => 'text/calendar',
:content => event.export
}
#recipient = recipient
#appointment = appointment
mail(:to => recipient.email, :subject => "Appointment book")
end
in appintment_book.text.erb file
(some simple variable values or plain text.)
Issue is (In Yahoo, specifically newer version. I am adding screenshots in edit part):--
attachment is there is mail, but apart from gmail content of the mail is having some vcard and other content. If i remove ics_generation code then its gone. Please check screenshots
In gmail it looks like (Also working on hotmail):--
EDIT
Working in yahoo Classic.
Not working in New yahoo:--
I'm a bit unsure what you're showing and asking. Assuming the first screenshot is an unnamed mail client incorrectly showing the icalendar data instead of having it as an attachment and you're wanting to fix that, then I'd suggest you specify the mime type, as in
attachments['event.ics'] = {
:mime_type => 'text/calendar',
:content => event.export
}
I can't say if this will help that mail client and/or calendar data, but it does the trick for vCard attachments in most clients, as in:
attachments['card.vcs'] = {
:mime_type => 'text/x-vcard',
:content => person.to_vcf
}
Thanks for the clarification, unfortunately for that question I've no answer better than "tell Yahoo," which is no help.
I sent attachments to Yahoo mail from a few different sources and I tried different content types, transfer encodings (e.g., Base64), different kinds of attachments, etc. The only constant I can find between those that Yahoo shows properly and those it fails on is that the successes do not use parameter folding, e.g.,
Success
Content-Disposition: attachment; filename="event.ics"
Fail
Content-Disposition: attachment;
filename="event.ics"
This (correct) folding appears to be hardwired into the mail gem that Rails uses and I didn't try another way of truly isolating it; I have to say I'd be surprised if that were really the problem.
Regardless, Yahoo is rendering non-inline attachments as inline, so if this is a significant problem for your customer base, I'd take it up with Yahoo directly; maybe they'll fix it or offer you a more concrete statement about what triggers it so you can work around it. For what it's worth, Yahoo does allow the user to access the attachment correctly as it should with Content-Disposition of "attachment"; it's just also showing it as if the Disposition where "inline."
By the way, this problem affects my apps as well, so I'll keep thinking about things for us to try.
This issue in only related to Yahoo! Mail. What a shame that Yahoo! Mail does not manage iCalendar events in Yahoo! Calendar yet, in 2011!
At least events are now provided as ICS attachments in "new" Yahoo! Mail and you can manage invitations manually. Maybe Yahoo! Classic did in the past. I will check and consider to revert possibly.
This is the only solution for people keeping on using Yahoo! Webmail service. Of course having a Yahoo! Mail account managed by a POP3 email client does manage iCalendars events properly: any attached ICS file shows up as a meeting invitation you can manage though your email client as usual.
Have a look here, at Yahoo! Developer Network : http://developer.yahoo.com/forum/General-Discussion-at-YDN/Problem-opening-ICS-files-from-Yahoo/1299668671000-24ed7a59-019f-3f1b-b596-70ab34db783c
Hope it helps,so late.

Resources