Meteor : Email a Template in Client using Mailgun - email

I have a Template in client
<template name="sendThis">
<img src="logo.png"><br>
<h3>Welcome to Meteor NewBie</h3>
Dear {{name}},
<p>You received this Email because you have subscribed to http://www.example.com</p>
</template>
I would like to send this Template(sendThis) as HTML body in my Email to subscribers.
I am using Mailgun as my Email Client. What are the steps I should take to make this happen as a subscriber clicks a button with an id "subscribe".
PS: I have multiple helpers in this template, multiple in the sense more than 20.
Thanks in advance.
Mahesh B.

One way to solve this is to use Blaze.toHTMLWithData to render your template (with a context) to an HTML string. You can then call a method on your server which emails the user with the appropriate subject and address. Here's an example:
client
var sendSignupEmail = function() {
// This assumes this first email address is the one you want.
// In some cases you may want the first verified email, but not
// during signup.
var address = Meteor.user().emails[0].address;
var subject = 'Thanks for signing up!';
// Here I used username - replace this with the appropriate data
// like Meteor.user().profile.firstName or something.
var body = Blaze.toHTMLWithData(Template.sendThis, {
name: Meteor.user().username
});
Meteor.call('sendEmail', address, subject, body);
};
server
Meteor.methods({
sendEmail: function(to, subject, html) {
check([to, subject, html], [String]);
this.unblock();
return Email.send({
to: to,
from: 'something#example.com',
subject: subject,
html: html
});
}
});
Also make sure your MAIL_URL environment variable has been defined.

Related

Google Form mail notification script. Emails always have "me" as sender

I have tried to use the search function, but did not find a solution for my problem, or I do not understand enough yet.
I have written a script for google forms, that sends an automatic email to two email addresses, when an user submits the form. I have build in some information that should show in the email subject and puts the input from the forms into the email, with some simple HTML formatting.
My problem is, that the emails always have my Email address as the sender (the account I used for creating the form)
I would like to have the senders email, the one that is submitting the form.
How is that possible?
Here is the code:
function sendFormByEmail(e)
{
// Sent to Email address
var email1 = "123#abc.com";
var email2 = "345#abc.com";
// Variables
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var txt = "";
var subject = "Example text: ";
// Var e = form input as array.
// Loop through the array.
for(var i in headers){
txt += "<b>" + headers[i] + '</b>' + ': '+
e.namedValues[headers[i]].toString() + '<br><br>';
}
// Insert variables from the spreadsheet into the subject.
// Create email subject
subject += "Example text " + e.namedValues[headers[2]].toString();
// Send the email
MailApp.sendEmail(email1, subject, "", {htmlBody:txt});
MailApp.sendEmail(email2, subject, "", {htmlBody:txt});
}
It is not possible to send the mail as the user who submitted the form as the script is running from the user account and the mailapp will send the mail from that account only. you can change the display name according to the user name who submiited the form by the parameter name. Also you can change the email to noreply#domainName.com by adding the parameter noReply in mailApp syntax. However you cannot change it to the user who submitted the form
You can refer this documentation for the above parameters : https://developers.google.com/apps-script/reference/mail/mail-app
Hope this could help

GmailApp.sendEmail() doesn't parse plain text when sending HTML mail. How can I make it to show plain text?

I have a document with email body content. I am using Apps Script to fetch the document from URL and send it as an HTML mail.
var url = "address of the document";
var param = {method : "get",
headers : {"Authorization": "Bearer " +ScriptApp.getOAuthToken()}, muteHttpExceptions:true,
};
var html = UrlFetchApp.fetch(url,param).getContentText();
MailApp.sendEmail(
emailId, // recipient
tempSubject, // subject
body,
{ // body
htmlBody: html, // advanced options
});
However, if my document has text like this for example:
Hi
username: rml#gmail.com
password : <aSrD9nn
Happy browsing!
In the resultant email, the text after password: is blank. It does not show in the mail body of the email sent. Apparently, it considers < as an opening tag, I think.
Is there a way to solve this? I tried with other text and it works fine.
How do I overcome this problem? Any help will be much appreciated.

Dynamic URL in Meteor Email Template

I'm a beginner in Meteor and i would like to send an invitation link to a dynamic generated page in my app with iron:router.
Meteor.methods({
'sendEmail': function(to) {
this.unblock();
SSR.compileTemplate( 'emailText', Assets.getText( 'html-email.html' ) );
Template.emailText.helpers({
link: function () {
return Router.current().route.path(this);;
}
});
Email.send({
to:to,
from: 'no-reply#whatever.xyz',
subject:'xyz wants to invite you ',
html: SSR.render('emailText')
});
}})
}
The Problem is that i dont get the url of the site in my html-email.html. There i got
Link to invitation
What am i doing wrong?
Your method is a server method (SSR). Router.current() is a client method and cannot return anything server side. The solution is to pass the url as a parameter. Call your method that way :
Meteor.call( 'sendEmail', email, url, ... )
Then your method will be :
'sendEmail': function( to, url ) {...
Looking at your code I would say you mix up server side methods and client side template helpers.
What do you want to achieve? Want the current route where the user is located in your site to be included in the mail? Then, send the url as an extra parameter in your sendEmail method like fabien suggested.
If the link is a static path that you use as a link to some landing page, I suggest to get that path from settings.json.
Either way, your problem is rendering an email with some link in it. You can pass an object as second parameter to SSR.render.
Here is how you could solve this:
Meteor.methods({
'sendEmail': function(to) {
this.unblock();
var linkInMail = 'htpp://some_url/etc?etc'; // Fill this value, see my remark above
var templateName = 'emailText';
SSR.compileTemplate(templateName, Assets.getText( 'html-email.html' ) );
var renderedContent = SSR.render(templateName, {link: linkInMail}); // This your missing part
Email.send({
to:to,
from: 'no-reply#whatever.xyz',
subject:'xyz wants to invite you ',
html: renderedContent
});
}})
}

