我是新來的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();
}
謝謝Bjarke,我會檢查你給的鏈接。 ! :) – alvirbismonte
不用'$ _FILES []'來捕捉我的附件變量的內容並仍然使用'$ _POST []'是好的嗎? – alvirbismonte
我是否正確地將此添加到我的功能? $ header。='Content-Type:multipart/form-data;' 。 「\ n」 個; – alvirbismonte