1

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 myemail@gmail.com%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'
    });
});
WeAreDoomed
  • 248
  • 1
  • 14

1 Answers1

1

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.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • All right. Thank you. the `"\r\n"` with double quotes did solve it, but I might use your anwser. If I would want to add additional string to the body (lets say something like: Thank you for registering. You have room1 reserved from xy to yx, would implode also work on it)? I'll accept the anwser in 2 mins. need to wait... – WeAreDoomed Feb 16 '21 at 22:24
  • You can always add additional lines after that as necessary, this format is quite open-ended. – tadman Feb 16 '21 at 23:14
  • Yea, just continue the string. Sorry a bit tired. I actually have a question about that. How can I add characters directly next to variables. something like `"price: €$price_per night"`? – WeAreDoomed Feb 16 '21 at 23:22
  • You can add whatever you want, [PHP strings](https://www.php.net/manual/en/language.types.string.php) are fairly flexible. – tadman Feb 16 '21 at 23:29
  • sorry, wrong example. I meant something like `"Price for night for $room: €$price_per_night"`. Im talking about the **:** next to `$room`. – WeAreDoomed Feb 16 '21 at 23:37
  • 1
    The docs explain in more detail, but generally only "variable-like" components are absorbed. `$room!` is fine. `$rooms` if you wanted the `s` separate is not, you'd have to do `{$room}s`. – tadman Feb 17 '21 at 00:05