1

Task to accomplish:

1) A user types information in a form

2) Via ajax (method: POST) the form gets posted to wordpress

3) Wordpress takes the form

4) Wordpress creates an HTML-email which contains all field forms with this format: 'name':'value'

5) The email is sent to a specific email-address.

Here is my function which builds up the email HTML body:

function buildEmailBody() {
    $emailMessage = '';
    $emailMessage .= "<!DOCTYPE html>";
    $emailMessage .= '<html><head><meta http-equiv="Content-Type" content="text/html charset=UTF-8" /></head>';
    $emailMessage .= '<body>';
    $emailMessage .= "<h1>Startup Bewerbung</h1>";
    foreach ($_POST['form_fields'] as $key => $value) {
        $emailMessage .=  '<strong>' . htmlspecialchars($key) . ': </strong>';
        $emailMessage .= '<br/>';
        if (is_array($value)) {
            $emailMessage .= htmlspecialchars(implode(',', $value));
        } else {
            $emailMessage .= htmlspecialchars($value);
        }
        $emailMessage .= '<br/>';
    }
    $emailMessage .= "</body>";
    $emailMessage .= "</html>";
    return nl2br($emailMessage);
}

In functions.php, I set the content type of the mail to 'text/html':

function set_mail_content_type(){
    return "text/html";
}
add_filter( 'wp_mail_content_type','set_mail_content_type' );

The mail is sent via wp_mail:

wp_mail('example@example.com', $emailSubject, $emailMessage, null, $attachements);

Problem:

The email is sent, BUT the content is very strange. Some 'names' are not displayed as as they should (within a strong tag as defined in code -> look image). On some points there are misterious whitespaces added. Any help is greatly appreciated.

issue

EDIT:

I guess the problem is somewhere around here: Is PHPMailer standard for wp_mail()?

Furthermore: After each line in the following picture, it seems like there is an whitespace added. That's why the html breaks and on some positions there are suddenly whitespaces. Issue

So look at the endlines in the previous pictures and see in the next picture: There are whitespaces added

  • if you output the email rather than passing it to wp_mail does it display correctly? – Tom J Nowell Jul 14 '18 at 19:03
  • If I extract the original text that is submitted to gmail, copy it to a .html file, open it in Chrome, it shows the exact same issue. I'll upload another screenshot... – QuickSort Jul 14 '18 at 19:25
  • I meant, don't send the email at all, output it directly and verify it's good at WP's end, did you do that? Or did you only look at the emailed result? We need to check assumptions, and you're assuming that good content is being passed to wp_mail where it gets broken, without checking if it's already broken – Tom J Nowell Jul 14 '18 at 20:13
  • Then I got you wrong. I have already checked it at wp-end. and there the message body is correct - well displayed in chrome-browser. – QuickSort Jul 14 '18 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.