Add HTML formatting in phpmailer - php

I am using PHP mailer to send an online form directly to email. I want to edit the form like this:
$message = '<p>The following request was sent from: '</p>;
$message .= '<p>Name: '.$name.'</p><br />';
$message .= '<p>Phone: '.$phone.'</p><br />';
$message .= '<p>Email: '.$email.'</p><br />';
However, in the email I receive back, I get the <p>, <br />, etc. Not the formatting I want, but the actual HTML. It has always worked for me, then I realized I was using the stock PHP mail function. not sure if PHP mailer has different parameters OR if I just missing small here?
Where would I set the headers for text/html?
$mail = new PHPMailer();
$mail -> IsSMTP();
$mail -> Host = "---";
$mail -> Port = 25;
$mail -> SMTPAuth = false;
$mail -> Username = EMAIL_USER;
$mail -> Password = EMAIL_PASS;
$mail -> FromName = $from_name;
$mail -> From = $from;
$mail -> Subject = $subject;
$mail -> Body = $message;
$mail -> AddAddress("---");
$result = $mail -> Send();

In PHP mailer, you need to set below
$mail->IsHTML(true);
Note: $mail means your PHPMailer object.
Reference Link: PHPMailer

By default, the PHP mail() function is text/plain.
In your mail() headers, change the content-type to text/html and try.
Example:
<?php
$body = "<html>\n";
$body .= "<body style=\"font-family:Verdana, Verdana, Geneva, sans-serif; font-size:12px; color:#666666;\">\n";
$body = $message;
$body .= "</body>\n";
$body .= "</html>\n";
$headers = "From: My site<noreply#example.com>\r\n";
$headers .= "Reply-To: info#example.com\r\n";
$headers .= "Return-Path: info#example.com\r\n";
$headers .= "X-Mailer: Drupal\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
return mail($recipient, $subject, $message, $headers);
?>
PHP Mailer:
You need to set IsHTML(true):
$mail->IsHTML(true);

Related

Send message via php

I have problem with sending messages via php from host email to someone email with script. Like for registration code etc...
Here is my code:
$to = "$email";
$from = "support#webiste.com";
$subject = 'Web Site';
$message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>WebSite</title></head><body style="padding: 20px;"><h1>WEBSITE</h1><p>Hello '.$name.' '.$lname.'</p><h3>Click the link below to activate your account: </h3><p><a style="text-decoration: none;" href="http://www.website.com/MB/activation.php?id='.$rewr.'&re='.$re.'">Click Here</a></p></body></html>';
$headers = "From: $from\n";
$headers = "MIME-Version: 1.0\n";
$headers = "Content-type: text/html; charset=iso-8859-1\n";
if(mail($to, $subject, $message, $headers)){
echo "Sent";}else echo "failed";
I got failed message every time
sendmail is server dependent a better solution would be to use Pear mail.
include_once 'Mail.php';
include_once 'Mail/mime.php' ;
$recipients = '<' . $this->to . '>';
$text = $this->message['text'];
$html = $this->message['html'];
$crlf = "\n";
$hdrs = array('From' => $this->from,'To' => $this->to,'Subject' => $this->subject);
$mime = new Mail_mime(array('eol' => $crlf));
if(!empty($text)){$mime->setTXTBody($text);}
if(!empty($html)){$mime->setHTMLBody($html);}
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$params["host"] = MailSMTP;
$params["port"] = MailPort;
$params["auth"] = true;
$params["username"] = $username;
$params["password"] = $password;
$mail =& Mail::factory('smtp',$params);
$value = $mail->send($recipients,$hdrs, $body);
if(PEAR::isError($value)) {
return false;
}
Above is code that I use myself. It will send multi-part mail ie HTML and plain text so they can read it on any device and it uses Pear mail and also send to 1 or multiple recipients. Pear is an extension of PHP.

php mail() function, headers issue, can not send an email

