2014-01-29 309 views
0

我是新來的Wordpress,我很努力地發送附件的電子郵件:(我不知道什麼似乎是錯的,但我知道我缺少一些代碼 請幫幫我。非常感謝你在先進的!:))發送電子郵件與wordpress附件

這裏是我的代碼,它位於兩個不同的文件。 在我的index.php

Name (required)<br>   
    <input type="text" class="name"></input> 
     Email (required)</br> 
     <input type="text" class="e-mail"></input> 
     Attach Your Resume</br> 
     <input type="file" class="attachment" accept="image/gif, image/jpeg, image/png, application/pdf, application/msword"/> 
     <div id ="submit"><input id="button-submit" type="submit"></input></div> 
     </div> 


<script type="text/javascript"> 
$('#button-submit').click(function(){ 
    var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>'; 
    var names = $('.name').val(); 
    var emails = $('.e-mail').val(); 
    var attachments = $('.attachment').val(); 
    if (names =="" || emails=="" || attachments == ""){ 
     alert('You must fill all the required fields!'); 
    }else{ 
     var pattern=/^([a-zA-Z0-9_.-])[email protected]([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/; 
     if(pattern.test(emails)){ 
      /*$('#submit').html('<div class="loader"></div>');*/ 
      jQuery.ajax({ 
       url: ajaxurl, 
       type: 'POST', 
       dataType: 'html', 
       data: {action: 'resume', 
        txtname: $('.name').val(), 
        txtemail: $('.e-mail').val(), 
        attachment: $('.attachment').val(), 
        }, 

       complete: function(xhr, textStatus) { 

       }, 
       success: function(data, textStatus, xhr) { 
       $('#submit').html('Thank you for submitting!'); 
       // $('.name').val(""); 
       // $('.e-mail').val(""); 
       // $('.attachment').val(""); 
       }, 
       error: function(xhr, textStatus, errorThrown) { 
       } 
      }); 
     } 
     else{ 
      alert("Invalid E-mail address format."); 
     } 

    } 
    return false; 
});   

這一個是位於和我的functions.php宣佈

function resume() { 
$name = $_POST['txtname']; 
$email = $_POST['txtemail']; 
$attachments = $_POST['attachment']; 
$headers = 'From: [email protected]' . "\r\n" .'Reply-To: [email protected]' . "\r\n" .'X-Mailer: PHP/' . phpversion(); 
$message .= "Contact Details:\n\nName: ".$name."\nEmail: ".$email."\nResume: ";  
     if (file_exists($attachments)) { 
      $handle = fopen($attachments, 'r'); 

      $content = fread($handle, filesize($attachments)); 

      fclose($handle); 
      $message .= '--' . "\n"; 
      $message .= 'Content-Type: application/octet-stream; name="' . basename($attachments) . '"' . "\n"; 
      $message .= 'Content-Transfer-Encoding: base64' . "\n"; 
      $message .= 'Content-Disposition: attachment; filename="' . basename($attachments) . '"' . "\n"; 
      $message .= 'Content-ID: <' . basename(urlencode($attachments)) . '>' . "\n"; 
      $message .= 'X-Attachment-Id: ' . basename(urlencode($attachments)) . "\n" ."\n"; 
      $message .= chunk_split(base64_encode($content)); 
     } 
     else 
     { 
      $message .= "Attachment failed to upload."; 
     } 
mail("[email protected]", 'Careers', $message,$headers); 
die(); 
} 

回答

2

不同,需要你的請求的報頭中設置的JavaScript文件上傳接受文件:

"Content-Type", "multipart/form-data" 

落實到你的jQuery小號cript你也可以使用FormData類。 記住在jQuery ajax請求中設置這些選項。否則,您的請求將不包含該文件。請參閱鏈接瞭解更多信息。

contentType: false, 
processData: false, 

見這裏的例子:Sending multipart/formdata with jQuery.ajax

+0

謝謝Bjarke,我會檢查你給的鏈接。 ! :) – alvirbismonte

+0

不用'$ _FILES []'來捕捉我的附件變量的內容並仍然使用'$ _POST []'是好的嗎? – alvirbismonte

+0

我是否正確地將此添加到我的功能? $ header。='Content-Type:multipart/form-data;' 。 「\ n」 個; – alvirbismonte

0

您的wp-content /主題/ yourtheme /下載文件夾複製FPDF從 http://www.fpdf.org/ 發送帶有附件的電子郵件在WordPress

require('../fpdf181/fpdf.php'); 
    $pdf = new FPDF(); 
    $pdf->AddPage(); 
    $pdf->SetFont('Arial','B',12); 
    $pdf->Cell(40,10,$buyer_name); 
    $pdf->Cell(40,10,$product_name_semail); 
    $pdf->Cell(40,10,$order_price_semail); 
    $pdf->Cell(40,10,$species_name_semail); 
    $pdf->Cell(40,10,$finish_name_semail); 
    $pdf->Cell(40,10,$order_date_semail); 
    $filename="file-path/order-".$order_id.".pdf"; 
    $opt=$pdf->Output($filename,'F'); 
    $to = $to_mail; 
    $from = $from_mail; 
    $headers = "From: " . strip_tags($from) . "\r\n"; 
    $headers.= "CC: [email protected]\r\n"; 
    $headers.= "BCC: $from\r\n"; 
    $headers.= "MIME-Version: 1.0\r\n"; 
    $headers.= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 
    $message = '<html><body>'; 
    $message .= '<table width="100%"; rules="all" style="border:1px solid #3A5896; padding-left:20px">'; 
    $message .= '<span style="font-size: 14px;margin-bottom: 5px;padding: 7px 10px; background:#999999; color:#ffffff; float:left; margin-right:5px;">Order Confirmation </span>'; 
    $message .= "<tr><td><h1>Dear $buyer_data->user_login,</h1><br /><br /><tr><td>We Are Heartly Thanks To You. You can Choose Me For Your Order.<br>Your Order Details Here</td></tr>"; 
    $message .= "<tr><td style='font-size:14px;'>Product Details<br /><br /></td><tr><td style='width:20%'>Product Name: Shirt</td></tr>"; 
    $message.='<tr><td style="width:20%">Category: 'Cloth'</td></td>'; 
    $message.="<tr><td style='width:20%'>Price: 300/-</td>"; 
    $message.="<tr><td style='width:20%'>Shipping Date: 25/06/2016</td>"; 
    $attachments = array( WP_PLUGIN_DIR . '/file_name.pdf');  
    $message .= "<tr><td style='font-size:12px'><br><I>Thanks & Regards<br>Vivek Tamrakar</I></td><br><br></tr>"; 
    $message .= "</table>"; 
    $message .= "</body></html>"; 
    wp_mail($to, 'Order Confirmation ', $message, $headers,$attachments);