0
<?php
include 'include/function.php';

//$random_hash = md5(time());
$User=new User();
session_start();

$id=$_POST['id'];
    //$date=$_POST['date'];
    $expected_date=$_POST["expected_date"];

    $comname=$_POST['comname'];
    $type=$_POST["type"];
    $name=$_POST["name"];
    $mime=$_POST["mime"];
    $size=$_POST["size"];
    $file1=$_POST["path"];

    //$comname=$_POST["comname"];
    $remark=$_POST["remark"];

    $other_detail=$_POST['other_detail'];
    $remark=$_POST["remark"];
    //$last_change_time=$row['last_change_time'];
    $email1=$_POST['email1'];
    $email2=$_POST['email2'];
    $email3=$_POST['email3'];
    $email4=$_POST['email4'];



    $email5=$_POST['email5'];
    $email6=$_POST['email6'];
    $username=$_SESSION["username"];
    //$username=$row['username'];



$sql=mysql_query("update  depository set expected_date='$expected_date',comname='$comname',last_change_username='$username',type='$type',name='$name',mime='$mime',size='$size',path='$file1',other_detail='$other_detail',remark='$remark',email1='$email1',email2='$email2',email3='$email3',email4='$email4',email5='$email5',email6='$email6'where id='$id'");






$htmlbody = " Message successfully send ok";


//$to .= msh@solnfinite.com;
//Recipient Email Address muktidar@gmail.com
$to = $email1. ', ';
$to .= $email3. ', ';
$to .= $email2. ', ';
$to .= "mukeshbpatidar@gmail.com"; 
$subject = 'Depositiory Detail From Solution Infinite'; //Email Subject

$headers = "From: ticketing@soite.com\r\nReply-To: mukesh@solite.com";

$random_hash = md5(date('r', time()));

$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

$attachment = chunk_split(base64_encode(file_get_contents($file1))); // Set your file path here

//define the body of the message.

$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message = "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";

//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";

//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/pdf; name=\"$name\"\r\n"."Content-Transfer-Encoding: base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";

//send the email
$mail = mail( $to, $subject , $message, $headers );

echo $mail ? "Mail sent" : "Mail failed";


?>

When I send email through windows than send properly but when I run script on linux than attachment mail send in plain text.

Email sent in full text shown in below:

PHP-mixed 83d06f048e070e4cfc0684d0e98f71db    
Content-Type: application/jpg; name="tkt.jpg"    
Content-Transfer-Encoding: base64    
Content-Disposition: attachment


/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcG    
BwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwM    
DAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAMABVYDASIA    
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
manlio
  • 18,345
  • 14
  • 76
  • 126

3 Answers3

1

It is because of situations like this (cross-platform compatibility) that the PHP constant PHP_EOL exists. Do this:

<?php
    ....
    $eol = PHP_EOL;
    $headers = "From: ticketing@soite.com".$eol."Reply-To: mukesh@solite.com";

    $random_hash = md5(date('r', time()));

    $headers .= $eol."Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

    $attachment = chunk_split(base64_encode(file_get_contents($file1))); // Set your file path here

    //define the body of the message.

    $message = "--PHP-mixed-$random_hash".$eol."Content-Type: text/html; charset=\"iso-8859-1\"".$eol."Content-Transfer-Encoding: 7bit".$eol.$eol;

    //Insert the html message.
    $message .= $htmlbody.$eol.$eol."--PHP-mixed-$random_hash".$eol;

    //include attachment
    $message .= "Content-Type: application/pdf; name=\"$name\"".$eol."Content-Transfer-Encoding: base64".$eol."Content-Disposition: attachment; filename=\"".$name."\"".$eol;
    $message .= $attachment;
    $message .= $eol."--PHP-mixed-$random_hash--";

    //send the email
    $mail = mail( $to, $subject , $message, $headers );

    echo $mail ? "Mail sent" : "Mail failed";


?>

EDIT:

