Php 如何从Gmail获取附件?

Php 如何从Gmail获取附件?,php,sendmail,email-attachments,Php,Sendmail,Email Attachments,我已经写了一封带有附件的电子邮件。像这样: require_once“Mail.php”; 需要一次“Mail/mime.php”; $fromtestsenderserver@gmail.com_from'; $to$testsenderserver@gmail.com'; $subject='您在获得新订单'。gmdate(“Y-m-d\TH:i:s\Z”); //电子邮件的文本和html版本。 $text='Name:'。带标签($\u POST['customerName'])。“\r

我已经写了一封带有附件的电子邮件。像这样:

require_once“Mail.php”;
需要一次“Mail/mime.php”;
$fromtestsenderserver@gmail.com_from';
$to$testsenderserver@gmail.com';
$subject='您在获得新订单'。gmdate(“Y-m-d\TH:i:s\Z”);
//电子邮件的文本和html版本。
$text='Name:'。带标签($\u POST['customerName'])。“\r\n”。
“电话:”。剥去标签($\u POST['phone'])。“\r\n”。
“电子邮件:”。去除标签($\u POST['email'])。“\r\n”。
“Make:”。带标签($\u POST['make'])。“\r\n”。
“型号:”。带标签($\u POST['model'])。“\r\n”。
“年份:”。剥去标签($\u POST['year'])。“\r\n”。
“部分:”。带标签($\u POST['part'])。“\r\n”。
“绘制代码:”。带标签($\u POST['paintCode'])。“\r\n”;
$html='Name:'。带标签($_POST['customerName'])。'
“。 “电话:”。剥去标签($_POST['phone'])。'
“。 “电子邮件:”。去除标签($_POST['email'])。'
“。 “Make:”。带标签($_POST['make'])。'
“。 “型号:”。带标签($_POST['model'])。'
“。 “年份:”。剥去标签($_POST['year'])。'
“。 “部分:”。带标签($_POST['part'])。'
“。 “绘制代码:”。带标签($_POST['paintCode'])。'; $file=''; 如果(0<$\u文件['image1']['error']){ //回显“错误:”.$\u文件['file1']['Error'].
; } 否则{ $file='uploads/'。$_文件['image1']['name']; 移动上传的文件($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\; } $file='./'$文件;//附件 $crlf=“\n”; $mime=新邮件\u mime($crlf); $mime->setTXTBody($text); //$mime->setHTMLBody($html); $mime->addAttachment($file,'image/png'); //永远不要试图以相反的顺序调用这些行 $body=$mime->get(); $headers=数组( 'From'=>$From, '至'=>$至, “主题”=>$Subject ); $mime->headers($headers)试试这个

   // now we'll process our uploaded files
   foreach($_FILES as $userfile){

      // store the file information to variables for easier access
      $tmp_name = $userfile['tmp_name'];
      $type = $userfile['type'];
      $name = $userfile['name'];
      $size = $userfile['size'];


      // if the upload succeded, the file will exist
      if (file_exists($tmp_name)){

         // check to make sure that it is an uploaded file and not a system file
         if(is_uploaded_file($tmp_name)){
      $file_path = 'uploads/' . $userfile['tmp_name'];
      move_uploaded_file($userfile['tmp_name'], $file_path);

            // open the file for a binary read
            $file = fopen($tmp_name,'rb');

            // read the file content into a variable
            $data = fread($file,filesize($tmp_name));



            // close the file
            fclose($file);

            // now we encode it and split it into acceptable length lines
            $data = chunk_split(base64_encode($data));
         }

         $message .= "--{$mime_boundary}\n" .
            "Content-Type: {$type};\n" .
            " name=\"{$name}\"\n" .
            "Content-Disposition: attachment;\n" .
            " filename=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n";
      }
   }
        $message.="--{$mime_boundary}--\n";

if (mail($to, $subject, $message, $headers))

echo 'success';

// Finally, destroy the session.
session_destroy();  
}
试试这个

   // now we'll process our uploaded files
   foreach($_FILES as $userfile){

      // store the file information to variables for easier access
      $tmp_name = $userfile['tmp_name'];
      $type = $userfile['type'];
      $name = $userfile['name'];
      $size = $userfile['size'];


      // if the upload succeded, the file will exist
      if (file_exists($tmp_name)){

         // check to make sure that it is an uploaded file and not a system file
         if(is_uploaded_file($tmp_name)){
      $file_path = 'uploads/' . $userfile['tmp_name'];
      move_uploaded_file($userfile['tmp_name'], $file_path);

            // open the file for a binary read
            $file = fopen($tmp_name,'rb');

            // read the file content into a variable
            $data = fread($file,filesize($tmp_name));



            // close the file
            fclose($file);

            // now we encode it and split it into acceptable length lines
            $data = chunk_split(base64_encode($data));
         }

         $message .= "--{$mime_boundary}\n" .
            "Content-Type: {$type};\n" .
            " name=\"{$name}\"\n" .
            "Content-Disposition: attachment;\n" .
            " filename=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n";
      }
   }
        $message.="--{$mime_boundary}--\n";

if (mail($to, $subject, $message, $headers))

echo 'success';

// Finally, destroy the session.
session_destroy();  
}
谢谢大家的帮助。 我找到了应该使用的解决方案,而不是

只需遵循PHPMailer示例。结果如下:

谢谢并致以最诚挚的问候。

谢谢大家的帮助。 我找到了应该使用的解决方案,而不是

只需遵循PHPMailer示例。结果如下:

谢谢并致以最诚挚的问候。

请参阅。这可能会对你有所帮助。这可能对你有帮助。