Sending an HTML e-mail with PHP? - php

I'm trying to send an HTML email with PHP. The body currently looks like this:
$body = "Hello," . "<br />" . "<br />" . "We sent you an e-mail to confirm your account has been "
. "created." . "<br />" . "<br />" . "Thank you!";
Would that properly separate the lines? When I declare my headers, would I also need to use the same format for line breaking?
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: donotreply#webmaster.net";
Would I have to switch those new lines at the end of each header from "\r\n" to "<br />"?

Is better using library but to have an idea try this:
function sendemail( $to, $from, $subject, $title, $html, $cc = "", $bcc = "" )
{
$message = "\n";
$message .= "<!doctype html><html><head><title>$title</title></head><body>";
$message .= $html;
$message .= "</body></html>";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: $from \r\n";
$headers .= "Reply-To: $from \r\n";
$headers .= ( $cc != "" ) ? "Cc: $bcc \r\n" : "";
$headers .= ( $bcc != "" ) ? "Bcc: $bcc \r\n" : "";
return mail( $to, $subject, $message, $headers, "" );
}

Related

PHP: Why email not support html

I try to send an email from my web site, I received email but not support HTML.
Here is my code:-
////////
$headers = "From: $email\n" . "Reply-To: $email\n";
$headers .= "Register: Super market\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
//$header = "From: $email\n" . "Reply-To: $email\n";
$subject = "Register in Super market";
$email_to = EMAIL;
$emailMessage = "<b>NO :</b> " . $aid."\n";
$emailMessage .= "<b>Name :</b> " . $firstname.' '.$fathername.' '.$familyname. "\n";
$emailMessage .= "<b>Tell :</b> " . $tell . "\n\n";
$emailMessage .= "<b>Fax :</b> " . $fax . "\n\n";
$emailMessage .= "<b>Email :</b> " . $email . "\n\n";
//use php's mail function to send the email
#mail($email_to, $subject ,$emailMessage ,$headers );
Update:
Hi every one.
I set an question here and solved successful
But now i need to add some file as attachment.
So, I have 2 variable, That save the file upload into DB like this:-
$filenameword
$filenamezip
How can set this tow variable as file attachment in email inbox.
////////
$headers = "From: $email\n" . "Reply-To: $email\n"; // See the s at the end of $headers
$headers .= "Register: Super market\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
//$header = "From: $email\n" . "Reply-To: $email\n";
$subject = "Register in Super market";
$email_to = EMAIL;
$emailMessage = "<b>NO :</b> " . $aid."\n";
$emailMessage .= "<b>Name :</b> " . $firstname.' '.$fathername.' '.$familyname. "\n";
$emailMessage .= "<b>Tell :</b> " . $tell . "\n\n";
$emailMessage .= "<b>Fax :</b> " . $fax . "\n\n";
$emailMessage .= "<b>Email :</b> " . $email . "\n\n";
//use php's mail function to send the email
mail($email_to, $subject ,$emailMessage ,$headers );
You have a typo in your code.
You are assigning header strings to $headers but you're passing $header to the mail function.
Also please no # before functions. It's a bad practice.
////////
$headers = "From: $email\n" . "Reply-To: $email\n"; // See the s at the end of $headers
$headers .= "Register: Super market\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
//$header = "From: $email\n" . "Reply-To: $email\n";
$subject = "Register in Super market";
$email_to = EMAIL;
$emailMessage = "<b>NO :</b> " . $aid."\n";
$emailMessage .= "<b>Name :</b> " . $firstname.' '.$fathername.' '.$familyname. "\n";
$emailMessage .= "<b>Tell :</b> " . $tell . "\n\n";
$emailMessage .= "<b>Fax :</b> " . $fax . "\n\n";
$emailMessage .= "<b>Email :</b> " . $email . "\n\n";
//use php's mail function to send the email
mail($email_to, $subject ,$emailMessage ,$headers ); // See the s at the end of $headers

PHP processing form : unknown sender

