使用php在电子邮件中发送附件文件 [英] Send a attach file in a email using php

查看:136
本文介绍了使用php在电子邮件中发送附件文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

使用php附加文件

我是使用以下代码在电子邮件中发送附件文件

i am using the following code to send a attached file in a email

 <?php

//to get the file extention
function getExtension($str)
{
    $i = strrpos($str,".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
}


//To Upload a file 
function upload_image()
{
  $newname="";



 if ($_FILES["resume_upload"]["error"] == UPLOAD_ERR_OK) 
    {


    $filename = $_FILES['resume_upload']['name'];
    $extension = getExtension($filename);
    $extension = strtolower($extension);
    $image_name=uniqid('img').'.'.$extension;
    $newname=$image_name;
    $temp_name=$_FILES['resume_upload']['tmp_name'];
    $copied = copy($temp_name, $newname);
return $newname;


}


}





$filename=upload_image();

echo "the file name :".$filename;


//$filename="";
$path ="";
$mailto="test@gmail.com";
$from_mail="test@gmail.com";
$from_name="test";
$replyto ="test@gmail.com";
$subject="test mail";
$message="test message";

mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message);

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
  //  $file = $path.$filename;
  $file=$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."--";


echo "end ofmail";
    if (mail($mailto, $subject, "", $header)) {
        $msg = "mail send ... OK"; // or use booleans here
    } else {
        $msg = "mail send ... ERROR!";
    }


}










?> 

我将文件上传到服务器。我需要将上传的文件作为附件发送到php电子邮件。

I am uploading the file to server . I need to send the uploaded file as attachment in php email.

上述代码无法发送附件邮件。有可能任何一个建议在php中实现这一点吗?

The above code not able to sending the attach mail. could any one please suggest something to achieve this in php?

推荐答案

希望它有帮助。

<?php
$eol = PHP_EOL;
$separator = md5(time());


 function mime_content_type($filename) {

        $mime_types = array(

            'txt' => 'text/plain',
            'htm' => 'text/html',
            'html' => 'text/html',
            'php' => 'text/html',
            'css' => 'text/css',
            'js' => 'application/javascript',
            'json' => 'application/json',
            'xml' => 'application/xml',
            'swf' => 'application/x-shockwave-flash',
            'flv' => 'video/x-flv',

            // images
            'png' => 'image/png',
            'jpe' => 'image/jpeg',
            'jpeg' => 'image/jpeg',
            'jpg' => 'image/jpeg',
            'gif' => 'image/gif',
            'bmp' => 'image/bmp',
            'ico' => 'image/vnd.microsoft.icon',
            'tiff' => 'image/tiff',
            'tif' => 'image/tiff',
            'svg' => 'image/svg+xml',
            'svgz' => 'image/svg+xml',

            // archives
            'zip' => 'application/zip',
            'rar' => 'application/x-rar-compressed',
            'exe' => 'application/x-msdownload',
            'msi' => 'application/x-msdownload',
            'cab' => 'application/vnd.ms-cab-compressed',

            // audio/video
            'mp3' => 'audio/mpeg',
            'qt' => 'video/quicktime',
            'mov' => 'video/quicktime',

            // adobe
            'pdf' => 'application/pdf',
            'psd' => 'image/vnd.adobe.photoshop',
            'ai' => 'application/postscript',
            'eps' => 'application/postscript',
            'ps' => 'application/postscript',

            // ms office
            'doc' => 'application/msword',
            'rtf' => 'application/rtf',
            'xls' => 'application/vnd.ms-excel',
            'ppt' => 'application/vnd.ms-powerpoint',

            // open office
            'odt' => 'application/vnd.oasis.opendocument.text',
            'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
        );

        $ext = strtolower(array_pop(explode('.',$filename)));
        if (array_key_exists($ext, $mime_types)) {
            return $mime_types[$ext];
        }
        elseif (function_exists('finfo_open')) {
            $finfo = finfo_open(FILEINFO_MIME);
            $mimetype = finfo_file($finfo, $filename);
            finfo_close($finfo);
            return $mimetype;
        }
        else {
            return 'application/octet-stream';
        }
    }

$file = array('mime' => mime_content_type($filename), 'name' => $filename, 'data' => chunk_split(base64_encode($filename)));


// Basic Header Input

$to='';
$cc='';
$bcc='';
$your_email = 'John@doe.com';
$to = 'Jane@doe.com';
$subject='A subject';
$from = 'The Matrix';
$message='What ever dot.com';
$headers  = "From: ".$your_email." <$from>".$eol;

$headers .= "Cc: $cc" . "\r\n";

$headers .= "Bcc: $bcc" . "\r\n"; 

// main header

$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$headers_to_rep .= "MIME-Version: 1.0".$eol; 
$headers_to_rep .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;


// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment

$body .= "--".$separator.$eol;
$body .= "Content-Type: ".$file['mime']."; name=\"".$file['name']."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $file['data'].$eol;
$body .= "--".$separator.$eol;

if(!mail($to, $subject, $body,$headers)) {
  echo "Mailer Error: ";
} else {

  echo "Message sent!";
}
?>

这篇关于使用php在电子邮件中发送附件文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