Apps script does not send email containing links to Google Drive files on a given G Suite domain, no error message - email

Context
We are currently working on a short Apps script that sends links to Google Drive files (shared with anyone with the link) through the MailApp.sendEmail(options) function.
The script works well on our test G Suite domain but on the production domain, it just does not send the emails. There are no error messages.
Some code
The issue can be reproduced through the following pieces of code:
Code.gs file
function test_sendEmail() {
const template = HtmlService.createTemplateFromFile('template.html');
template.link = "https://drive.google.com";
template.title = "This is a link";
const mailBody = template.evaluate().getContent();
console.log("Quota: " + MailApp.getRemainingDailyQuota()); /* Quota is not exceeded. */
try {
const options = {
to: "myEmail#Address.com", /* Replace this with your email address */
subject: "LINK",
htmlBody: mailBody,
noReply: true
};
MailApp.sendEmail(options);
} catch(e) {
console.log(e.message);
}
}
template.html file
Link: <a href='<?= link ?>'><?= title ?></a>
What we tried
Emails are properly sent when the link does not contain the drive.google.com part. For example, emails with links to google.com are sent properly.
We are able to send an email from the Gmail account the script is executed as and this email is sent properly with the Drive links.
As opposite as this question, I do not get "Message blocked" email and using GmailApp.sendEmail instead of MailApp.sendEmail does not seem to change anything.
Finally, the script above works well in the test G Suite domains and in some others we tried.
Thus, I believe that it comes from the G Suite domain configuration that have specific restrictions on Apps script. Is it possible? Where can I change it in the G Suite admin console? What other points should I check to make it work?
More details on the issue:
The script fail to send email both with G Suite, gmail and other
email addresses.
We tried in both Apps script runtime (V8 and legacy). Both doesn't
work properly.
I believe that SPF/DKIM/DMARC are not setup in the domain.
We use the same test recipients in both G Suite environments.
The code after the sendEmail method call is executed properly.
The provided template was just an example to reproduce the issue.
The real template looks like a typical email, with a list of links. It is not link-only.
In the future, the script is supposed to send less than 100 emails per day. Right now, it is just sending a few test emails (not more than in test environment).
We use the default cloud project associated with the script.
The script is bound to a Google Sheets file.
Below is the real template that we use.
Dear Customers,<br/><br/>
We inform you that new documents are available. Please find them below:
<ul>
<? for(let i = 0; i < docs.length; i++) { ?>
<li>Document <a href='<?= docs[i].link ?>'><?= docs[i].title ?></a>, <i><?= docs[i].documentType ?></i> is available</li>
<? } ?>
</ul>
<br/>
Regards,<br/>
Your customer service.

Instead of MailApp use GmailApp (you might have to change the sendEmail parameters, check the docs)
The above because others have being reported similar problems, actually something similar happened recently to me while working on client's project.
Related
MailApp.sendEmail method doesn't get through to accounts with URL in the body - Message Blocked

Related

Capture HTML Form Responses in Google Sheets and send a Confirmation Email to the Form Submitter

I want the script to send the created html email template email.html to the person who last submitted the form.
Below is the success message I get in the console:
Below is the actual email that is received by the person who submits the form:
The email.html is formatted correctly and appears perfectly when sent manually.
I hope this is reprex enough.
EDIT: Maybe it's easier if I include the code
function sendEmail () {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var data = ss.getRange(lr,3);
var email = data.getValue();
var message = HtmlService.createTemplateFromFile('email');
var subject = "Test Subject"
GmailApp.sendEmail(email,subject, message);
}
It's not clear why you are using HtmlService.createTemplateFromFile, but from the image it's clear there at least one error, the script misses two methods:
HtmlService.HtmlTemplate.evaluate() to evaluate the Templated HTML
HtmlService.HtmlOutput.getContent() to get the HTML from the HtmlService.HtmlOutput object returned by the previous method.
Another option that looks to be an error is the use of GmailApp.sendEmail(string,string,string) method, as the third parameter should be a string to be used as the email plain text content. If you want to pass HTML, instead use GmailApp.sendEmail(string,string,string, Object)
Related
Emailing Google Sheet range (with or without formatting) as a HTML table in a Gmail message
Sending an email in HTML and plain with a Gmail Apps Script
Google script inject html into html email
References
https://developers.google.com/apps-script/guides/html
https://developers.google.com/apps-script/guides/html/templates
https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String)

Retrieving gmail signature with google apps script