Based on new information in your comment, and another look at the code, I wonder why windows ever delivered the message as html. You specified the content-type as text/plain here

$message = "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n"

You should change that to simply

$message = "--PHP-alt-$random_hash".$eol."Content-type:text/html; charset=iso-8859-1".$eol."Content-Transfer-Encoding: 7bit".$eol.$eol;

This should most likely resolve your problem.

EDIT 2 Check changes I made to the main answer...

kennasoft
  • 1,595
  • 1
  • 14
  • 26
  • Oops! Seems I'm late to the party. Just do what @beerwin says. – kennasoft Aug 31 '16 at 09:33
  • email sent with attachment properly but html content not sent $htmlbody value not sent in mail @beerwin , kennasoft – Black_Panthor Sep 01 '16 at 05:30
  • yes email send with attachment but the problem is $htmlbody content not sent in email @kennasoft – Black_Panthor Sep 01 '16 at 09:12
  • I see you have more `$eol` than is necessary. I'll update the answer to trim your code a bit – kennasoft Sep 01 '16 at 09:16
  • Page not working if i will changes made as par above code The 48.48.48.248 page isn’t working 48.48.48.248 is currently unable to handle this request. – Black_Panthor Sep 01 '16 at 09:58
  • Sorry, missing semicolon. If it still does not work, then I may need to test it locally, and get back to you – kennasoft Sep 01 '16 at 10:17
  • Same problem occur if I will run Above code than all content goes into html format and no attachment in email @kennasoft – Black_Panthor Sep 01 '16 at 10:29
  • Why are you using `PHP-alt-$random_hash`, when you defined your boundary as `PHP-mixed-$random_hash`? Any reason? – kennasoft Sep 01 '16 at 12:15
  • No reason @kennasoft first I would like to send email attachment with html content – Black_Panthor Sep 02 '16 at 04:55
  • change it all to `PHP-mixed-$random_hash` and see if that helps. Those boundaries are what differentiates the parts of the email and tells the client what part is the body and what part is attachment. – kennasoft Sep 02 '16 at 06:31
  • Yes...... Finally done @kennasoft it is possible to send html content through a variable name Like $companyname actually I am new in php – Black_Panthor Sep 02 '16 at 06:43
  • If this resolves your problem, then mark the answer as accepted. You can then proceed to ask a separate question about email templating. – kennasoft Sep 02 '16 at 07:11
  • There should be a check-mark on the left of the answer, just below the zero with up- and down- arrows. Click on it and we're done :). You can paste a link to your new question here – kennasoft Sep 02 '16 at 08:17
  • ops ... when I am sending email with attachment is properly send on gmail with content and attachment but in outlook shows one text file and another one is attach file but attach file is corrupted and text file shows content of html I am stuck in last few days on its... @kennasoft – Black_Panthor Sep 03 '16 at 06:43
0

Use Linux style newline characters: \n instead of \r\n, or, use PHP_EOL constant.

beerwin
  • 9,813
  • 6
  • 42
  • 57
  • Instead of calling the form variables from within --- $message .= $htmlbody;. $message .= $comname; Hard code the variables into the $message.= Like-- $message .= "Content-Transfer-Encoding: 7bit\n" . $htmlbody. "\n" . $comname. "\n"; but still the problem is same – Black_Panthor Sep 01 '16 at 08:59
0

Use PHP_EOL constant. It substitutes end of line based on platform specific.

Note: mysql_query was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.Alternatives to this function include: mysqli_query() or PDO::query()

You basically have two options to achieve this:

1.Using PDO (for any supported database driver):

$stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name');

$stmt->execute(array('name' => $name));

foreach ($stmt as $row) {
    // do something with $row
}

2.Using MySQLi (for MySQL):

$stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?');
$stmt->bind_param('s', $name);

$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // do something with $row
}

Please refer How can I prevent SQL-injection in PHP?

Community
  • 1
  • 1
Tamil
  • 1,193
  • 9
  • 24