sending Email with Attachment xpages - xpages

this is my sample coding for sending email with attachment. The content of word is not send correctly.
attachment is using "file upload"
// write mail
var setdoc:NotesDocument = database.getProfileDocument("System Setting", "");
var server = setdoc.getItemValueString("MailDBSvr");
var dname = setdoc.getItemValueString("MailDbPath");
var web = setdoc.getItemValueString("InternetAddress");
var maildoc:NotesDocument = database.createDocument();
maildoc.replaceItemValue("Form", "Memo");
maildoc.replaceItemValue("Subject","Test Send Mail");
session.setConvertMime(false);
var stream = session.createStream();
stream.writeText("<html><body>");
stream.writeText("<p>Dear " + "[person]" + ",</p>");
stream.writeText("<p>Attached item is an image of </p>");
stream.writeText("<p> ***THIS IS AN AUTOMATED MESSAGE - PLEASE DO NOT REPLY DIRECTLY TO THIS EMAIL***</p>");
stream.writeText("</body></html>");
var body = maildoc.createMIMEEntity("Body");
var Att= document1.getDocument(true).getFirstItem("Attachment");
maildoc.copyItem(Att,"Body") // try adding an item
body.setContentFromText(stream, "text/html;charset=UTF-8", 1725);
stream.close();
maildoc.closeMIMEEntities(true);
session.setConvertMime(true);
maildoc.replaceItemValue("SendTo","TestUser1#devsvr1.pcs.com.my");
maildoc.send();
The result come out is only the attachment field without any text value inside. I not sure which part of it is wrong.
sample Result screen:
Recommended Mime style
var HTMLMail = function() {
...
}
var mail = new HTMLMail();
mail.setTo("TestUser1#devsvr1.pcs.com.my")
//mail.addFileAttachment(result);
mail.setSubject("Test Send Mail");
mail.addHTML("<h1>Hi!</h1>");
mail.addHTML("<table><tbody><tr><td>contents in a table here</td></tr></tbody></table>");
mail.send();

IBM Notes/Domino has 2 ways to show 'pretty words and pictures'
RichText
MIME
You should only use one or the other, but here you are actually mixing both the different types.
Above, when you copy the Attachment Item, you are actually adding the first 'Body' item, you can see it's type 'RichText'.
Then we you createMimeEntity you are creating the second 'Body' item, and it's type is 'MimePart' (it is probably showing second because the Mime is not applied until CloseMimeEntities)
So now you have 2 Body items with different parts. You are seeing the 'RichText' attachment in Notes because it is first listed item.
What you actually need to do is create the correct multipart mime structure.
If you want a bit more information about mime, I a blog post on my site which explains it a little bit more, including some info about the correct mime structure.
http://camerongregor.com/2016/04/21/webmail-ui-you-must-learn-about-mime/
If you haven't seen it yet there is an XSnippet by Mark Leusink which has a demo of creating a mime email using SSJS. I don't use this myself as I don't use SSJS but it might be useful to you as it should handle most of this mime manipulation for you.
https://openntf.org/XSnippets.nsf/snippet.xsp?id=create-html-mails-in-ssjs-using-mime