I created a form with a processing PHP file. Everything works fine. I receive the mail but it's from "Unknow sender" in Gmail. Why please ?
I would like to see in my email box the name and the firstname of the person who fills the form. What's wrong in my code ?
<?php
if(isset($_POST) && isset($_POST['form3_firstname']) && isset($_POST['form3_name']) && isset($_POST['form3_email']) && isset($_POST['form3_telephone']) && isset($_POST['form3_message'])) {
extract($_POST);
if(!empty($form3_firstname) && !empty($form3_name) && !empty($form3_email) && !empty($form3_telephone) && !empty($form3_message)) {
$to = 'XXXXXX#gmail.com'; // My real email
$subject = 'Contact from the site';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
$headers .= 'Reply-To:'.$form3_email. "\r\n";
$message = '<html><body>';
$message .= '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>';
$message .= '<table>';
$message .= '<tr><td colspan="2"><p>MESSAGE</p></td></tr>';
$message .= '<tr><td>Firstname :</td><td>'.$form3_firstname.'</td></tr>';
$message .= '<tr><td>Name :</td><td>'.$form3_name.'</td></tr>';
$message .= '<tr><td>Email :</td><td>'.$form3_email.'</td></tr>';
$message .= '<tr><td>Telephone :</td><td>'.$form3_telephone.'</td></tr>';
$message .= '<tr><td>Message :</td<td>'.stripslashes($form3_message).'</td></tr>';
$message .= '</table>';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers)){
echo "Form sent";
} else {
echo "Form not sent";
}
} else {
echo "You have not filled in the field";
}
}
?>
Replace the $form3_name with $form3_email
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\r\n";
^^^^^^^^^^^^ //<----- Here
That's a name , not an email address , and that's the reason you get that error.
Also, you need to wrap them in tags <>
The right way..
$headers .= 'From:' .$form3_firstname. " ".'<'.$form3_email.'>'."\r\n";
replace "\r\n" with "\n" and your problem will be solved... and also put return-path in your headers...
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
$headers .= 'From: \''.$form3_firstname.'\' <'.$form3_firstname.'>\r\nReturn-Path: <'.$form3_firstname.'>
$headers .= 'From:' .$form3_firstname. " " .$form3_name. "\n";
$headers .= 'Reply-To:'.$form3_email. "\n";
please let me know if you want furtther guidance...
Because you do not supply an emailaddress in your "From:" header. There always needs to be an emailaddress.
Try something like:
$headers .= "From: $form3_firstname $form3_name <$form_email>\r\n";
Mind you, you may have to test and/or escape your form variables; for instance, check that there is no newline in there, otherwise your form might be abused for spamming.

Sending E-Mail in PHP

