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!";
    }
}
?>

次に、この関数を使用して1つの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を介してWebサイトのメールメッセージを送信する場合は、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 にそのフォルダーへのアクセス許可がないかの 2 つの問題が考えられます。

于 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 に答える