Max Scott
Max Scott

Reputation: 1

embed a button in email created by script

I have a script from a sheet which can create and send an email. The email contains a link to form. But the link is now very long and pretty ugly. Can I instead add a button to the email body and have the link held with the button? Would all have to created by the script though.

Thanks

Max

Upvotes: 0

Views: 1514

Answers (2)

RayGun
RayGun

Reputation: 521

HTML Button elements are used to submit data from a form or otherwise execute scripts when they are clicked. If you want to shorten a link, you would be better off going with an HTML Anchor element, which has a format as follows:

<a href="www.yourURL.com">Shortened Text</a>

If you want your anchor element to look like a button, you can achieve that with only a few lines of CSS, as demonstrated here.

So, your code to send a shortened link (that looks like a button) in an email would be as follows:

const  recipient = "[email protected]"
const subject = "Email Subject"
const url = " ... "
const htmlBody =
 "<a 
    style="display: block;
    width: 115px;
    height: 25px;
    background: #4E9CAF;
    padding: 10px;
    text-align: center;
    border-radius: 5px;
    color: white;
    font-weight: bold;
    line-height: 25px;"
    href=" + url + ">
                      Shortened Text
  </a>"

GmailApp.sendEmail(recipient, subject, htmlBody, { htmlBody });

[Edit] As @Martí mentions in his/her/their answer, you can use MailApp or GmailApp to send emails. MailApp has a more limited authorization scope, so it should be used unless you need access to the Gmail API. To send an email with MailApp, you would change the last line of code in the above sample to

MailApp.sendEmail(recipient, subject, htmlBody, { htmlBody });

Upvotes: 0

Mart&#237;
Mart&#237;

Reputation: 2871

You cannot make buttons but you make links with custom text using an HTML body. Notice that email HTML is more restrictive than regular HTML that you find in pages (which is a good thing for security reasons). Usually when you make an HTML body you also make a plain text version of it, so it’s easier to read on every device.

Here is an example using Google Apps Script MailApp:

MailApp.sendEmail(
  '[email protected]',
  `Requested link`,
  `Here is the requested link:\nhttps://example.com/?ugly=true`,
  {
    htmlBody: `<a href="https://example.com/?ugly=true">Click here to go to the page</a>`
  }
)

Notice that if you are mailing automatically you probably want to use bcc instead of recipient to prevent leaking emails to multiple people and set noReply (if have a custom domain) if you don't want them to reply:

MailApp.sendEmail(
  '',
  `Requested link`,
  `Here is the requested link:\nhttps://example.com/?ugly=true`,
  {
    bcc: '[email protected]',
    htmlBody: `<a href="https://example.com/?ugly=true">Click here to go to the page</a>`,
    noReply: true
  }
)

References

Upvotes: 1

Related Questions