how to update an subscription without id in mail chimp rest api

I really like the new Mail Chimp REST API - it is easy to create subscriptions by PUT and those can be updated using the subscription id.
But I would like to update a subscription simply using the email address, because I do not want to save any new Mail Chimp Id in my Middle-ware application, as long as the email should be sufficient as identifier?
To update a List Member the API is:
/lists/{list_id}/members/{id}
but I would prefer a simpler way:
/lists/{list_id}/members/{email}
is something like this possible?
The subscriber's ID is the MD5 hash of their email address. Since you would have to make a function call to URL Encode the email address for your second way, using the first way is just as easy.
See this help document on managing subscribers for more details.
More specifics on updating a subscriber via MailChimp's REST API.
// node/javascript specific, but pretty basic PUT request to MailChimp API endpoint
// dependencies (npm)
var request = require('request'),
url = require('url'),
crypto = require('crypto');
// variables
var datacenter = "yourMailChimpDatacenter", // something like 'us11' (after '-' in api key)
listId = "yourMailChimpListId",
email = "subscriberEmailAddress",
apiKey = "yourMailChimpApiKey";
// mailchimp options
var options = {
url: url.parse('https://'+datacenter+'.api.mailchimp.com/3.0/lists/'+listId+'/members/'+crypto.createHash('md5').update(email).digest('hex')),
headers: {
'Authorization': 'authId '+apiKey // any string works for auth id
},
json: true,
body: {
email_address: email,
status_if_new: 'pending', // pending if new subscriber -> sends 'confirm your subscription' email
status: 'subscribed',
merge_fields: {
FNAME: "subscriberFirstName",
LNAME: "subscriberLastName"
},
interests: {
MailChimpListGroupId: true // if you're using groups within your list
}
}
};
// perform update
request.put(options, function(err, response, body) {
if (err) {
// handle error
} else {
console.log('subscriber added to mailchimp list');
}
});

Send mail for multiple user and the each recipents have only his email address not the others

I am new bie for send grid. I have checked this url for two emails "https://sendgrid.com/api/mail.send.js..." and got the mail successfully in both emails.
The mail received by the users from the above URL have both email address in "TO" field like
For ex. User Test To: test#example.com;test2#example.com. and For User test2 To: test#example.com;test2#example.com.
As per my requirement i want to send mail for multiple user and the each recipents have only his email address not the others.
For ex. User Test To: test#example.com and For User test2 To: test2#example.com.
Can this scenario is possible with send grid.
Thanks
You can send the same email to multiple recipients by using the SendGrid SMTP API's to parameter.
To do this you'll set an X-SMTPAPI: header in your message, this header will include the JSON to communicate with SendGrid's SMTP API. The header will look as such:
X-SMTPAPI: { "to": [ "test#example.com", "test2#example.com" ] }
Each recipient will receive a unique email, addressed only to them.
Be aware: you'll still need to set a To: header for the message to send, however you can set this to yourself or one of the recipients (and then exclude them from the JSON list).
Send grid being a standard SMTP server will respond as if you are sending email from gmail, yahoo or outlook.
The scenario is not possible that I am aware of. I have only incorporated it into 2 applications, so I am certain there are better experts.
As an alternative you could test using the blind copy, The problem with that is would need a main address in the To field, which may not fill your requirement.
Send email on Azure Mobile Service /NodeJS
var sendgrid = new SendGrid('KEY');
sendgrid.send({
to: toEMail, // ["email1#mail.com", "email2#mail.com",...]
from: fromEMail,
subject: subject,
text: body
}, function (success, message) {
console.log("send mail: " + subject);
// If the email failed to send, log it as an error so we can investigate
if (!success) {
console.error(message);
}
});
possible on Sendgrid. You can use the normal bcc on sendgrid via personalization, but we dont like it because the To: is always required. So my solution is sendgrid transactional email. This will send 1 email to multiple users (using sendgrid / php / laravel ):
$email = new \SendGrid\Mail\Mail();
$email->setFrom("sender#mail.com", "Sender Name");
$email->setSubject("Your subject");
$email->addTo(
"email.1#mail.com",
"User One",
[],
0
);
$email->addTo(
"email.2#mail.com",
"User Two",
[],
1
);
$email->addTo(
"email.3#mail.com",
"User Three",
[],
2
);
$email->addContent("text/plain", "your body");
$email->addContent("text/html", "<strong>your body</body>");
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
$response = $sendgrid->send($email);
return response()->json($response, 200);
} catch (Exception $e) {
return response()->json($e->getMessage(), 400);
}