Phpmailer html in body

Send phpmailer email with html code

I made a function that sends mails, but they interpret html code. How can I make it to send exact html code in the body? For instance, the user should receive text and not bolded ‘strong’.

function sendmail($dest,$subj ,$htmlmessage,$textmessage) < $Mail = new PHPMailer( true ); try< $Mail->IsSMTP(); // Use SMTP $Mail->SMTPAuth = TRUE; // enable SMTP authentication //$Mail->SMTPDebug = 2; // 2 to enable SMTP debug information $Mail->Host = "smtp.gmail.com"; // Sets SMTP server $Mail->Username = 'mymail@gmail.com'; // SMTP account username $Mail->Password = 'mypassword'; // SMTP account password $Mail->SMTPSecure = "ssl"; //Secure conection $Mail->Port = 465; // set the SMTP port $Mail->SetFrom ( 'mymail@gmail.com', 'def' ); $Mail->FromName = 'myname'; //$Mail->addReplyTo( 'mymail@gmail.com', 'Reply here' ); $Mail->addAddress($dest, 'to' ); // To: $Mail->isHTML( false ); $Mail->Subject=$subj; $Mail->Body=$htmlmessage; $Mail->AltBody=$textmessage; $Mail->Send(); > catch ( phpmailerException $e ) < file_put_contents( 'Mailerrors.txt', $e->errorMessage() , FILE_APPEND ); die( "Problem with emailing." ); > > 
$dest='sendto@gmail.com'; $subj='hello'; $htmlmessage='this is the bodyasd'; $textmessage='this is the body'; sendmail($dest,$subj, $htmlmessage,$textmessage); 

2 Answers 2

There are several possible solutions to this.

To send only plain text, just do this:

and do not put anything in $mail->AltBody . That way the message will be sent as plain text, even if it contains HTML markup.

If you only want part of your message body to escape HTML rendering, you can either use htmlspecialchars or wrap that portion of your markup in tags.

Applying htmlspecialchars to the whole message body is a bit pointless as it achieves a similar looking result to isHTML(false) , but far less efficiently.

Читайте также:  Шрифты для страниц html

Источник

inserting HTML POST form field data into PHPmailer email body

I have tried every hint I have found across the web and here but what I cannot achieve is for the sent email to contain the form data in the html formatted message. The mailing function is working and I get the email properly. The form is basically:

And using the latest phpmailer from google the action script is as follows. I have the emailing function working through gmail, I am just not sure what to put where to pull the data from the form and put it into the email body. The code that is pasted in the body now is that we used to use before switching to phpmailer. Thank you!

IsSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp.gmail.com'; // Specify main and backup server $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'xxxxxxxxxxxxxxxx'; // SMTP username $mail->Password = 'xxxxxxxxx'; // SMTP password $mail->SMTPSecure = 'ssl'; // Enable encryption, 'ssl' also accepted $mail->Port = 465; // set the SMTP port for the GMAIL server $mail->From = 'xxxxxxxxxxxxxx'; $mail->FromName = 'NPLH'; $mail->AddAddress('xxxxxxxxxxxxxxxxxxxxx'); // Name is optional $mail->AddCC(''); $mail->AddBCC(''); $mail->WordWrap = 50; // Set word wrap to 50 characters $mail->IsHTML(true); // Set email format to HTML $mail->Subject = 'xxxxxxxxxxxxxxx'; $mail->Body = ' 

$first_name $last_name, $license_type

'; $mail->AltBody = ''; if(!$mail->Send()) < echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; exit; > echo 'Thank you, your message has been sent!'; ?>

Источник

PHPmailer sending HTML CODE

enter image description here

Once the email is sent i see the actual HTML code instead of the contents please check below **Not sure what is the issue **

Читайте также:  Vectors in java with examples

D’oh! Right. Body is an attribute, not a method. IN any case, here’s the ‘official’ tutorial on how to send an html mail: phpmailer.worxware.com/index.php?pg=tutorial#4

7 Answers 7

Calling the isHTML() method after the instance Body property (I mean $mail->Body ) has been set solved the problem for me:

$mail->Subject = $Subject; $mail->Body = $Body; $mail->IsHTML(true); // Body has been set. 

That is correct but you should never call $mail->ClearCustomHeaders(); after $mail->IsHTML(true) . ClearCustomHeader gets rid of your email HTML formatting and instead recipient will receive HTML source code.

There is msgHTML() method, which also call IsHTML() .

The name IsHTML is confusing.

/** * Create a message from an HTML string. * Automatically makes modifications for inline images and backgrounds * and creates a plain-text version by converting the HTML. * Overwrites any existing values in $this->Body and $this->AltBody * @access public * @param string $message HTML message string * @param string $basedir baseline directory for path * @param bool $advanced Whether to use the advanced HTML to text converter * @return string $message */ public function msgHTML($message, $basedir = '', $advanced = false) 

Источник

How to send html table in email body in php?

I am sending dynamically creating html table in email body but in the email I receive the html code instead of displaying a table. Please help me. Here is my code.

'; $output .= ''; while($row = mysql_fetch_array($sql)) < $data = explode('"', $query['data']); $output .= ''; > $output .= '
AuthorNode TitleNode SummaryNode BodyEdit this nodeReport AbuseGroup
'; if($row['created'] >= $strdate && $row['created'] < $enddate) < $output .= '' . $query['name'] . ''; $output .= '' . $row['title'] . ' (New)'; > else < $output .= '' . $query['name'] . ''; $output .= '' . $row['title'] . ' (Updated)'; > //$output .= ''; $output .= $row['teaser'] . ''; if($row['field_provcomp_level_value'] == 0 || $row['field_provcomp_level_value'] == 1)
< $output .= $row['body'] . '
'; > else < $output .= ''; > $output .= 'Abuse'; $output .= 'Edit'; $query = mysql_fetch_array(mysql_query("SELECT title from node Where type = 'groupnode' AND nid = '" . $row['nid'] . "'")); $output .= $query['title'] . '
'; print $output; $to = 'hmdnawaz@gmail.com'; $headers = "From Ahmad \r\n"; //$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n"; //$headers .= "CC: susan@example.com\r\n"; $headers .= 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n"; $subject = 'Email report'; mail($to, $subject, $output, $headers); if(mail) < echo 'Email sent'; >else < echo 'Error sending email'; >?>

Источник

Оцените статью