0

我正在通过 php mail() 发送电子邮件。在那,我能够接收简单的(文本)邮件。当我附加文件时,我收到了电子邮件中的附件,但是当我尝试查看它时,文件总是空白,即使它是 PDF 格式!!!。请帮帮我。我的发送邮件页面的代码如下:

include_once("db_connect.php");
session_start();
error_reporting(16);
require_once('class.phpmailer.php');//Getting values from Session
$tot = $_SESSION['$tot'];
$from = $_SESSION['from'];
$message = $_SESSION['message'];
$subject = $_SESSION['subject'];
$full_nm = $_SESSION['firstnm']." ".$_SESSION['lastnm'];
$temp_passwd = $_SESSION['e_pwd'];
$email_use = $_SESSION['email'];

function send_mail($to, $message, $subject, $from, $temp_passwd, $full_nm, $email_use)
{
    $message_org      = nl2br($message);
    $mail             = new PHPMailer();
    $body             = $message_org;
    $mail->IsSMTP(); 
    $mail->SMTPAuth   = true;
    $mail->SMTPSecure = "ssl";
    $mail->AddAttachment("Uploads/file.pdf");
    $mail->ContentType = "mutlipart/alternative";
    $mail->Host       = "smtp.mail.yahoo.com";
    $mail->Port       = 465;
    $mail->Username   = $from;
    $mail->Password   = $temp_passwd;
    $mail->SetFrom($from, $full_nm);
    $mail->Subject    = $subject;
    $mail->MsgHTML($body);
    $address = $to;
    $mail->AddAddress($address, $address);

    if(!$mail->Send())
    {
        $err_send = "Mailer Error: " . $mail->ErrorInfo;
    }
        else
    {
        $err_send = "";
    }
}
foreach($tot as $to_trim)
{
    $to = trim($to_trim);
    send_mail($to, $message, $subject, $from, $temp_passwd, $full_nm, $email_use);
    echo $err_send;
}

The function AddAttachment in class.phpmailer.php is as follows:

public function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
try {
  if ( !@is_file($path) ) {
    throw new phpmailerException($this->Lang('file_access') . $path, self::STOP_CONTINUE);
  }
  $filename = basename($path);
  if ( $name == '' ) {
    $name = $filename;
  }

  $this->attachment[] = array(
    0 => $path,
    1 => $filename,
    2 => $name,
    3 => $encoding,
    4 => $type,
    5 => false,  // isStringAttachment
    6 => 'attachment',
    7 => 0
  );

} catch (phpmailerException $e) {
  $this->SetError($e->getMessage());
  if ($this->exceptions) {
    throw $e;
  }
  echo $e->getMessage()."\n";
  if ( $e->getCode() == self::STOP_CRITICAL ) {
    return false;
  }
}
return true;
}
4

2 回答 2

0

当您执行 $mail->AddAttachment("Uploads/file.pdf"); 如果您将添加附件放在“如果文件存在添加附件”子句中,文件不会被附加吗?我在想“Uploads/file.pdf”不是足够的路径。

于 2012-04-25T07:32:30.960 回答
0

根据此类的文档,您应该能够为该文件指定相对路径。

尝试在该行之前使用 PHP 测试来查看文件是否实际存在:

// Change this
$mail->AddAttachment("Uploads/file.pdf");

// To this...
$attachment = "Uploads/file.pdf";
$mail->AddAttachment($attachment);

// And then to this
$attachment = "Uploads/file.pdf";
if(!file_exists($attachment) && !file_exists(getcwd().'/'.$attachment)
{
    die(sprintf("attachment: %s does not exist", $attachment));
}
$mail->AddAttachment($attachment);

我包括一个中间步骤只是为了向您展示我的想法。

我没有测试过这个if语句,但想法是确保$attachment变量是有效的绝对(第一部分)或相对(第二部分)路径。

于 2012-04-25T07:43:43.833 回答