Search code examples
javascriptsmtpnewlinesmtpjs

How to add a newline in string smtpjs


Im trying to make newlines in the body of the SMTPJS

        Email.send({
        Host: "smtp.gmail.com",
        Username : "adventureboots.manager@gmail.com",
        Password : "gabriela2600",
        
        To : "bernardoolisan@gmail.com",
        From : "adventureboots.manager@gmail.com",
        Subject : "Compra AdventureBoots de " + name + " " + apellido,
        Body : "ADVENTUREBOOTS!\n" + name + " " + apellido + "\nDATOS DE DISEÑO" + "\nModelo | " + modelo +"\nTalla | " + talla + "\nColor | " + color + "\nColor De Costuras | " + color_de_costura + "\nMaterial | " + material + "\nDATOS DE DIRECCION" + "\nDirección | " + direccion + "\nLugar | " + vivienda + "\nCiudad | " + ciudad + "\nCodigo Postal | " + codigo_postal + "\nCONTACTO" + "\nTelefono | " + telefono + "\nEmail | " + email
    })

when i saw the mail \n is not working, any idea?


Solution

  • We could try with HTML with the br tag (this is what it works in the OP):

    <br>
    

    or, in JS we could use the template literals (also called Template strings).

    `some text ${aVariable} some other text`
    

    Even if it doesn't work, the string assigned to the body property would look much cleaner.

    const obj = {Body : `ADVENTUREBOOTS!
    ${name} ${apellido}
    DATOS DE DISEÑO
    Modelo ${modelo}
    Talla ${talla}`};
    

    JS Snippet:

        const name = "John";
        const apellido = "Green"
        
        const obj = {Body : `ADVENTUREBOOTS!
        ${name} ${apellido} ...`};
        
        console.log(obj);

    Since we are using SMTP, if the solutions above are not working, you might need to create a string according to the Simple Mail Transfer Protocol spec: Spec 2.3.7:

    "[message data] Lines consist of zero or more data characters terminated by >the sequence ASCII character "CR" (hex value0D) followed immediately by ASCII >character "LF" (hex value 0A).

    ASCII table