简体   繁体   中英

Send email with attachments with wordpress

I'm new to Wordpress and I'm very struggling about this sending an email with attachments :( I don't know what seems to be wrong but I know that I lack some codes. Please help me. Thank you very much in advanced! :))

Here is my code which is located on two different files. In my 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;
});         

This one is located and declared on my 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();
}

To upload files with javascript you need to set the header of your request to accept files:

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

To implement it into your jQuery script you could also use the FormData class. Remeber to set these to options in your jQuery ajax request. Otherwise your request will not include the file. See link for more info.

contentType: false,
processData: false,

See example here : Sending multipart/formdata with jQuery.ajax

Copy FPDF on your wp-content/theme/yourtheme/ download folder from http://www.fpdf.org/ Send email with attachments In 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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