I know this is really common issue, but I have problem with headers. This function send email only when I send mail with $header2. In other cases I get fail echo. Does not working with $headers.
I don't know why it is happening, also it doesn't work with php.net default headers below:
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Complete function:
function sendMail() {
$to = "mymail#gmail.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
$header2 = "From:abc#somedomain.com \r\n";
$header2 = "Cc:afgh#somedomain.com \r\n";
$header2 .= "MIME-Version: 1.0\r\n";
$header2 .= "Content-type: text/html\r\n";
$retval = mail ($to, $subject, $message, $headers);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
}
Firstly, just get rid of this entire code block since there is no reference to your usage of the $headers2 variable. Do go over my answer in its entirety.
$header2 = "From:abc#somedomain.com \r\n";
$header2 = "Cc:afgh#somedomain.com \r\n";
// ^ BROKEN concatenate
$header2 .= "MIME-Version: 1.0\r\n";
$header2 .= "Content-type: text/html\r\n";
PHP is going over your entire code, regardless. And it contains a broken concatenate.
However, it's unclear as to what you want to do here and have obviously taken examples from the manual on mail() http://php.net/manual/en/function.mail.php then just blindly adding stuff in there.
Sidenote: The To: is already pre-determined as to where it should be sent to, so the use of To: in the headers isn't required.
function sendMail() {
$to = "mymail#gmail.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= "From:abc#somedomain.com \r\n";
$headers .= "Cc:afgh#somedomain.com \r\n";
$retval = mail ($to, $subject, $message, $headers);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
}
and if the goal here is to send 2 seperate mails, then you will need to use 2 seperate mail() functions and 2 different header variables.
Something to which you can consult, as per an answer I once gave for another question:
https://stackoverflow.com/a/18382062/1415724
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
Please stop using the php mail() function! My advice will be to use PHPMailer and sending the emails via SMTP.
Here is a small code snippet using PHPmailer:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtpserver.com'; // Specify main SMTP server. If you dont have one us GMAL or mandrill
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#youremail.com'; // SMTP username
$mail->Password = 'pass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#youremail.com', 'Mailer');
$mail->addAddress('joe#youremail.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#youremail.com'); // Name is optional
$mail->addReplyTo('info#youremail.com', 'Information');
$mail->addCC('cc#youremail.com');
$mail->addBCC('bcc#youremail.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

how to send php mail one copy to admin and reply thanks message to subscriber?

I have following php code for send mail, it works fine. it's contact form, the information send to admin and reply thanks message to subscriber.
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$sub=$_POST['subject'];
$describe=$_POST['describe'];
$mail -> From = "karthik#xfacttechnologies.com";
$mail -> FromName = "Vignesh Agency";
$mail -> AddAddress ("karthik#xfacttechnologies.com");
$mail -> headers = "From: vigneshagency\r\n";
$mail -> headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$mail -> headers .= "MIME-Version: 1.0\r\n";
$mail -> headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mail -> Subject = "Vignesh Agency";
$mail -> IsHTML (true);
$mail -> Body .= '<html><body>';
$mail -> Body .='<table style="width:60%;border:1px solid #eee;" border="1" bordercolor="#eee" cellpadding="6" cellspacing="0">';
$mail -> Body .='<tr><th colspan="2" style="background-color:#7DA3B3; color: white;"><h2>Customer Details</h2></th></tr>';
$mail -> Body .="<tr><td><b>Name </b></td><td>".$name."</td></tr>";
$mail -> Body .="<tr><td><b>E-mail </b></td><td>".$email."</td></tr>";
$mail -> Body .="<tr><td><b>Phone</b></td><td>".$phone."</td></tr>";
$mail -> Body .="<tr><td><b>Purpose </b></td><td>".$sub."</td></tr>";
$mail -> Body .="<tr><td><b>Message </b></td><td>".$describe."</td></tr> </table>";
$mail -> Body .= "</body></html>";
if(!$mail->Send())
echo "Error:" . $mail->ErrorInfo;
else
echo "<script>window.location = 'http://vigneshagency.com/contact.html'</script>";
exit();
Just send reply message to subscriber like "Thank you for visiting us"
Finally i find the solution.How to send Automated mail to Subscriber
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$sub=$_POST['subject'];
$describe=$_POST['describe'];
$mail -> From = "******#gmail.com";
$mail -> FromName = "abvdAgency";
$mail -> AddAddress ("******#gmail.com");
$mail -> headers = "From: abcd agency\r\n";
$mail -> headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$mail -> headers .= "MIME-Version: 1.0\r\n";
$mail -> headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mail -> Subject = "abcd Agency";
$mail -> IsHTML (true);
$mail -> Body .= '<html><body>';
$mail -> Body .='<table style="width:60%;border:1px solid #eee;" border="1" bordercolor="#eee" cellpadding="6" cellspacing="0">';
$mail -> Body .='<tr><th colspan="2" style="background-color:#7DA3B3; color: white;"><h2>Customer Details</h2></th></tr>';
$mail -> Body .="<tr><td><b>Name </b></td><td>".$name."</td></tr>";
$mail -> Body .="<tr><td><b>E-mail </b></td><td>".$email."</td></tr>";
$mail -> Body .="<tr><td><b>Phone</b></td><td>".$phone."</td></tr>";
$mail -> Body .="<tr><td><b>Purpose </b></td><td>".$sub."</td></tr>";
$mail -> Body .="<tr><td><b>Message </b></td><td>".$describe."</td></tr> </table>";
$mail -> Body .= "</body></html>";
if(!$mail->Send())
{
echo "Error:" . $mail->ErrorInfo;
}
else
{
// To send automated reply mail
$autoemail = new PHPMailer();
$autoemail->From = "*****#gmail.com";
$autoemail->FromName = "ABCD Agency";
$autoemail->AddAddress($email, $mail->FromName);
$autoemail->Subject = "Autorepsonse: We received your submission";
$autoemail->Body = "We received your submission. We will contact you soon ...";
$autoemail->Send();
echo "<script>window.location = 'http://vigneshagency.com/contact.html'</script>";
exit();
}

I can't send an email php to Hotmail?

Here's my code:
<?php
$to = 'test#hotmail.com';
$subject = 'reservation hotel n';
$msg ='ok';
// Make sure to escape quotes
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: hôtel <reservation#hotel.com' . "\r\n";
mail($to, $subject, $msg, $headers);
?>
It worked for Gmail, Yahoo, GMX ...but it didn't work for Hotmail/Live/MSN.
Because it worked for Gmail, I can assume that it has nothing to do with my server, right?
I also tried it with just:
http://www.microsoft.com/mscorp/safety/content/technologies/senderid/wizard/Default.aspx
System Maintenance in progress. Please try again later.
think's for help
Check it
<?php
$to = "$email";
$subject = "Welcome to";
$message = " Hi $username,<br /><br />
Thank you for signing up with us.<br />
Thanks <br />";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <test#gmail.com>' . "\r\n";
$mail=mail($to,$subject,$message,$headers);
?>
problem solved i change with php mailer
require_once "class.phpmailer.php";
require_once "class.smtp.php";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "**#gmail.com";
$mail->Password = "**";
$mail->From = "***";
$mail->FromName = "Hôtel **";
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.
//Caractéristiques du message
$mail->CharSet = 'utf-8';
$mail->ContentType = 'text/plain';
$mail->Encoding = '8bit';
$mail->Subject = "**";
$mail->Body = "okkk";
$mail->WordWrap = 0;
$mail->AddAddress("**#hotmail.com", "nom");
$mail->Send();
I think that you need one of these for it to work:
Sender ID Framework SPF Record Wizard
It should solve your problem as Hotmail wants this for safety.

send email in php with attachment

i have use this code in php mail send with attachment. I am using the Apache http server with wamp.
<?php $to = 'santosh_091020#rediffmail.com';
// Declaring the necessary variables for mail sending :
$subject = 'Testing sendmail.exe';
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$mob = $_REQUEST['mob'] ;
$msg = $_REQUEST['msg'] ;
$message = ' santosh enquire \n\nName: '.$name.' \n Email:'. $email.' \n Mobile:'. $mob.' \n Message: '.$msg.' \n ';
// Declaration of the attributes for attachment.
$upload_name=$_FILES["upload"]["name"];
$upload_type=$_FILES["upload"]["type"];
$upload_size=$_FILES["upload"]["size"];
$upload_temp=$_FILES["upload"]["tmp_name"];
$fp = fopen($upload_temp, "rb");
$file = fread($fp, $upload_size);
fclose($fp);
$file = chunk_split(base64_encode($file));
$num = md5(time());
// Defining the contents of the header.
$headers = "From: ".$email. "\r\n" . "CC: santosh#creativecrows.com" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
//$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
$headers .= "Content-Type:".$upload_type." ";
$headers .= "name=\"".$upload_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$upload_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
//$data = chunk_split(base64_encode($data));
// sending the mail.
if(mail($to, $subject, $message, $headers))
{
echo "Email sent";
send();
}
else
{
echo "Email sending failed";
}
//send mail in client
function send()
{
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$to= trim($email);
$subject = 'thanx';
$message = '<html><head><title>HTML email</title></head>
<body style="background-color:#000099;"><p style="color:#000099;">This email contains HTML Tags!</p><table><tr><th>Firstname</th><th>Lastname</th></tr><tr><td>John</td><td>Doe</td></tr></table></body></html>';$headers1 = 'MIME-Version: 1.0' . "\r\n";
$headers1.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers1.= "From: ".$email;
if(mail($to, $subject, $message,$headers1))
{
echo "Email sent";
}
else
{
echo "Email sending failed";
echo $to;
echo $subject;
echo $message;
}
}
?>
but i got the following error;
Delivery to the following recipient failed permanently:
1.0#localhost
Technical details of permanent failure:
DNS Error: Domain name not found.
please help me. Thnx in advance.
Check this URL,
seems your header info is not absolute
http://webcheatsheet.com/php/send_email_text_html_attachment.php
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup server
$mail->Port = '';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = '';
$mail->FromName = '';
$mail->AddAddress($row['client_email'], ''); // Add a recipient
$mail->AddReplyTo('', 'Information');
$mail->AddCC('cc#example.com');
$mail->AddBCC('bcc#example.com');
$mail->WordWrap = 50;
// Set word wrap to 50 characters
$mail->AddAttachment('kurtacompany/techreporting/upload/"'.$row['file1'].'"'); // Add attachments
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = '';
$mail->Body = '<p>type whatever you want</p>';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';

Categories