簡體   English   中英

在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"]);

而且工作正常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM