繁体   English   中英

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

[英]Send email with attachments with 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();
}

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

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

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

contentType: false,
processData: false,

请参阅此处的示例: 使用jQuery.ajax发送multipart / formdata

http://www.fpdf.org/复制您的wp-content / theme / yourtheme / download文件夹中的FPDF。在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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM