且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP 发送带有文件附件的电子邮件 - 根本不发送电子邮件

更新时间:2023-02-25 10:58:31

For sending mail with attachment using php mail().Try this code:

<?php

 //If there is no error, send the email
 if(isset($_POST['ur_submit_button_name'])) {
  $EmailTo = "Me@here.com";
  $EmailFrom = "You@There.com";
  $EmailSubject = "The Email Subject";


  $separator = md5(time());

  // carriage return type (we use a PHP end of line constant)
  $eol = PHP_EOL;

  // attachment name
  $filename = "ip.zip";//store that zip file in ur root directory
  $attachment = chunk_split(base64_encode(file_get_contents('ip.zip')));

  // main header
  $headers  = "From: ".$from.$eol;
  $headers .= "MIME-Version: 1.0".$eol; 
  $headers .= "Content-Type: multipart/mixed; boundary="".$separator.""";

  // no more headers after this, we start the body! //

  $body = "--".$separator.$eol;
  $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
  $body .= "This is a MIME encoded message.".$eol;

  // message
  $body .= "--".$separator.$eol;
  $body .= "Content-Type: text/html; charset="iso-8859-1"".$eol;
  $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
  $body .= $message.$eol;

  // attachment
  $body .= "--".$separator.$eol;
  $body .= "Content-Type: application/octet-stream; name="".$filename.""".$eol; 
  $body .= "Content-Transfer-Encoding: base64".$eol;
  $body .= "Content-Disposition: attachment".$eol.$eol;
  $body .= $attachment.$eol;
  $body .= "--".$separator."--";

  // send message
  if (mail($to, $subject, $body, $headers)) {
  $mail_sent=true;
  echo "mail sent";
  } else {
  $mail_sent=false;
  echo "Error,Mail not sent";

 }
}

?>