Is there a way to retrieve the signature of my Gmail account using Google Apps script?
I'm currently using Google Apps Script to send out emails and it would be nice to be able to append my email signature to the end of the email body because Google Apps Script doesn't automatically do so.
While the Gmail API doesn't expose the signature, you can use a workaroud to grab the signature from Gmail.
Create a new Gmail draft, put the word "signature" in the subject and save the draft.
The body will include the signature by default. Do not modify the body.
Save and Close the draft.
Go to Google Script editor and use this script to grab the signature.
function getGmailSignature() {
var draft = GmailApp.search("subject:signature label:draft", 0, 1);
return draft[0].getMessages()[0].getBody();
}
Unfortunately, We don't have any user level access to retrieve our signature but, We have workaround, you can append signature simply copy your signature as a HTML [ Browser inspect mode] code from Gmail UI add to script variable, You can append this HTML string at the end of every email.
You can make it dynamic like name and Email ID
Sample code
function myFunction() {
var signHTML =
'<br><br><br><br><table style="border-bottom:1px solid"><tbody><tr><td><table><tbody><tr><td><table style="padding-right:20px">'+
'<tbody><tr><td></td></tr></tbody></table></td><td><table><tbody><tr><td><font face="open sans, sans-serif">'+
'<span style="font-size: 12px;"><b>YOUR NAME</b></span></font></td></tr><tr><td><font face="open sans, sans-serif">'+
'<span style="font-size: 12px;"><b>DESIGNATION </b></span></font>'+
'</td></tr><tr><td><div><span style="font-family:open sans,sans-serif;font-size:12px">+91-XXX-711-XXXX</span>'+
'</div><div><span style="font-family:open sans,sans-serif;font-size:12px">+91-XXX-8XXX09-XXXX</span></div>'+
'<div><a href="https://www.searce.com" style="text-decoration:blink;vertical-align:top" target="_blank">'+
'<span style="color:#000000;font-family:open sans,sans-serif;font-size:12px">www.yourComany.com</span></a>'+
'</div></td></tr></tbody></table></td></tr></tbody></table></td></tr><tr><td></td></tr></tbody></table>';
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: "Upend signature at the end of mail",
htmlBody: "Dear User</b>Signature testing" +
signHTML,
});
}
It works we are using this. Hope it helps you. :)

Google Script email also being sent to account owner?

I have a problem with a Google MCC Script I have. It's set up to run every day in the early hours of the morning, do some processing, and email out a result, using Google Scripts' built in MailApp.sendEmail function.
The problem is that, while the email is sent successfully, I'm also recieving messages in the inbox of the email address which owns the MCC account along the lines of
Delivery to the following recipient failed permanently:
MCC_account#example.com
Technical details of permanent failure: The email account that you
tried to reach does not exist. Please try double-checking the
recipient's email address for typos or unnecessary spaces.
with the 'Original Message' appended below that indicating it is indeed the message the Script has sent. Here's my code:
function main() {
var accountSelector = MccApp.accounts();
var accountIterator = accountSelector.withIds('###-###-###').get();
if(accountIterator.hasNext()){
var account = accountIterator.next();
MccApp.select(account);
var data = getData();
sendEmail(data);
} else Logger.log("Error: no accounts found");
}
function sendEmail(data){
var name = 'name';
var bodytext = 'body';
MailApp.sendEmail({
to: 'receiver-inbox#example.com',
name: 'Google Adwords Scripts',
replyTo: 'do-not-reply#example.com',
subject: 'SUBJECT',
attachments: [{fileName: name, mimeType: 'text/csv', content: data}],
body: bodytext
});
}
So, to clarify, the MCC account is owned by one email address, the script doesn't reference that at all, but I'm recieving the email not only in the target mailbox but also a failed delivery message in the owner inbox.
Can anyone shed any light on what is happening here?
It is very likely that you are running another copy of the script that is sending these emails. Go to your Google Account settings here and revoke access to the other script.
If you have multiple Google accounts, do a scan for all the accounts.
Okay, apparently this is a known issue with AdWords Scripts:
https://groups.google.com/forum/#!topic/adwords-scripts/SJtNW_wuArI

Get sender email address in Infopath

