使用wordpress发送带附件的电子邮件

时间:2014-01-29 11:54:14

标签: javascript php jquery ajax 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_.-])+@([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: automailer@sample.ph' . "\r\n" .'Reply-To: automailer@sample.ph' . "\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("sample@outlook.com", 'Careers', $message,$headers);
die();
}

2 个答案:

答案 0 :(得分:2)

要使用javascript上传文件,您需要设置接受文件的请求标题:

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

要将其实现到jQuery脚本中,您还可以使用FormData类。 记得在jQuery ajax请求中将这些选项设置为选项。否则您的请求将不包含该文件。有关详细信息,请参阅链接。

contentType: false,
processData: false,

请参阅此处的示例:Sending multipart/formdata with jQuery.ajax

答案 1 :(得分:0)

  

从您的wp-content / theme / yourtheme / download文件夹复制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: v.tamrakar@laxyosolutionsoft.com\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);