Form sending email and HTML tags

All the activities are happening in .php file.

After collecting all the Inputs from the form by the user. The output is generated like this.

ob_start(); ?>

		First Name: <?= $firstname; ?>
		Family Name: <?= $familyname; ?>
		Email: <?= $email; ?>
		Other Name: <?= $other_names; ?>
		DOB: <?= $dob_day; ?>
		DOM: <?= $dob_month; ?>
		DOY: <?= $dob_year; ?>

<?php
		$message = ob_get_clean();

If i try to clean the output atleast every item in one uniqe row. If i do this:

First Name: <?= $firstname; ?> <br>
Line break is occurring, but in the received email <br> tag is also visible.

There must be a certain method to achieve the desired objective.

You may want to duplicate the code so you are outputting to HTML in the first instance and email in the second.

For the second, replace
with \r\n or %0D%0A for line breaks readable in email.

what does that means sir? Please pardon?

Are you asking me to put the whole code here?

This needs to be echoed in PHP tags →

The code the way you have it with <br> is good for HTML. Underneath all that code put it like:
First Name: <?= $firstname; ?> \r\n
… when you want it to output to email. Or are you trying to format the email as HTML?

1 Like

Even
was working, but those also appears in email. see here →

Thanks.

N.B. I tried that on few line.

An alternate is to try %0D%0A in place of /r/n (for a carriage return (%0D) and line feed (%0A)).

1 Like

What are you using to send the email? The standard PHP mail() function or one of the other libraries? Are you sending the proper MIME headers?

1 Like

WP Mail function(which uses PHP mailer) along with WP SMTP clubbed with mailgun.

same issue. Creating a Line break, but those tags are also present there.

image

What I was getting at is are you telling the mail function to use html for the message body? I just wondered if that was elsewhere in the code, but primarily because your <br> tags are showing rather than being interpreted, which suggests the email is being sent as plain text.

ETA: Also note that sending emails from the address that your form-filler typed into their form is fraught with issues. Most commercial mail servers will only send emails from a domain that they’re configured to handle. You’d be better to send the email from an address of your own, and set the “Reply-to” header to the form-fillers email address.

2 Likes

That is the problem, and the cause of this thread creation. what is the solution?

That is already been done. I am using mailgun SMTP to send emails.

If you open the documentation for wp_mail ( https://developer.wordpress.org/reference/functions/wp_mail/ ) and find that bit that I quoted, the mention of wp_mail_content_type is a link to another page in their documentation that shows how to specify HTML email contents.

OK, good. It’s just that your code (which I can’t quote because you’ve posted it as an image for some reason) is specifying that the email is sent from $email, which is the same value that you put into the body of your email along with the other form contents, which led me to presume that it’s the form-fillers email. I’m not familiar with mailgun, so maybe that deals with it.

1 Like

So if my understanding is not wrong. Putting this →

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

will fix the whole issue.

That seems to be the case, according to the notes on that page. Obviously you might need to have other html in the mail contents to make it display properly. Only way to be sure is to try it.