PHP: Why email not support html - php

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

Related

redirect after form submission PHP

I am trying to redirect the user after they enter their email. I am able to send the email with the attachment but not able to redirect the user to thankyou.html. If I uncomment the hardcoded value and use that the redirect is working fine. Not sure what I am doing wrong. Any help is appreciated.
Here is my php file:
<?php
//form variables
$to = $_POST['userEmail'];
$user_name = $_POST['userName'];
$subject = $_POST['subject'];
$fname = $_POST['filename'];
$mailcopyfile = $_POST['backupfile'];
//$subject = 'Templage from us';
//$fname = 'somefile.xlsx';
//$mailcopyfile = 'somefile.csv';
// common for all landing page emails
$from_name = 'MyCompany LLC';
$from_mail = 'admin#mycompany.com';
$now = date('Y-m-d H:i:s');
$num = md5(time());
$message = "<html><body>";
$message .= "<span>Dear " . $user_name . ",</span>";
$message .= "<table><tr><td>Thank you for your interest in our company.</td></tr>";
$message .= "<tr><td>Here is the template you requested.</td></tr>";
$message .= "<tr><td> </td></tr>";
$message .= "<tr><td>Regards,</td></tr>";
$message .= "<tr><td>Our Team</td></tr>";
$message .= "</table>";
$message .= '<img src="http://mycompany.com/Images/logo.jpg" alt="MyCompany LLC">';
$message .= "</body></html>";
//File open
$fp = fopen($fname, "rb");
$upload_size=filesize($fname);
$file = fread($fp, $upload_size);
$file = chunk_split(base64_encode($file));
fclose($fp);
$upload_type=filetype($fname);
//Normal headers
$headers = "From: ".$from_name." <".$from_mail.">\r\n";
$headers .= "Reply-To: ".$from_mail."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--" .$num. "\r\n";
// With message
$headers .= "Content-Type: multipart/alternative; boundary=";
$headers .= $num. "\r\n";
$headers .= "--" .$num. "\r\n";
$headers .= "Content-Type: text/plain; charset=iso-8859-1\r\nContent-Transfer-Encoding: 7bit\r\n";
$headers .= "Thanks for your interest in our company. Here is the template you requested. Thanks Our Team\r\n";
$headers .= "--" .$num. "\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= $message."\r\n\r\n";
$headers .= "--".$num."\r\n";
// Attachment headers
$headers .= "Content-Type:".$upload_type."; ";
$headers .= 'name="'.$fname.'"'. "\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= 'filename="'.basename($fname).'"' . "\r\n";
$headers .= $file."\r\n";
$headers .= "--".$num."--";
//send the email
$mail_sent = #mail( $_POST['userEmail'], $subject, "", $headers );
//$mail_sent = #mail( 'myemail#hotmail.com', $subject, "", $headers );
if($mail_sent){
$fp = fopen($mailcopyfile, "a");
fputs($fp, $now . "\t" . $user_name . "\t" . $_POST['userEmail'] . "\t\n" );
//fputs($fp, $now . "\t" . $user_name . "\t" . 'myemail#hotmail.com' . "\t\n" );
fclose($fp);
header('Location: Thankyou.html', true);
exit;
}
?>

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

Simple script to send email?

I use the code to send email:
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
But I got:
sendmail: fatal: chdir /Library/Server/Mail/Data/spool: No such file or directory
And I don't know how to add attachment and importantly I need to specify the Content-Type for attachment, can anyone help me?
Thanks!
<?php
$to = "someone#example.com";
$subject = "Test mail";
$from = "someonelse#example.com <someonelse#example.com>";
$message = "Hello! This is a simple email message.";
// let's say you want to email a comma-separated-values
$tofile = "col1,col2,col3\n";
$tofile .= "val1,val1,val1\n";
$filename = "filename.csv";
$random_hash = md5(date('r', time()));
$attachment = chunk_split(base64_encode($tofile));
$headers = "From: " . $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-MSMail-Priority: Normal\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$body = "--PHP-mixed-".$random_hash."\r\n";
$body .= "Content-Type: text/plain; charset=\"UTF-8\"\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n";
$body .= $message;
$body .= "\r\n\r\n";
$body .= "\r\n--PHP-mixed-" . $random_hash . "\r\n\r\n";
$body .= "--PHP-mixed-".$random_hash."\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Type: text/comma-separated-values; name=\"" . $filename . "\" charset=\"UTF-8\"\r\n";
$body .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
$body .= $attachment;
$body .= "\r\n--PHP-mixed-".$random_hash."--";
mail($to, $subject, $body, $headers);
?>

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?

php email not reading html

For some reason, the html fails to render in gmail, but renders in hotmail.
Its vital that gmail reads the html, so I wonder which changes I should make to this header.
$from = "info#email.co";
$headers = "From: bob at info.co <" .($from) . ">\n";
$headers .= "Reply-To: ".($from) . "\n";
$headers .= "Return-Path: ".($from) . "\n";;
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-Mailer: PHP". phpversion() ."\n";
The message starts of as:
$message = '<html><body>';
$message .= "<p>";
$message .= "Hi $clean_fullName, <br><br>";
$message .= " Well, I've looked at what you shared with me and I'm delighted to include my personal learning suggestions that I hope will help you achieve your startup goals.";
$message .= "<br><br>";
$message .= "If they aren't quite what you're looking for, I take criticism better than most Entrepreneur Wizards
so please let me know by responding to this email and I'll take another look for you.";
$message .= "<br><br>";
$message .= "
$message .="<br><br>Otherwise, happy learning!<br><br>";
$message .= "<b>Total Learning time: </b>";
// create an array of all the duration
$counter = array();
foreach($data as $item) {
// add each duration item to the array after every iteration
array_push($counter, "{$item['duration']}");
}
//record and display the result to the user
$message .= array_sum($counter);
$message .= " hours <br><br>";
foreach($data as $item) {
$message .= "<b>
&#10139<a style='color:#FF6400; text-decoration: none' href='{$item['link']}'>{$item['title']}</a></b><br>";
$message .= "Format: {$item['format']} <br>";
$message .= "Cost: ${$item['costs']} <br>";
$message .= "Estimated Duration: {$item['duration']} hours<br>";
$message .= "<br>";
}
$message .= " If you have any questions, do not hesitate to reach out to us. <br><br>";
$message .= "</p>";
$message .= '</body></html>';
mail
mail($to,$subject,$message,$headers);
I am still learning :)
but this should work
$from = "info#email.co";
$headers .= 'From: bob at info.co <$from>' . "\r\n";
$headers .= 'Reply-To: <$from> ' . "\r\n";
$headers .= 'Return-Path: <$from>' . "\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";
Answer from comments
$message = '<html><body>';
u should do like this
$message .= '<html>'. "\r\n";
$message .= '<body>' . "\r\n";
You have too much errors in writing missing dot semi etc
as i see your html tag wasn't open at all.
there is lot options to write this template
1.
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// 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: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
2.
$message.= 'blasldl asdas d asdas' . "\r\n";
$message.= ' sdfadasdasd dsad' . "\r\n";
U can change "\r\n"with $rn = "\r\n"; and use it fast as $rn
$message.= 'blasldl asdas d asdas' . $rn;
$message.= ' sdfadasdasd dsad' . $rn;
u can try those solutions and tell me whats happening .
and dont place all email under<p></p>

Categories