簡體   English   中英

Linux中電子郵件附件的標題問題

[英]Headers issue in email attachment in linux

<?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";


?>

當我通過Windows發送電子郵件而不是正常發送時,但是當我在Linux上運行腳本時,附件郵件將以純文本格式發送。

全文發送的電子郵件如下所示:

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

正是由於這種情況(跨平台兼容性),PHP常量PHP_EOL存在。 做這個:

<?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";


?>

編輯:

基於您評論中的新信息以及對代碼的另一番觀察,我想知道為什么Windows曾經將消息傳遞為html。 您在此處將內容類型指定為text / plain

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

您應該將其更改為簡單

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

這最有可能解決您的問題。

編輯2檢查我對主要答案所做的更改...

使用Linux樣式的換行符: \\n代替\\r\\n ,或者使用PHP_EOL常量。

使用PHP_EOL常量。 它根據特定平台替換行尾。

注意: mysql_query在PHP 5.5.0中已棄用,在PHP 7.0.0中已刪除。 而是應使用MySQLi或PDO_MySQL擴展名。此函數的替代方法包括:mysqli_query()或PDO :: query()

您基本上有兩種選擇可以實現此目的:

1.使用PDO(用於任何受支持的數據庫驅動程序):

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

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

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

2.使用MySQLi(對於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
}

請參閱如何防止PHP中的SQL注入?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM