Search code examples
phpjqueryphpmailerwp-mail

Mail - \r\n characters visible in recieved email


I'm sending a test email to myself which contain values inserted into a form.

I recieve the email, but the "formating" is all wierd and \r\n is visible. I tried %0D%0A and </br> (although didn't specify that it would be an HTML) as well and it was also visible. I also tried PHP_EOL.

Additionally, I also recieve a 2cnd email right after the 1st one, but it contains only a fraction of the previous form.

1st email:

Date of arrival: 19-02-2021%0D%0A
                                                                        Date of departure: 27-02-2021%0D%0A
                                                                        Room selected:  Room 01 %0D%0A
                                                                        Name: dread%0D%0A
                                                                        Surname: Zxy%0D%0A
                                                                        Email [email protected]%0D%0A
                                                                        Phone number: 012345678

2cnd email:

Date of arrival: 19-02-2021%0D%0A
                                                                        Date of departure: 26-02-2021%0D%0A

Edit I just noticed that the 2cnd email does not contain the same date of departure as the 1st one. I have no idea why that is, so I'll add the jQuery code as well.

I would be greatful if anyone has any idea how to resolve this.

The code:

<?php
    if(isset($_POST['submit'])) {
        $arrivalDate = $_POST['arrivalDate'];
        $departureDate = $_POST['departureDate'];
        $room = $_POST['room'];
        $user_name = $_POST['user_name'];
        $surname = $_POST['surname'];
        $email = $_POST['email'];
        $tel = $_POST['tel'];
                    
        $to = $_POST['email'];
        $subject = 'Reservation';
        $body = 'Date of arrival: '.$arrivalDate.'\r\n
                Date of departure: '.$departureDate.'\r\n
                Room selected: '.$room.'\r\n
                Name: '.$user_name.'\r\n
                Surname: '.$surname.'\r\n
                Email '.$email.'\r\n
                Phone number: '.$tel;
                            
        wp_mail( $to, $subject, $body );
        echo "Sent!";
    }
?>
jQuery(document).ready(function($) {
    $("#arrivalDate").datepicker({
        minDate: 'dateToday',
        dateFormat: 'dd-mm-yy',
        onSelect: function (date) {
            $("#departureDate").datepicker('option', 'minDate', date);
        }
    });
    $("#departureDate").datepicker({
        dateFormat: 'dd-mm-yy'
    });
});

Solution

  • In PHP there's a world of difference between single and double quotes:

    <?php echo('\r\n'); ?>
    

    This produces \r\n, literally, as text, whereas this:

    <?php echo("\r\n"); ?>
    

    Actually emits CRLF, as you want.

    Single quoted strings are not interpolated, and do not support control characters like \r. Double quoted strings do.

    That being said, there's a better way of expressing this:

    <?php
    $body = implode("\r\n", [
      "Date of arrival: $arrivalDate",
      "Date of departure: $departureDate",
      "Room selected: $room",
      "Name: $user_name",
      "Surname: $surname",
      "Email $email",
      "Phone number: $tel"
    ]);                        
    

    Let the string interpolation do the work for you, then use implode() to connect them together.

    In your code you're not only putting in \r\n but with the string spanning multiple lines you're including at least an LF as well, another extraneous \n.

    That means:

    <?php
    
    echo('I love
    newlines!');
    ?>
    

    Already contains a newline.