Headers 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
使用 Linux 样式的换行符:\n
而不是 \r\n
,或者,使用 PHP_EOL
常量。
正是由于这种情况(跨平台兼容性),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
检查我对主要答案所做的更改...
Use PHP_EOL constant. It substitutes end of line based on platform
specific.
注意: mysql_query 在 PHP 5.5.0 中被弃用,在 PHP 7.0.0 中被移除。相反,此函数的 MySQLi 或 PDO_MySQL 扩展名应为 used.Alternatives 包括:mysqli_query() 或 PDO::query()
你基本上有两种选择来实现这一点:
1.Using PDO(对于任何支持的数据库驱动程序):
$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(对于 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
}
请参考How can I prevent SQL-injection in PHP?
<?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
使用 Linux 样式的换行符:\n
而不是 \r\n
,或者,使用 PHP_EOL
常量。
正是由于这种情况(跨平台兼容性),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 检查我对主要答案所做的更改...
Use PHP_EOL constant. It substitutes end of line based on platform specific.
注意: mysql_query 在 PHP 5.5.0 中被弃用,在 PHP 7.0.0 中被移除。相反,此函数的 MySQLi 或 PDO_MySQL 扩展名应为 used.Alternatives 包括:mysqli_query() 或 PDO::query()
你基本上有两种选择来实现这一点:
1.Using PDO(对于任何支持的数据库驱动程序):
$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(对于 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
}
请参考How can I prevent SQL-injection in PHP?