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();
}