I tried almost everything. I just want to send e-mail.
My First Code:
<?php
$to = "yourgmail#gmail.com";
$subject = "HELLO";
$body = "HI!";
try {
mail($to, $subject, $body);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
It isn't working. No error message, not working.
My Second Code:
function send_mail('mygmail#gmail.com','yourgmail#gmail.com','HELLO','HI!')
{
$headers = '';
$headers .= "From: $from\n";
$headers .= "Reply-to: $from\n";
$headers .= "Return-Path: $from\n";
$headers .= "Message-ID: <" . md5(uniqid(time())) . "#" . $_SERVER['SERVER_NAME'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Date: " . date('r', time()) . "\n";
mail($to,$subject,$body,$headers);
}
It isn't working too.
Please help me :)
Does this work?
First part:
<?php
$to = "yourgmail#gmail.com";
$subject = "HELLO";
$body = "HI!";
$from = "mygmail#gmail.com";
send_mail($to,$from,$subject,$body);
?>
Second part:
<?php
function send_mail($to,$from,$subject,$body) {
$headers = '';
$headers .= "From: $from\n";
$headers .= "Reply-to: $from\n";
$headers .= "Return-Path: $from\n";
$headers .= "Message-ID: <" . md5(uniqid(time())) . "#" . $_SERVER['SERVER_NAME'] . ">\n";
$headers .= "MIME-Version: 1.0\n"; $headers .= "Date: " . date('r', time()) . "\n";
mail($to,$subject,$body,$headers);
}
?>
I'm not sure if you can send mail via your gmail using php mail function, never looked into it, this should work for name#yourdomain.com

PHP HTML mail is not working as I wanted

I have created reset password page, where used enters hes email, and PHP sends him back a the reset key. Mail works, but its going as plain text in my gmail account. I wanted it to go in HTML.
$subject = "Your password reset for {$config['site_name']}";
$message = "<html><body>";
$message .= "<p>Someone on" . $config['site_domain'] . "tried to reset your password.</p>";
$message .= "<p>Please click below link, if you want to reset your password.</p>";
$message .= "<p><a href='" . $config['site_url'] . "/forgot_password.php?key=" . $key . "'>" . $config['site_url'] . "/forgot_password.php?key=" . $key . "</a></p>";
$message .= "<p>Thank you,<br>The Admin - " . $config['site_url'] . " </p>";
$message .= "</body></html>";
// Create email headers
// To send HTML mail, the Content-type header must be set
$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: " . $config['site_name'] . " <noreply#" . $config['site_domain'] . "> \r\n";
$headers .= "X-Sender: <noreply#" . $config['site_domain'] . "> \r\n";
$headers .= "Reply-To: <noreply#" . $config['site_domain'] . "> \r\n";
mail($input['email'],$subject,$message,$headers);
//update pw_reset field into DATABASE
$stmt = $mysqli->prepare("UPDATE members SET pw_reset = ? WHERE email = ?");
$stmt->bind_param("ss", $key, $input['email']);
$stmt->execute();
$stmt->close();
You should structure your headers like this:
$headers = 'From: You <you#example.com>' . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Notice that the From is before the MIME and Content and only Content ends with "\r\n", the other are just "\n".
Source (saganwebdesign)
Try this function. returns true on success
function sendMail($email, $subject, $message)
{
$supportEmail = 'support#abc.com';
$from = 'Test Application';
$msg = $message;
$from = str_replace(' ', '-', $from);
$frm = $from.' <'.$supportEmail.'>';
preg_match("<(.*)#(.*\..*)>", $frm, $match);
///////////////////Headers/////////////////
$hdr='';
$hdr.='MIME-Version: 1.0'."\n";
$hdr.='content-type: text/html; charset=iso-8859-1'."\n";
$hdr.="From: {$frm}\n";
$hdr.="Reply-To: {$frm}\n";
$hdr.="Message-ID: <".time()."#{$match[2]}>\n";
$hdr.='X-Mailer: PHP v'.phpversion();
$x=#mail($email, $subject, $msg, $hdr);
if($x==0)
{
$email=str_replace('#','\#', $email);
$hdr=str_replace('#','\#',$hdr);
$x=#mail($email, $subject, $msg, $hdr);
}
return $x;
}

PHP Email Formatting Issue

I am sending form data through a .swf file into this PHP page.(see below) When the email is sent, if there is an attachment the body of the message will not appear but if there is not an attachment the body appears just fine. I know that all of the variable names are correct because all of the data has been passed from the swf file. Is there something in the way I have set up the email that makes it work that way? Please let me know.
<?php
$to=$_POST['toEmail'];
$subject=$_POST['subject'];
$body = $_POST['messaggio'];
$nome = $_POST['nome'];
$email = $_POST['email'];
$attach = $_POST['attach'];
$headers = "From: $nome<" . $email . ">\n";
if ($attach == 1) {
$tmp_name = $_FILES['Filedata']['tmp_name'];
$type = $_FILES['Filedata']['type'];
$name = $_FILES['Filedata']['name'];
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
$headers .= "Reply-To: <" . $email . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDARY_main_message\"\n";
$headers .= "X-Sender: $to <" . $to . ">\n";
$headers .= "Return-Path: <" . $email . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDARY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDARY_message_parts\"\n";
$message = "------=MIME_BOUNDARY_message_parts\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
$message .= $body . "\n";
$message .= "\n";
$message .= "------=MIME_BOUNDARY_message_parts--\n";
$message .= "\n";
$message .= "------=MIME_BOUNDARY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $name . "\"\n\n";
$message .= $data;
$message .= "\n";
$message .= "------=MIME_BOUNDARY_main_message--\n";
mail($to, $subject, $message, $headers);
}
} else {
if(mail($to, $subject, $body, $headers)) {
echo "ok=1";
}
}
?>
Building MIME messages, especially with attachments, is painful. You'd be better off using something like PHPMailer, which will handle the whole business for you automatically... You just have to provide the content.
Beyond that, you're slurping the attachments into memory. How big are they? Are you exceeding the script's memory_limit?

Categories