1

这是我用来发送带有附件的电子邮件的代码片段,但它不起作用。我得到的输出包括大量的哈希码(我想是的)。应该怎么做才能将 screenshot.rar 作为附件发送?

<?php

      $to = "abc@gmail.com";

      $subject = "A test email";

      $random_hash = md5(date('r', time()));

      $headers = "From: xyz@gmail.com\r\n";

      $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

      $attachment = chunk_split(base64_encode(file_get_contents("screenshot.rar")));

      $output = "
      --PHP-mixed-$random_hash
      Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash'
      --PHP-alt-$random_hash
      Content-Type: text/plain; charset='iso-8859-1'
      Content-Transfer-Encoding: 7bit

      Hello World!
      This is the simple text version of the email message.

      --PHP-alt-$random_hash
      Content-Type: text/html; charset='iso-8859-1'
      Content-Transfer-Encoding: 7bit

      <h2>Hello World!</h2>
      <p>This is the <b>HTML</b> version of the email message.</p>

      --PHP-alt-$random_hash--

      --PHP-mixed-$random_hash
      Content-Type: application/zip; name=screenshot.rar
      Content-Transfer-Encoding: base64
      Content-Disposition: attachment

      $attachment
      --PHP-mixed-$random_hash--";

      echo @mail($to, $subject, $output, $headers);

    ?>
4

2 回答 2

3

PHP Mailer 是解决这个问题的最佳选择。试试看。点击这里

前任:

<?php
require 'class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->AddAddress('josh@example.net', 'Josh Adams');  // Add a recipient
$mail->AddAddress('ellen@example.com');               // Name is optional
$mail->AddReplyTo('info@example.com', 'Information');
$mail->AddCC('cc@example.com');
$mail->AddBCC('bcc@example.com');


$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->AddAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->AddAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->IsHTML(true);                                  // Set email format to HTML


$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->Send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}
echo 'Message has been sent';
?>

这是 phpmailer 提供的示例代码。

于 2013-04-20T16:25:13.963 回答
0

Zend_Mail库非常适合这个。库是精确编写的,因此您所处情况的人不必从头开始创建这些包,利用它们。

文档非常好(上面链接),您只需下载 Zend Framework 并将其包含在您include_files的 php.ini 中。

于 2013-04-20T16:05:01.257 回答