0
votes

I have an HTML form with a textarea which contains a default message which can be edited by the user. The HTML is as follows:

To: Email address >

 </div>

 <div class="form-group" id="ccInput">
     <label for="ccAddress"><i class="fa fa-envelope" aria-hidden="true"></i> Cc: Email address</label>
         <input type="text" data-role="tagsinput" multiple="multiple" name="ccAddress" class="form-control" id="ccAddress" aria-describedby="emailHelp" value=<?=ccEmail?>>
 </div>

 <div class="form-group">
    <label for="subject"><i class="fa fa-info-circle" aria-hidden="true"></i> Subject</label>
    <input type="input" name="subject" class="form-control" id="subject" value=<?=mailSubject?>>
 </div>

 <div class="form-group">
    <label for="subject"><i class="fa fa-pencil" aria-hidden="true"></i> Message</label>
 <div id="blankInput"></div>

   <textarea class="form-control" rows="12" id="text" name="message">Dear <?= firstName ?> <?= surName ?>,

<?= mailBody ?>

Kind regards,


<?= contractorName ?>


<?= contractorTitle ?></textarea>

 </div>

 <div class="form-group" id="pdfForm">

 <label for="pdfUrl"><i class="fa fa-paperclip" aria-hidden="true"></i> PDF Attachment</label>
 <input type="url" name="pdfUrl" class="form-control" id="pdfUrl" value=<?=pdfUrl?>>
 </div>

 </div>

 <button type="submit" class="btn btn-primary googleGreen" id="load" data-loading-text="<i class='fa fa-spinner fa-spin'></i> Sending">Send Email</button>
  <button type="button" class="btn btn-primary googleGray" onClick="google.script.host.close()">Cancel</button>

 <input type="hidden" name="process" value=<?= process ?>>   
 <input type="hidden" name="fromEmail" value=<?= fromEmail ?>>   
            </form>
        </div>
    </div>

I then have the following script to send the email in my .gs file:

  var toEmail = form.toAddress;  
  var ccEmail = form.ccAddress;  
  var fromEmail = form.fromEmail;
  var subject = form.subject;
  var message = form.message;
  var sender = form.contractorName; 


    GmailApp.sendEmail(
      toEmail,
      subject,
      message,
      {
      attachments:[pdfFile],
      cc: ccEmail,
      from: fromEmail,
      name: sender
      });

}

This all works fine, but the problem with the textarea is that obviously it cannot process HTML. I want to add an email signature containing several styles and a logo etc. but as far as I can see the only way to do this is the use the htmlBody advanced option. But then I can't use the textarea!

Is there a way to combine these 2 elements so that I can add some HTML at the end of the body which would contain the signature?

1
Why does it bother to use HTML only emails? You won't be able to use HTML in a text only container. The only thing you can to is write a separate text for people who reject the HTML bodies, I know such people still exist but I personally don't care, the only thing I put in the text body is "HTML only" ;)Serge insas
You would need to convert the plain text to html. The code would need to search for "end of line" characters \n and replace them with html break tags <br> If you don't do that, and you put the plain text into the HTML the new lines and spacing will be removed. You could do a global replace on all \n newline character codes, and replace with <br> tags.Alan Wells

1 Answers

2
votes

There is no such thing as "plain text email with an HTML signature at the bottom". The recipient will either see HTML, or the plain text fallback. If a part of the message requires HTML, the entire message must be in HTML. Something like

message = message.replace(/&/g, '&amp;')
                 .replace(/"/g, '&quot;')
                 .replace(/'/g, '&#39;')
                 .replace(/</g, '&lt;')
                 .replace(/>/g, '&gt;')
                 .replace(/\n/g, "<br>") 
                 + HtmlSignature

would avoid some common issues with plain-text characters that have special meaning in HTML, insert HTML breaks for new line characters, and append the signature.

See also HTML-encoding lost when attribute read from input field