0

phpのメール機能を使ってpdfを添付したメールを送ろうとしています。メールで他のものを受け取っていますが、添付ファイルのサイズはゼロキロバイトです。誰かが私のコードを見て、ここで何が間違っているかを提案できますか?

ありがとう!以下はコードです:

public static function sendMailWithAttachment($to, $from, $from_name, $subject, $msg,$attachmentName, $attachmentPath){
// Setting a timezone, mail() uses this.
//date_default_timezone_set('America/New_York');
$semi_rand = md5(time());
$data = chunk_split(base64_encode($data));

$fileatt_type = "application/pdf"; // File Type
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// set header ........................
$headers = "From: ".$from;
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// set email message......................
//$email_message = "Thanks for visiting ";
//$email_message .= "Thanks for visiting.<br>";// Message that the email has in it
$email_message=$msg;
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$attachmentPath}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$attachmentName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";

$sent = @mail($to, $subject, $email_message, $headers);
if($sent) {
echo "Your email attachment send successfully.";
} else {
die("Sorry but the email could not be sent. Please try again!");
}
} 
4

2 に答える 2

0

個人的には、すべてのメール活動を処理するために、実証済みのメール コンポーネントを使用することをお勧めします。私が過去に使用したものはPHPMailer(ここにあります) で、箱から出してすぐにうまく動作するように見えました (しかも無料です!)。

使用法を簡素化するために、次のラッパー クラスを作成しました。

<?php
require_once "PHPMailer.php";

/**
 * class SimpleMailer
 * Very basic mail wrapper around PHPMailer,
 * helps to abstract some of PHPMailer's huge feature list
 * and keep the complexity as low as possible
 *
 * @author Jason Larke
 * @date  10/10/2012
 */
class SimpleMailer {
    /**
     * bool send(array, string, string, mixed, array)
     * Send a basic email to a list of recipients
     *
     * @param $recipients an array of recipients to this email. Each array entry should be either an email address, or a key-value pair of email=>value and name=>value
     * @param $subject Email subject string
     * @param $body Email body string
     * @param $from mixed, can either be a single email address or an associative array specifying both email and name keys i.e array('email'=>'admin@example.com','name'=>'Example')
     * @param $attachments An array of file-paths to files that should be attached to the email
     */
    public static function send($recipients, $subject, $body, $from, $attachments=array()) {
        // sanitize
        $recipients = (array)$recipients;
        $attachments = (array)$attachments;

        $client = new PHPMailer();
        $client->IsMail(); // use PHP's internal 'mail' function, for simplicity.

        $senderEmail = is_array($from) && !empty($from['email']) ? $from['email'] : $from;
        $senderName = is_array($from) && !empty($from['name']) ? $from['name'] : $senderEmail;

        $client->SetFrom($senderEmail, $senderName);
        $client->Subject = $subject;
        $client->MsgHTML($body);

        foreach($recipients as $to) {
            $email = is_array($to) && !empty($to['email']) ? $to['email'] : $to;
            $name = is_array($to) && !empty($to['name']) ? $to['name'] : $email;
            $client->AddAddress($email, $name);
        }

        foreach($attachments as $attach)
            $client->AddAttachment($attach);

        $result = $client->Send();
        if (!$result) {
            // todo: Log any information about why the mail failed to send (accessed by $client->ErrorInfo)
        }
        return $result;
    }
}
?>

クラスの使用例は次のようになります。

SimpleMailer::send(
    'foobar@example.com', // to-address
    'This is a test email', // subject
    'Hello Foo Bar, we're testing our email service.' // content
    array('email' => 'admin@example.com', 'name' => 'Example.com'), // sender
    '/local/docs/example.pdf' // attachments (can be an array)
);
于 2013-02-28T06:27:30.510 に答える
0

添付ファイル付きのメールを送信するためにこのアプローチを使用する理由がわかりません。デバッグが困難です。私の機能を見てください。

<?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!";
    }
}
?>
于 2013-02-28T06:17:56.993 に答える