Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP发送带有附件的邮件_Php_Image_Email_Attachment - Fatal编程技术网

用PHP发送带有附件的邮件

用PHP发送带有附件的邮件,php,image,email,attachment,Php,Image,Email,Attachment,我想用php邮件功能发送一封带有附件的邮件。我可以收到一封邮件。不幸的是,图像被破坏了。该图像与php脚本位于同一目录中 我的脚本源代码: $name = "Name goes here"; $email = "recipient@gmail.com"; $to = "$name <$email>"; $from = "Sender"; $subject = "Here is your attachment"; $fileatt = "test.jpg"; $fileatttype

我想用php邮件功能发送一封带有附件的邮件。我可以收到一封邮件。不幸的是,图像被破坏了。该图像与php脚本位于同一目录中

我的脚本源代码:

$name = "Name goes here";
$email = "recipient@gmail.com";
$to = "$name <$email>";
$from = "Sender";
$subject = "Here is your attachment";
$fileatt = "test.jpg";
$fileatttype = "image/jpeg";
$fileattname = "test.jpg";
$headers = "From: $from";

// File
$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);

// This attaches the file
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"-{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message .= "\n\n";

$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";

// Send the email
if(mail($to, $subject, $message, $headers)) {

    echo "The email was sent.";

}
else {

    echo "There was an error sending the mail.";

}
遵循本教程:


?>

尝试使用图像附件的绝对路径。请参阅我尝试了绝对路径。它也不起作用。有一个附件坏了。
 $fileatt = "your folder name/test.jpg";

<?php

       $files = array(
                    "yourfoldername/yourfilename.png",
                    "yourfoldername/yourfilenamepng"
                  );
                  //1431347258selfie_1431347013130.png

    // email fields: to, from, subject, and so on
    $to         =   "example@gmail.com";
    $from       =   "support@mail.com"; 
    $subject    =   "My subject"; 
    $message    =   "My message";
    $headers    =   "From: $from";

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

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

    // multipart boundary 
    $message        = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
    $message .= "--{$mime_boundary}\n";

    // preparing attachments
    for( $x=0; $x<count($files); $x++ )
    {
        $file = fopen($files[$x],"rb");
        $data = fread($file,filesize($files[$x]));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
        "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }

    // send mail
    if( mail($to, $subject, $message, $headers) ) 
    {
        $somthing = "Mail successfully Send.";
    }
    else
    {
        $somthing = "Mail sending fail.";
    }
    echo $somthing;