使用php mail()附件邮件?

时间:2013-04-20 15:50:32

标签: php

这是我用来发送带附件的电子邮件的代码片段,但它不起作用。我得到的输出包含大量的哈希码(我想是这样)。应该怎么做才能发送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);

    ?>

2 个答案:

答案 0 :(得分:3)

PHP Mailer是此问题的最佳选择。试试吧。 Click Here

例如:

<?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提供的示例代码。

答案 1 :(得分:0)

Zend_Mail库对此非常理想。库是精确编写的,以便您处理的人不必从头开始创建这些包,并利用它们。

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