My coding with reference using rich-text style :
var setdoc:NotesDocument = database.getProfileDocument("System Setting", "");
var server = setdoc.getItemValueString("MailDBSvr");
var dname = setdoc.getItemValueString("MailDbPath");
var web = setdoc.getItemValueString("InternetAddress");
var maildoc:NotesDocument = database.createDocument();
maildoc.replaceItemValue("Form", "Memo");
maildoc.replaceItemValue("Subject","Test Send Mail");
session.setConvertMime(false);
var stream = session.createStream();
stream.writeText("<html><body>");
stream.writeText("<p>Dear <b>" + "person" + "</b>,</p>");
stream.writeText("<p> ***THIS IS AN AUTOMATED MESSAGE - PLEASE DO NOT REPLY DIRECTLY TO THIS EMAIL***</p>");
stream.writeText("</body></html>");
var tmpDoc:NotesDocument = maildoc.getParentDatabase().createDocument();
var mime:NotesMIMEEntity = tmpDoc.createMIMEEntity("myBody");
var addRt:NotesMIMEEntity = maildoc.getMIMEEntity("addBody");
var Att:NotesRichTextItem = document1.getDocument(true).getFirstItem("Attachment");
if(addRt != null && #Length(addRt.getContentAsText().trim()) > 28) {
stream.writeText('<font size="2" face="sans-serif">'); // Enforce "simiilar" font type/size...
stream.writeText(addRt.getContentAsText());
stream.writeText('</font>');
}
mime.setContentFromText(stream, "text/html", NotesMIMEEntity.ENC_NONE);
var prevMime = session.isConvertMime();
session.setConvertMime(true);
tmpDoc.closeMIMEEntities(true,"myBody");
var rt:NotesRichTextItem = maildoc.getFirstItem("Body");
var body = null;
if (rt != null) {
body = rt.copyItemToDocument(tmpDoc,"Body");
rt.remove();
}
rt = maildoc.createRichTextItem("Body");
var rtMime:NotesRichTextItem = tmpDoc.getFirstItem("myBody");
rt.appendRTItem(rtMime);
if(Att != null) {
if(addRt == null) rt.addNewLine(1);
rt.appendRTItem(Att);
Att.remove();
}
if(body != null) {
rt.addNewLine(2);
rt.appendRTItem(body);
}
if(addRt != null) {
addRt.remove();
addRt.recycle();
}
stream.close();
maildoc.closeMIMEEntities(true);
session.setConvertMime(true);
maildoc.replaceItemValue("SendTo","TestUser1#devsvr1.pcs.com.my");
maildoc.send();

Related

How can I send image attachment to email