I have a form sent by email that travels through different persons like this.
Person A --> Person B --> Person C
I want the person A to be informed when the form is treated by person C. So Person A needs to be in copy of the email sent by person B.
Because person A isn't always the same one, I think the best way to put him/her in copy is to use the "from" field of the email received by person B and to put it in copy.
But how can I find this address with infopath and how can I place it into my email data connection ?
I had this same question today myself and could not find much in the way of answers.
So... I did some work myself and came up with a few solutions.
First I don't believe there is any way to get/set the "From" address using the InfoPath OM. This means you will have to use one of the following options:
No Code:
You will be limited to providing a field on the form where "Person A" can put their email address and use this in the CC. for subsequent stages. That's kind of the only way and while it an extra burden to the user it does have the benefit of providing flexibility.
Code:
Write your own code to send the mail using Outlook Interop or System.Net.Mail and then you will be setting all of the addresses manually anyway.
If you are using AD or something else then you could always get the email address of the current use using System.DirectoryServices.AccountManagement.
Based on an assumption which I cannot find any documentation to back up. That InfoPath uses the account associated with the default store to send email using EmailSubmitConnection. You should be able to use Outlook Interop to find the address that InfoPath will use.
Here is a code sample:
using Outlook = Microsoft.Office.Interop.Outlook;
public string GetDefaultSenderAddress()
{
// This actually opens outlook in the same way as InfoPath does to send the message.
// which can be slow.
string DefaultAddress = string.Empty;
Outlook.Application OutlookApplication = new Outlook.Application();
string DefaultStoreId = OutlookApplication.Session.DefaultStore.StoreID;
foreach (Outlook.Account Account in OutlookApplication.Session.Accounts)
{
if (Account.DeliveryStore.StoreID == DefaultStoreId)
{
DefaultAddress = Account.SmtpAddress;
}
}
// Note you probably won't want to quit if you are about to send the email.
// However I have noticed that this doesn't seem to close Outlook anyway.
OutlookApplication.Quit();
return DefaultAddress;
}
You may have to provide a few more checks in case of different account types etc. But I believe it will work. (I tested it for my scenario and it does).
Note: Of course this opens an outlook instance which you will have to close as well. And it can be slow. Unless outlook is already open in which case it will be very quick. Anyhow when sending from InfoPath Outlook will have to be opened so if you do this just before sending then there should be no noticeable difference.
I would advise using a combination of the no code/with code options so provide a return address which is automatically complete to save the user time. But can be corrected if the user wishes to have the email returned to a different address of if there is a mistake.
Hope that you find that useful.

how to send files as attachment in email in magento for downloadable products?

I am new to Magento. I am working on a web site which is selling downloadable products.
My client want purchased products should be sent via email in attachment?
Currently, I am developing in localhost so I am not sure whether magneto actually send product files in email or not?
Should I need to enable any option for that in configuration?
If you want to develop this at your localhost keep in mind, that you have to set up mail server or use some solutions from community like below link, to send it by gmail and other:
https://www.magentocommerce.com/magento-connect/smtp-pro-email-free-custom-smtp-email.html
You also can just configure your server to save mails without sending.
So you'll be able to review it.
Good article about how to send downloadable by email here:
https://magento.stackexchange.com/questions/49511/how-can-i-get-only-the-downloadable-product-url-in-email-template
But!!! keep in mind that the latest Magento use queue to send email (which runs by cron), so if you have such a distribution (for example 1.9+) you need to adjust code which you have from link above.
Here istwo solutions for this case:
Disable adding email to queue after order.
Just comment this part of code in "Mage_Core_Model_Email_Template"'s "send()" method:
// if ($this->hasQueue() && $this->getQueue() instanceof Mage_Core_Model_Email_Queue) {
/** #var $emailQueue Mage_Core_Model_Email_Queue */
/* $emailQueue = $this->getQueue();
$emailQueue->clearRecipients();
$emailQueue->setMessageBody($text);
$emailQueue->setMessageParameters(array(
'subject' => $subject,
'return_path_email' => $returnPathEmail,
'is_plain' => $this->isPlain(),
'from_email' => $this->getSenderEmail(),
'from_name' => $this->getSenderName(),
'reply_to' => $this->getMail()->getReplyTo(),
'return_to' => $this->getMail()->getReturnPath(),
))
->addRecipients($emails, $names, Mage_Core_Model_Email_Queue::EMAIL_TYPE_TO)
->addRecipients($this->_bccEmails, array(), Mage_Core_Model_Email_Queue::EMAIL_TYPE_BCC);
$emailQueue->addMessageToQueue();
return true;
}*/
So emails will be sent immediately (be aware, that if you have very big turnover with spikes it can create performance issue)
Second way is to save full path of attachment to table "core_email_queue" field - "message_parameters". You can do it adding url to array of argument here $emailQueue->setMessageParameters( in code above.
After that you can handle it in "Mage_Core_Model_Email_Queue"'s "send()" method
using standard "Zend Mail"'s method - "createAttachment()". Below link provides deeper explanation of this part.
https://magento.stackexchange.com/questions/9652/magento-send-file-attachements-in-emails
Hope it will help sombody.
Have a good day!!!