php - 如何在 PHP 中发送电子邮件附件

标签 php

  <?php
  // array with filenames to be sent as attachment
  $files = array("sendFiles.php");

  // email fields: to, from, subject, and so on
  $to = "dfjdsoj@googlemail.com";
  $from = "mail@mail.com";
  $subject ="My subject";
  $message = "A logo has been sen't by". $_SESSION['loggedin_business_name'];
  $headers = "From: $from";

  // boundary 
  $semi_rand = md5(time()); 
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

  // headers for attachment
  $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

  // multipart boundary
  $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
  $message .= "--{$mime_boundary}\n";

  // preparing attachments
  for($x=0;$x<count($files);$x++){
      $file = fopen($files[$x],"rb");
      $data = fread($file,filesize($files[$x]));
      fclose($file);
      $data = chunk_split(base64_encode($data));
      $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
      "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
      "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
      $message .= "--{$mime_boundary}\n";       
  }

  // send       
  echo sizeof($files);
  $ok = @mail($to, $subject, $message, $headers);
  if ($ok) {
      echo "<p>mail sent to $to!</p>";
  } else {
      echo "<p>mail could not be sent!</p>";
  }
  ?>

我收到一封电子邮件,其中包含我的 sendfiles.php,然后是文本文件 ATT00424.txt。数字每次都在变化。将它发送到我的 gmail 就可以了!很奇怪!


$files = array("sendFiles.php");

  // email fields: to, from, subject, and so on

  $to = "hdfiuhufsadhfu@yaho.com";

  $from = "mail@mail.com";

  $subject ="My subject";

  $message = "A logo has been sen't by". $_POST['loggedin_business_name'];

  $headers = "From: $from";

  // boundary

  $semi_rand = md5(time());

  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// headers for attachment

  $headers .= "\r\nMIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\"";
  // multipart boundary

  $message = "This is a multi-part message in MIME format.\r\n" . "--{$mime_boundary}\r\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" . "Content-Transfer-Encoding: 7bit\r\n" . $message . "\r\n";

  $message .= "--{$mime_boundary}\r\n";
  // preparing attachments

  for($x=0;$x<count($files);$x++){

      $file = fopen($files[$x],"rb");

      $data = fread($file,filesize($files[$x]));

      fclose($file);

      $data = chunk_split(base64_encode($data));

      $message .= "Content-Type: {\"application/octet-stream\"};\r\n" . " name=\"$files[$x]\"\r\n" .

      "Content-Disposition: attachment;\r\n" . " filename=\"$files[$x]\"\r\n" .

      "Content-Transfer-Encoding: base64\r\n" . $data . "\r\n";

      $message .= "--{$mime_boundary}\r\n";

  }

在代码中添加 CRLF 解决了附件问题,但是现在消息“A logo has been sent by”已经消失了。这是为什么?

最佳答案

Use phpMailer()

<?php 
require_once('phpmailer.php');

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSendmail(); // telling the class to use SendMail transport

try {
    $mail->AddReplyTo('email@example.com', 'First Last');
    $mail->AddAddress('John@example.com', 'John Doe');

    $mail->SetFrom('email@example.com', 'First Last');
    $mail->Subject  =  "Subject Line";
    $mail->AltBody    = "Alternate Text"; // optional, comment out and test

    $mail->WordWrap     =   50; // set word wrap    
    $mail->Body = "This is the body of the email";
    $mail->IsHTML(true); // send as HTML

    // Single or Multiple File Attachments
    $mail->AddAttachment('../path-to-file.pdf', 'File-Name.pdf');
    $mail->Send(); // Try to send email
    //echo "Message Sent OK<p></p>\n";
    } catch (phpmailerException $e) {
        echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
        echo $e->getMessage(); //Boring error messages from anything else!
}
// end try


?>

关于php - 如何在 PHP 中发送电子邮件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1498219/

相关文章:

php - 类 mysqli 的对象无法转换为 int

php - 仅在函数内部循环迭代一次

javascript - 通过php代码获取JSON数据

php - Windows Azure 中的非拉丁字符 URL 重写

PHP,格式数字总是有 2 个小数位

php - 使用 cronjob 运行 php 脚本

php - 我可以使用 php 脚本覆盖/取消 HTTP 响应 header 字段,以便不设置该字段吗?

php - 压力测试时平均响应时间增加

php - 可以在 FreeBSD 10.3 中安装没有依赖项的 pkg 吗?

php - 来自 mysql 的图像正在加载到位,但未显示