Sending HTML emails via wp_mail not working properly
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 .= 'htmlheadmeta http-equiv="Content-Type" content="text/html charset=UTF-8" //head';
$emailMessage .= 'body';
$emailMessage .= "h1Startup 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('[email protected]', $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.
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.
So look at the endlines in the previous pictures and see in the next picture: There are whitespaces added
Topic html-email email plugins Wordpress
Category Web