8

基本上,我正在尝试通过 PHPMail 发送 PDF。电子邮件已发送,我在 Outlook 中完美接收。问题是附件坏了,打不开。我什至尝试发送 HTML 并且也是空的。

我尝试在论坛中进行研究,尝试了几个“工作代码”并且其他人使用此代码进行了工作......我不知道为什么不适合我。我正在使用最新版本的 PHPMail 5.2.2

$mail = new PHPMailer();
        $staffEmail = "staffemail";
        $mail->From = $staffEmail;
        $mail->FromName = "name";
        $mail->AddAddress('my@email.com');
        $mail->AddReplyTo($staffEmail, "name");

        $mail->AddAttachment('test.pdf');
        $mail->Subject = "PDF file attachment";

        $mail->Body = "message!";
        $mail->Send();
4

5 回答 5

8

这个自定义 PHP 函数将向(初级)PHP 开发人员展示如何构建带有附件功能的电子邮件脚本。请注意,邮件功能内部没有可用的验证功能。

<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
    }
}
?>

接下来,我们将展示一个示例,说明如何使用此功能发送带有附加 zip 文件的电子邮件:

<?php    $my_file = "somefile.zip";
    $my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
    $my_name = "Olaf Lederer";
    $my_mail = "my@mail.com";
    $my_replyto = "my_reply_to@mail.net";
    $my_subject = "This is a mail with attachment.";
    $my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
    mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
?>

您是否正在寻找发送多个附件的脚本?试试我们的邮件附件类脚本。

如果您想通过 SMTP 和 Gmail 发送您的网站邮件,请查看我们的 PHPMailer 教程。

于 2012-11-07T12:28:19.880 回答
2

我建议您将文件上传到服务器,然后将其附加到电子邮件中。例如,您可以使用以下步骤:

  1. 上传文件
  2. 将其附加到电子邮件
  3. 发送电子邮件
  4. 从服务器中删除此临时文件

试试这个,因为我认为这就是你问题的原因。

以防万一: http: //php.net/manual/en/features.file-upload.php

祝你好运

于 2012-10-26T09:30:54.887 回答
1

可能有两个问题,附件路径不正确或 php 对该文件夹没有权限

于 2012-11-06T12:10:47.843 回答
0

您可以尝试使用这个使用 PHP 简单邮件功能的功能:

function mail_attachment ($from , $to, $subject, $message, $attachment){
$fileatt = $attachment; // Path to the file                  
$fileatt_type = 'application/octet-stream'; // File Type 
$start= strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
$fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the     attachment 

$email_from = $from; // Who the email is from 
$email_subject =  $subject; // The Subject of the email 
$email_txt = $message; // Message that the email has in it 

$email_to = $to; // Who the email is to

$headers = "From: ".$email_from;

$file = fopen($fileatt,'rb'); 
$data = fread($file,filesize($fileatt)); 
fclose($file); 
$msg_txt=""; //\n\nMail created from websiteaddress systems : http://websiteaddress.com

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

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

$email_txt .= $msg_txt;

$email_message = "This is a multi-part message in websiteaddress format.\n\n" . 
            "--{$mime_boundary}\n" . 
            "Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
           "Content-Transfer-Encoding: 7bit\n\n".$email_txt. "\n\n"; 
$data = chunk_split(base64_encode($data)); 
$email_message .= "--{$mime_boundary}\n" . 
              "Content-Type: {$fileatt_type};\n" . 
              " name=\"{$fileatt_name}\"\n" . 
              //"Content-Disposition: attachment;\n" . 
              //" filename=\"{$fileatt_name}\"\n" . 
              "Content-Transfer-Encoding: base64\n\n" . 
             $data . "\n\n" . 
              "--{$mime_boundary}--\n"; 


$ok = @mail($email_to, $email_subject, $email_message, $headers); 

if($ok) { 
//echo "Attachment has been mailed !";
//header("Location: index.php");
} else { 
    die("Sorry but the email could not be sent. Please go back and try again!"); 
} 
}

希望这会帮助你。

于 2012-11-05T05:59:41.873 回答
0

这个正在为我工​​作,试图发送简历在 zend 中有附件

            $mail = new Zend_Mail ();
            $mail->setBodyHTML ( stripslashes ($message) );

            // add attachment
            $fileContents = file_get_contents($attachemnet);
            $resume = $mail->createAttachment($fileContents);
            $resume->filename = $EmployeeDeatils['resume'];

            //$mail->createAttachment($attachemnet);
            $mail->setFrom ( $mail_template ['from_email'], $mail_template ['from_caption'] );
            $mail->addTo ( $clientemail, $employee_name );
            $mail->setSubject ($subject );
            try {
                $mail->send ();
            } catch ( Exception $e ) {
                $this->_helper->errorlog ( " Send mail to member with activation link : " . $e->getMessage () );
            }
于 2012-11-01T09:47:10.453 回答