As I have created list of qr codes to scan and then I want to send this images to email by using Google app script.
current result
image is not sent to gmail.
codes
function sendEmail(sheetName = "emails") {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).activate();
var spreedsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = spreedsheet.getLastRow();
//var ssa = SpreadSheetApp().getSheetByName(sheetName); // spread sheet object
const messageBody = "Dear receiver, We would like to invite you on upcoming seminar please use this link {actual link} and show receiptionis this bar code {actual code}"
const subjectText = "Seminar invitation"
// loop rows or spread sheet
for (var i=2; i<= lastRow; i++) {
var receiverName = spreedsheet.getRange(i, 1).getValue();
var receiverEmail = spreedsheet.getRange(i, 2).getValue();
var seminarLink = spreedsheet.getRange(i, 3).getValue();
var barcode = spreedsheet.getRange(i, 4).getBlob();
var msgBdy = messageBody.replace("receiver",receiverName).replace("{actual link}", seminarLink);
var photoBlob = barcode
MailApp.sendEmail({
to: receiverEmail,
subject: subjectText,
htmlBody: messageBody + "<br /><img src='cid:qrCode'>",
inlineImages: { qrCode: photoBlob }
});
}
}
From your following reply,
images are created from =IMAGE("chart.googleapis.com/…) with that google sheet excel
In this case, I thought that your goal can be achieved using Google Apps Script.
When I saw your script, getValue() is used in a loop. In this case, the process cost will become high. In this modification, this issue is also modified.
Modified script:
function sendEmail(sheetName = "emails") {
var spreedsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var lastRow = spreedsheet.getLastRow();
const messageBody = "Dear receiver, We would like to invite you on upcoming seminar please use this link {actual link} and show receiptionis this bar code {actual code}";
const subjectText = "Seminar invitation";
const range = spreedsheet.getRange("A2:D" + lastRow);
const values = range.getValues();
const formulas = range.getFormulas();
values.forEach(([receiverName, receiverEmail, seminarLink], i) => {
const url = formulas[i][3].split('"')[1];
const photoBlob = UrlFetchApp.fetch(url).getBlob();
var msgBdy = messageBody.replace("receiver", receiverName).replace("{actual link}", seminarLink);
MailApp.sendEmail({
to: receiverEmail,
subject: subjectText,
htmlBody: messageBody + "<br /><img src='cid:qrCode'>",
inlineImages: { qrCode: photoBlob }
});
});
}
When this script is run, the values are retrieved from "A2:D" of the sheet. And, each value is retrieved from each row. In the case of the column "D", the URL is retrieved from the formula, and the image blob is retrieved using UrlFetchApp. And, those values are put as the email.
References
forEach()
fetch(url, params)
Update 2
As said in the comments, replace this line:
var barcode = spreedsheet.getRange(i, 4).getBlob();
To:
var barcode = UrlFetchApp.fetch(spreedsheet.getRange(i, 4).getFormula().match(/\"(.*?)\"/)[1]).getAs('image/png')
Update
As you are inserting the image via a IMAGE Formula, you can easily parse the value, obtain the URL and get the Blob via UrlFetchApp
Sample:
const sS = SpreadsheetApp.getActive()
function testSendImage() {
const img = sS.getRange('A1').getFormula()
const urlImg = img.match(/\"(.*?)\"/)[1]
const fetchImage = UrlFetchApp.fetch(urlImg).getAs('image/png')
MailApp.sendEmail({
to: "some#gmail.com",
subject: "QR",
htmlBody: 'Check this QR <img src="cid:fetchImage" />',
inlineImages: { fetchImage: fetchImage } })
}
In the current state, there is no method for extrancting the image from a cell, however there is a Feature Request on Google's Issue Tracker, you can click here to review it.
Remember to click in the top right if you want this feature to be implemented.
In any case, you review this StackOverflow question for workarounds or alternative methods.

activation of customer by sending link to his email using Suitelet Scripts

Overview:
I create one custom customer form using Suitelet Script with fields like name,email,phone in GET Method and the values entered in this fields obtained in the POST Method by request.getParameter() method. the customer record is created is successfully but when clicking the submit button the customer record is to be "inactive" mode and the url link is to sent to the customer email address ,when he clicks the link his record is to be changed to"active" mode this must be with in same suitelet page.
My Requirement:
I created the customer record successfully with INACTIVE MODE and link this send the customer mail but i need how to make the customer record to ACTIVE MODE when he clicks the link and the result is to be viewed in same suitelet page.
I have Given my code Sample Below:
function getCustomerInfo(request, response){
if(request.getMethod() == 'GET'){
//CREATING THE CUSTOM FORM AND ADDING FIELDS IN THE FORM
var form = nlapiCreateForm('Custom Customer Form');
form.addField('sfg_company', 'text', 'Company Name').setMandatory(true);
form.addField('sfg_address1','text','Address1');
form.addField('sfg_address2','text','Address2');
form.addField('sfg_city','text','City');
form.addField('sfg_state','text','State');
form.addField('sfg_emailaddr','email','Email').setMandatory(true);
form.addField('sfg_phone','phone','Phone');
form.addSubmitButton('Submit');
response.writePage(form);
}else{
nlapiLogExecution('DEBUG','form',form);
// CREATING THE RECORD BY GETTING THE VALUES ENTERED IN THE CUSTOM FORM
var compName = request.getParameter('sfg_company');
var compAdd1 = request.getParameter('sfg_address1');
var compAdd2 = request.getParameter('sfg_address2');
var cities = request.getParameter('sfg_city');
var stateName =request.getParameter('sfg_state');
var email_addr = request.getParameter('sfg_emailaddr');
var phone_num = request.getParameter('sfg_phone');
var newCust = nlapiCreateRecord('customer');
newCust.setFieldValue('companyname', compName);
newCust.setLineItemValue('addressbook', 'addr1', 1, compAdd1);
newCust.setLineItemValue('addressbook', 'addr2', 1, compAdd2);
newCust.setLineItemValue('addressbook', 'city', 1, cities);
newCust.setLineItemValue('addressbook', 'state', 1, stateName);
newCust.setFieldValue('email', email_addr);
newCust.setFieldValue('phone', phone_num);
newCust.setFieldValue('subsidiary', 1);
newCust.setFieldValue('isinactive','T');
//sending activation link to the customer
var sender = nlapiGetUser();
var receiver = email_addr;
var subject = 'Customer Activation Link';
var recordId = nlapiGetRecordId();
var url = "https://system.na1.netsuite.com/app/common/entity/custjob.nl?id="+recordId+"&whence=";
var body = 'Dear Customer,Your Record is Created Successfully and it will activated by click the following link :'+ url;
nlapiSendEmail(sender,receiver,subject,body);
var id = nlapiSubmitRecord(newCust);
nlapiSetRedirectURL('RECORD','customer',id,null,false);
}
}
It sounds like what you basically want is to send out a confirmation link to a customer that they can click and complete some sort of registration.
If you want to re-use your suitele code then you need to pass a custom stage or action parameter that you can trigger different actions from. e.g.:
var stage = request.getParameter('custparam_action') || 'showForm';
switch(stage){
case 'showForm': doShowForm(request, response); break;
case 'validate' : doValidate(request, response); break;
}...
Then the url you need to send out needs to reference the suitelet rather than the customer page in Netsuite:
var ctx = nlapiGetContext();
var url = nlapiResolveURL('SUITELET', ctx.getScriptId(), ctx.getDeploymentId(), true) +"&custparam_action=validate"+ getSecureValidationParams());
the function getSecureValidationParams should create some sort of time sensitive parameters including a hashed key. If the request passes validation then you would show whatever other form you want to show or redirect to or show a thank you page.
function getCustomerInfo(request, response){
if(request.getParameter('custscript_sfg_custmer_param') != null){
var value = request.getParameter('custscript_sfg_custmer_param')
var loadRecord = nlapiLoadRecord('customer',value);
loadRecord.setFieldValue('isinactive','F');
loadRecord.setFieldValue('custentity_sfg_referredby',1);
nlapiSubmitRecord(loadRecord);
nlapiSetRedirectURL('RECORD','customer',value);
}
if(request.getParameter('custscript_sfg_custmer_param') == null){
if(request.getMethod() == 'GET'){
//CREATING THE CUSTOM FORM AND ADDING FIELDS IN THE FORM
var form = nlapiCreateForm('Custom Customer Form');
form.addField('sfg_company', 'text', 'Company Name').setMandatory(true);
form.addField('sfg_address1','text','Address1');
form.addField('sfg_address2','text','Address2');
form.addField('sfg_city','text','City');
form.addField('sfg_state','text','State');
form.addField('sfg_emailaddr','email','Email').setMandatory(true);
form.addField('sfg_phone','phone','Phone');
form.addSubmitButton('Submit');
response.writePage(form);
}
}
if(request.getMethod() == 'POST'){
nlapiLogExecution('DEBUG','form',form);
// CREATING THE RECORD BY GETTING THE VALUES ENTERED IN THE CUSTOM FORM
var compName = request.getParameter('sfg_company');
var compAdd1 = request.getParameter('sfg_address1');
var compAdd2 = request.getParameter('sfg_address2');
var cities = request.getParameter('sfg_city');
var stateName =request.getParameter('sfg_state');
var email_addr = request.getParameter('sfg_emailaddr');
var phone_num = request.getParameter('sfg_phone');
var newCust = nlapiCreateRecord('customer');
newCust.setFieldValue('companyname', compName);
newCust.setLineItemValue('addressbook', 'addr1', 1, compAdd1);
newCust.setLineItemValue('addressbook', 'addr2', 1, compAdd2);
newCust.setLineItemValue('addressbook', 'city', 1, cities);
newCust.setLineItemValue('addressbook', 'state', 1, stateName);
newCust.setFieldValue('email', email_addr);
newCust.setFieldValue('phone', phone_num);
newCust.setFieldValue('subsidiary', 1);
newCust.setFieldValue('isinactive','T');
//sending activation link to the customer
var sender = nlapiGetUser();
var receiver = email_addr;
var subject = 'Customer Activation Link';
var recordId = nlapiGetRecordId();
var webAddress = "https://system.na1.netsuite.com"
var scriptType = nlapiGetContext().getScriptId();
var scriptId = nlapiGetContext().getDeploymentId();
var location = nlapiResolveURL('SUITELET',scriptType,scriptId);
var id = nlapiSubmitRecord(newCust);
var link = webAddress+location+'&custscript_sfg_custmer_param='+id+"&whence=";
var url = 'Click Here ';
var body = 'Dear Customer,Your Record is Created Successfully and it will activated by click the following link :'+ url;
nlapiSendEmail(sender,receiver,subject,body);
nlapiSetRedirectURL('SUITELET',scriptType,scriptId);
}
The above code will Get Customer Information through external Suitelet form and saving the record in the system.

NotesRichTextItem.getMIMEEntity() always returns null

I have a notes form with a rich text field on it, called "Body". I've set the "Storage" property of the field to "Store contents as HTML and MIME".
Now, I am creating a new document with that form in the Notes Client.
However, if I try to access the rich text field's value in SSJS with NotesRichTextItem.getMIMEEntity(), it always returns null.
Am I missing something?
Thank you for your help in advance.
Update 2: 02/12/2015
I did some more testing and I found the cause, why it won't recognize the rich text field as MIME Type, but rather always returns it as RICH TEXT:
The cause is me accessing the database with "sessionAsSigner" rather than just using "database".
If I remove "sessionAsSigner" and use "database" instead, making the XPage unavailable to public access users, so, I am forced to log in, the code recognizes it as MIME Type and I can get a handle on NotesMIMEEntity.
Unfortunately, the XPage has to be available to public access users and I have to use sessionAsSigner.
When I open the document properties and I look at the rich text field, I can see that the "Field Flags" are "SIGN SEAL". My guess is, that's why sessionAsSigner doesn't work, but it is just a guess.
Any ideas?
Update 1: 02/12/2015
Here is the code I am using in my SSJS:
var oDBCurrent:NotesDatabase = sessionAsSigner.getDatabase(session.getServerName(), session.getCurrentDatabase().getFilePath());
var oVWMailProfiles:NotesView = oDBCurrent.getView('$vwSYSLookupEmailProfiles');
var oVWPWResetRecipient:NotesView = oDBCurrent.getView('$vwPWPMLookupPWResetNotificationProfiles');
var oDocPWResetRecipient:NotesDocument = null;
var oDocMailProfile:NotesDocument = null;
var oDocMail:NotesDocument = null;
var sServer = session.getServerName();
oDocPWResetRecipient = oVWPWResetRecipient.getDocumentByKey(sServer, true);
oDocMailProfile = oVWMailProfiles.getDocumentByKey('.MailTemplate', true);
oDocMail = oDBCurrent.createDocument();
//Set default fields
oDocMail.replaceItemValue('Form', 'Memo');
oDocMail.replaceItemValue('Subject', oDocMailProfile.getItemValueString('iTxtSubject'));
oDocMail.replaceItemValue('SendTo', oDocPWResetRecipient.getItemValue('iNmesRecipients'))
//Get body text
var oItem:NotesItem = oDocMailProfile.getFirstItem("Body");
var entity:NotesMIMEEntity = oItem.getMIMEEntity();
//Create email body
var tmp = entity.getContentAsText();
//Replace <part2> with part 2 of the password
tmp = #ReplaceSubstring(tmp, "<part2>", sPWPart2);
//Set content of Body field as MIME type
var body = oDocMail.createMIMEEntity();
var stream = session.createStream();
stream.writeText(tmp);
body.setContentFromText(stream, "text/html; charset=iso-8859-1", 0);
//Send email
oDocMail.send();
As I mentioned before, I've also tried:
var oDBCurrent:NotesDatabase = sessionAsSigner.getDatabase(session.getServerName(), session.getCurrentDatabase().getFilePath());
var oVWMailProfiles:NotesView = oDBCurrent.getView('$vwSYSLookupEmailProfiles');
var oVWPWResetRecipient:NotesView = oDBCurrent.getView('$vwPWPMLookupPWResetNotificationProfiles');
var oDocPWResetRecipient:NotesDocument = null;
var oDocMailProfile:NotesDocument = null;
var oDocMail:NotesDocument = null;
var sServer = session.getServerName();
oDocPWResetRecipient = oVWPWResetRecipient.getDocumentByKey(sServer, true);
oDocMailProfile = oVWMailProfiles.getDocumentByKey('.MailTemplate', true);
oDocMail = oDBCurrent.createDocument();
//Set default fields
oDocMail.replaceItemValue('Form', 'Memo');
oDocMail.replaceItemValue('Subject', oDocMailProfile.getItemValueString('iTxtSubject'));
oDocMail.replaceItemValue('SendTo', oDocPWResetRecipient.getItemValue('iNmesRecipients'))
//Get body text
var entity:NotesMIMEEntity = oDocMailProfile.getMIMEEntity('Body');
//Create email body
var tmp = entity.getContentAsText();
//Replace <part2> with part 2 of the password
tmp = #ReplaceSubstring(tmp, "<part2>", sPWPart2);
//Set content of Body field as MIME type
var body = oDocMail.createMIMEEntity();
var stream = session.createStream();
stream.writeText(tmp);
body.setContentFromText(stream, "text/html; charset=iso-8859-1", 0);
//Send email
oDocMail.send();
Try calling sessionAsSigner.setConvertMime(false)
You get the MIMEEntity from the document, not from the Richtext item. See an example here (starting at line 103): https://github.com/zeromancer1972/OSnippets/blob/master/CustomControls/ccSnippets.xsp
You should set the session to not convert MIME to RichText.
Add this at the start of your code.
session.setConvertMime(false);

How Do I get a page's URL using JSOM

I am using SharePoint 2013 workflow.
I am in the Initiation form when my my users clock the Start button to start the workflow.
I am using JSOM to start the workflow but since I am on the Initiation form, I don't know the URL of the page. I do know the list (pages) and the the list id (2).
Can someone help me retrieve the list id's url using JSOM?
Thanks
Tom
How to get Page Url in Initiation Form page:
var listId = getParameterByName('List');
var itemId = getParameterByName('ID');
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getById(listId);
var listItem = list.getItemById(itemId);
ctx.load(listItem);
ctx.executeQueryAsync(
function () {
var itemUrl = listItem.get_item('FileRef');
console.log(itemUrl);
},
function (sender, args) {
console.log(args.get_message());
}
);
,where
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
is intended for retrieving parameter from query string
Source

Sending Emails in Sharepoint

I need to know what is the best practice for sending emails from my sharepoint webparts and/or customized features.
Should I just use the normal .Net classes to send email ? or is their a better way to do it through integration with an outlook server ?
Easy way is to use the built in Utilities, this will then use the mail server setttings setup in central admin
using Microsoft.SharePoint.Utilities;
SPUtility.SendEmail(SPContext.Current.Web, false, false,
"toaddress#mail.com", "subject",
"body");
Universal way to send email in any context(where SPWeb not available) is read OutboundMailService settings which is used in SPUtility. Then create SmtpClient manually:
var adminApp = SPAdministrationWebApplication.Local;
var instance = adminApp.OutboundMailServiceInstance;
var server = instance.Server.Address;
var defaultFrom = adminApp.OutboundMailSenderAddress;
var client = new SmtpClient();
client.Host = server;
message.From = new MailAddress(defaultFrom );
...
You also can use this code for dynamic mail id. this code gets the mail according to the user. I have used CAML query to get the data for the email content from the lists.
SPUser AssigUser = oWeb.EnsureUser(Assigned_Username);
SPQuery mquery = new SPQuery();
mquery.Query = #"<Where><Eq><FieldRef Name='Email_x0020_Type' />
<Value Type='Text'>Review - Ready for Review</Value>
</Eq></Where>";
string Emailsub = "";
string Emailbody = "";
SPList mList = oWeb.Lists["Email Content"];
SPListItemCollection itemcollection = mList.GetItems(mquery);
foreach (SPListItem item in itemcollection)
{
Emailsub = item["Email Subject"].ToString();
Emailbody = item["Email Content"].ToString();
SPUtility.SendEmail(oWeb, false, false, AssigUser.Email, Emailsub,
Emailbody + "</br>" + oWeb.Url);
break;
}
using overload with StringDictionary arguments (source)
StringDictionary headers = new StringDictionary();
headers.Add("to", currCtxt.Web.CurrentUser.Email);
headers.Add("cc", "xyz#abc.com");
headers.Add("bcc", "");
headers.Add("from", "email#add.com");
headers.Add("subject", "Email Subject");
headers.Add("content-type", "text/html");
string bodyText = "Hello how are you?";
SPUtility.SendEmail(currCtxt.Web, headers, bodyText.ToString());

Resources