多部分/混合电子邮件形式数据中md5的用途

时间:2015-09-18 17:16:57

标签: php email md5 multipartform-data multipart

我想知道以下PHP代码段中md5("sanwebe")校验和的用途,我发现herehere。 “sanwebe”参数有什么作用?

if($file_attached) //continue if we have the file
{
  $boundary = md5("sanwebe"); 
  //header
  $headers = "MIME-Version: 1.0\r\n"; 
  $headers .= "From:".$from_email."\r\n"; 
  $headers .= "Reply-To: ".$email."" . "\r\n";
  $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

  //plain text 
  $body = "--$boundary\r\n";
  $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
  $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
  $body .= chunk_split(base64_encode($message_body)); 

  //attachment
  $body .= "--$boundary\r\n";
  $body .="Content-Type: $file_type; name=\"$file_name\"\r\n";
  $body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n";
  $body .="Content-Transfer-Encoding: base64\r\n";
  $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
  $body .= $encoded_content; 
}else{
  //proceed with PHP email.
  $headers = "From:".$from_email."\r\n".
  'Reply-To: '.$email.'' . "\n" .
  'X-Mailer: PHP/' . phpversion();
  $body = $message_body;
}

2 个答案:

答案 0 :(得分:2)

md5("sanwebe")的结果被用作电子邮件中不同内容部分的边界。

为什么" sanwebe"?没有理由。你在那里的代码片段可能是在sanwebe.com上发布的(一个快速的google引发了一对),或者源自最初的片段。关于" sanwebe"没什么特别的,您也可以使用md5("stackoverflow")

答案 1 :(得分:1)

多部分MIME邮件中的不同部分必须具有一种标记,以便收件人知道再次拆分部件的位置。 这是通过使用Content-Type邮件头的边界参数定义任意令牌来完成的。这个令牌没有太多要求,但其中一个是:我可能不会出现在邮件的任何其他地方。

在你所显示的代码中使用md5函数只是使用md5函数生成一些可能不存在于消息中的令牌。您可以使用md5的任何其他参数,或者您可以定义$ boundary变量以包含任何其他随机字符串。