How to send attachments using mailgun? - forms

In my form there is section to upload resume I use mailgun API for my mails to send, I am finding trouble for sending attachments using the form.
How to send the attached file along with other details of my form when user submits.
Please see the below image of my form the attachments should be dynamic but not static.

Example with Node.js
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
var filename = 'mailgun_logo.png';
var filepath = path.join(__dirname, filename);
var file = fs.readFileSync(filepath);
var attch = new mailgun.Attachment({data: file, filename: filename});
var data = {
from: 'Excited User <me#samples.mailgun.org>',
to: 'serobnic#mail.ru',
subject: 'Hello',
text: 'Testing some Mailgun awesomness!',
attachment: attch
};
mailgun.messages().send(data, function (error, body) {
console.log(body);
});

Related

Send email to the new google form submission only

I'm new to the Google apps script. I wrote a script to send emails when there is a new submission from google forms using data and template from a spreadsheet. However, it sends an email to not just the new submission but also to all of the previous submissions. The whole script is quite long, so I only copy a short part of it. Is there any way to fix it?
Here is the link to the spreadsheet https://docs.google.com/spreadsheets/d/1fhuwEndIS3khg3W19jpQnBAaCp_MMrD_bfATrdf2-4I/edit?usp=sharing
Thank you.
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Calculation");
var lr = ss.getLastRow();
for (var i = 3; i<=lr;i++){
var currentEmail = ss.getRange(i, 1).getValue();
var currentName = ss.getRange(i, 3).getValue();
var currentScore1 = ss.getRange(i, 4).getValue();
MailApp.sendEmail(
currentEmail,
subjectline,
"HTML",
{ htmlBody: messageBody }
);
}
}
Instead of reading the values from the spreadsheet take advantage of the form submit event object. This event object has two properties including the form submission values, one is an Array of form submission values in the same order than the sheet columns, the other is an object having a property for each question each of them having an Array of values. Ref. https://developers.google.com/apps-script/guides/triggers/events
This shows the changes that need to done to your script:
function sendEmail(e) {
var currentEmail = e.values[0];
var currentName = e.values[2];
var currentScore1 = e.values[3];
MailApp.sendEmail(
currentEmail,
subjectline,
"HTML",
{ htmlBody: messageBody }
);
}
Note: In order to make the event object work, the function should be called by a Google Sheets form submit trigger.
Related
Event values and offset in Google spreadsheet
How to send an email only to the last Google form submission?

App Scripts: Attempting to grab a specific form response, get a URL, and email that URL

I'm attempting to capture a directly link or the response ID to create a link and forward it into a ticketing queue but every function seems to give me the wrong ID.
function onFormSubmit()
{
//email content creation
var formID = '(copy/pasted from form url)';
var emailBody = "You have received a form submission: ";
var emailAddress = 'TestEmail';
var emailSubject = 'Your form has a new response';
//find the form
var form = FormApp.openById(formID)
var formResponses = form.getResponses();
//find the last response
var lastResponse = formResponses[formResponses.length-1];
var responseID = lastResponse.getID();
//build link
var url = https://docs.google.com/forms/d/ + formID + "/edit#response=" + responseID;
//send email
MailApp.sendEmail(emailAddress, emailSubject, emailBody + url);
}
Hopefully this comes off as coherent on what I'm trying to do. I've attempted to use the
onFormSubmit(e)
responseID = e.getResponses[e.length-1].getID();
but that doesn't work either.
I've found numerous posts on how to do this, but even copy/pasting the test code throws errors of getID being undefined.
If anyone can help figure this out, I would appreciate it!

Google Sheets App Script- Add signature when sending e-mail

I'm working on a script that will save my Google Sheet spreadsheet as a pdf and email it as an attachment. The code I'm using works, but I don't know how to add a signature to the email I'm sending. Is this possible, and if so; what would be the cleanest way to do it? Here's the code I'm working with. I know, it's not pretty.
function displayPrompt() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getActiveSheet().getName();
var ui = SpreadsheetApp.getUi();
var result = ui.prompt("Please enter email body");
//Get the button that the user pressed.
var button = result.getSelectedButton();
var message = {
to: "someone#somewhere.com",
subject: sheetName,
body: "Hi team,\n\nPlease find attached the quote that you requested.\n" +
result.getResponseText() + "\nThank you,\nMe",
name: "My Name",
attachments:
[SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("Quote")]
}
if (button === ui.Button.OK) {
Logger.log("The user clicked the [OK] button.");
Logger.log(result.getResponseText());
MailApp.sendEmail(message);
} else if (button === ui.Button.CLOSE) {
Logger.log("The user clicked the [Cancel] button.");
}
}
I believe your goal as follows.
You want to add the signature to the message of Gmail using MailApp.sendEmail
In this case, I think that there are 2 patterns.
Pattern 1:
In this pattern, by declaring the signature as the variable, the signature is added.
Sample script:
Please modify message in your script as follows.
var signature = "\nsample signature"; // Added
var message = {
to: "someone#somewhere.com",
subject: sheetName,
body: "Hi team,\n\nPlease find attached the quote that you requested.\n" + result.getResponseText() + "\nThank you,\nMe" + signature, // Modified
name: "My Name",
attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("Quote")]
}
Pattern 2:
In this pattern, by retrieving the sigunature of Gmail, the signature is added.
Sample script:
In this sample, Gmail API is used. So, before you use this script, please enable Gmail API at Advanced Google services. And, please modify message in your script as follows.
var signature = "<br>" + Gmail.Users.Settings.SendAs.list("me").sendAs[0].signature; // Added
var message = {
to: "someone#somewhere.com",
subject: sheetName,
htmlBody: ("Hi team,\n\nPlease find attached the quote that you requested.\n" + result.getResponseText() + "\nThank you,\nMe").replace(/\n/g, "<br>") + signature, // Added
name: "My Name",
attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("Quote")]
}
The signature retrieved from Gmail is HTML. So in this pattern, htmlBody is used instead of body.
References:
Method: users.settings.sendAs.list

NetSuite SuiteScript 2.0 - Attach File Object to Email Being Sent Via SuiteScript

I am writing a script where I am trying to print a transaction and then email it. I keep getting the following error.
"Wrong parameter type: options.attachments is expected as file.File[]."
I can't tell what is wrong with my code. Here is the snipped of code that is causing the issues. The email sends just fine if I don't include the attachments parameter. Any help would be greatly appreciated.
var recID = 1213;
var userID = 1478;
var emailSender = 1478;
var emailSubject = "Test Email";
var emailBody = "This is a test.";
var mainRecipient = "test#test.com";
var additionalRecipients = "";
var recPDF = render.transaction({
entityId: recID,
printMode: render.PrintMode.PDF,
});
email.send({
author: author,
recipients: mainRecipient,
subject: emailSubject,
cc: additionalRecipients,
body: emailBody,
attachments: [recPDF],
relatedRecords: {
transactionId: recID
}
});
Figured out my issue. There was a conflict attachment for some reason with the library file I had attached to the Suitelet running the script. By removing the library file, it resolved the issue.

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.