2014-01-29 113 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);