0

How do you custom this code to be able to send multiline email? (code credit: Firestore + Swift; trigger email extension)

db.collection("mail").addDocument(data: [
    "to": "someone@example.com",
    "message": [
      "subject": "Hello from Firebase",
      "html": "This is an <code>HTML</code> email body."
    ]
]) { err in
    if let err = err {
        print("Error writing document: \(err)")
    } else {
        print("Document successfully written!")
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Shanely
  • 103
  • 12

3 Answers3

0

This should've been a comment, but i'm a new SO user.
Anyway, if i understood correctly, you want a multiline email. If you're using html code in your message body, i think you can use a line break </br>. On the other hand, a plain text could use a new line \n.
If this does not answer the question, i think i just misunderstood something. :/

mjoe7
  • 134
  • 6
0

Plese check the official documentation regarding how to compose and sends an email based on the contents of a document written to a specified Cloud Firestore collection.

Trigger Email Firebase Extension

The parameters that can be configured are:

Cloud Functions location
SMTP connection URI
Email documents collection
Default FROM address
Default REPLY-TO address
Users collection
Templates collection

You can also optionally configure this extension to render emails using Handlebar templates

marian.vladoi
  • 7,663
  • 1
  • 15
  • 29
0

If I'm understanding you correctly you have a similar problem to what I had; the user is able to type in something which can include line breaks and you want that to appear with line breaks in the email triggered by the Email Trigger function?

If so this is what I did (in Vue but can obviously be changed as appropriate).

I have a data property called 'message' and that automatically changes line breaks to \n. What I'm doing is saving a copy of the form to one collection ('enquiries') and triggering an email by writing to a different collection ('email').

In my function to do both of these I'm just using a different variable to change the line breaks to use the HTML tag

let HTMLmessage = this.form.message.replace(/\n/g, '<br/>');

Then when I'm adding to the email collection it's taking that:

db.collection('email').add({
            to: 'example@domain.com',
            template: {
              name: "template_name",
              data: {
                name: this.form.name,
                email: this.form.email,
                phone: this.form.phoneNumber,
                message: HTMLmessage
              }
            },
            replyTo: this.form.email,
            from: this.form.email,
          })

And in the email template ("template_name") in the exam above it is using the triple handbars:

<p>{{{message}}}</p>

This then preserves the line breaks in the email

enter image description here

There might well be better ways of doing it but this worked for me. In summary:

  1. Replace the \n with the HTML tag and then send that to the template
  2. Ensure the template uses the triple handlebars for that particular field
ashleytwo
  • 191
  • 2
  • 9