在wordpress中发送带有附件的电子邮件


Send Email with Attachment in wordpress

我正试图通过ajax从wordpress发送电子邮件,其中包含一个附件。但无法发送电子邮件。以下是代码。

//PHP Code
//=======================================================
$fileName=$_FILES["fileName"];
//  $attachments = array( WP_CONTENT_DIR . '/plugins/widget/uploads/'.$_FILES["fileName"]["tmp_name"]);  tried not working
        if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
            $output = json_encode(array( //create JSON data
                    'type'=>'error',
                    'text' => 'Sorry Request must be Ajax POST'
            ));
            die($output); //exit script outputting json data
        }
        //Sanitize input data using PHP filter_var().
        $user_name      = "";
        $user_email     = "xyz@yahoo.co.in";
        $country_code   = "";
        $phone_number   = "";
        $subject        ="CV";
        $message        = "Test Mail";
        if(strlen($subject)<1){ //check emtpy subject
            $output = json_encode(array('type'=>'error', 'text' => 'Subject is required'));
            die($output);
        }
        if(strlen($message)<1){ //check emtpy message
            $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
            die($output);
        }
        $message_body = $message."'r'n'r'n";
        ### Attachment Preparation ###
        $file_attached = false;
        if(isset($_FILES['fileName'])) //check uploaded file
        {
            //get file details we need
            $file_tmp_name    = $_FILES['fileName']['tmp_name'];
            $file_name        = $_FILES['fileName']['name'];
            $file_size        = $_FILES['fileName']['size'];
            $file_type        = $_FILES['fileName']['type'];
            $file_error       = $_FILES['fileName']['error'];
            //exit script and output error if we encounter any
            if($file_error>0)
            {
                $mymsg = array(
                        1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
                        2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
                        3=>"The uploaded file was only partially uploaded",
                        4=>"No file was uploaded",
                        6=>"Missing a temporary folder" );
                $output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
                die($output);
            }
            //read from the uploaded file & base64_encode content for the mail
            $handle = fopen($file_tmp_name, "r");
            $content = fread($handle, $file_size);
            fclose($handle);
            $encoded_content = chunk_split(base64_encode($content));
            //now we know we have the file for attachment, set $file_attached to true
            $file_attached = true;
        }
        if($file_attached) //continue if we have the file
        {
        # Mail headers should work with most clients
        $headers = "MIME-Version: 1.0'r'n";
    $headers = "X-Mailer: PHP/" . phpversion()."'r'n";
           // $headers .= "From: ".$from_email."'r'n";
            $headers .= "Subject: ".$subject."'r'n";
            //$headers .= "Reply-To: ".$user_email."" . "'r'n";
            $headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."'r'n'r'n";
                    $headers .= "--".md5('boundary1')."'r'n";
                    $headers .= "Content-Type: multipart/alternative;  boundary=".md5('boundary2')."'r'n'r'n";
                    $headers .= "--".md5('boundary2')."'r'n";
                    $headers .= "Content-Type: text/plain; charset=utf-8'r'n'r'n";
                            $headers .= $message_body."'r'n'r'n";
                            $headers .= "--".md5('boundary2')."--'r'n";
                            $headers .= "--".md5('boundary1')."'r'n";
                            $headers .= "Content-Type:  ".$file_type."; ";
                            $headers .= "name='"".$file_name."'"'r'n";
                            $headers .= "Content-Transfer-Encoding:base64'r'n";
    $headers .= "Content-Disposition:attachment; ";
    $headers .= "filename='"".$file_name."'"'r'n";
    $headers .= "X-Attachment-Id:".rand(1000,9000)."'r'n'r'n";
            $headers .= $encoded_content."'r'n";
            $headers .= "--".md5('boundary1')."--";
                            }else{
                            //proceed with PHP email.
                            $headers = 'From: '.$user_name.'' . "'r'n" .
                                    'Reply-To: '.$user_email.'' . "'r'n" .
                                    'X-Mailer: PHP/' . phpversion();
    }
    //$send_mail = mail("xyz@yahoo.co.in", $subject, $message_body, $headers);
    $subject="CV-For Job";
    $message_body="This the message for test";
    $send_mail=wp_mail("xyz@yahoo.co.in",$subject,$message_body, $headers);

///JavaScript代码//====================================

Query("body").on("click","#sendEmail",function(e)
{
    e.preventDefault();
    jQuery("#divError").remove();
    var uploader =$('#file');
    alert(uploader);
    if (uploader.val()=="") {
       jQuery("#flUpload").append("<div id='divError' style='color:red' class='error'>Please select file.</div>");
    }
    else
    {   
        var data=new FormData();
        data.append("action","sendEmail");
        data.append('fileName',$("#file")[0].files[0]);
        jQuery.ajax({
            type: "post",
            url:ajaxurl,
            processData: false,
            contentType: false,
            data: data,
            success: function(msg){
                alert(msg);
            },
            error: function(xhr, status, error) 
            {
               alert("Error->"+xhr.responseText);
            }
        });
    }
});

现在电子邮件发送成功,但当尝试附加文件时发生了以下情况:

  1. 带有附件选项的文件未附加,但邮件发送成功
  2. 当我试图通过头文件附加文件时,在电子邮件男孩和返回1的电子邮件中成功附加。但收件人无法收到电子邮件

我也查了垃圾文件夹,但没有

我做错了什么?

所有标头都有问题。我删除并处理附件

move_uploaded_file($_FILES["fileName"]["tmp_name"],WP_CONTENT_DIR .'/plugins/widget/uploads/'.basename($_FILES['fileName']['name']));
    $attachments = array( WP_CONTENT_DIR . '/plugins/widget/uploads/'.$_FILES["fileName"]["name"]);

并且它在上运行良好