gpt4 book ai didi

javascript - 使用 WordPress 发送带有附件的电子邮件

转载 作者:行者123 更新时间:2023-12-02 17:50:13 25 4
gpt4 key购买 nike

我是 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 上传文件,您需要设置请求的 header 以接受文件:

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

要将其实现到 jQuery 脚本中,您还可以使用 FormData 类。请记住将这些设置为 jQuery ajax 请求中的选项。否则您的请求将不会包含该文件。请参阅链接了解更多信息。

contentType: false,
processData: false,

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

关于javascript - 使用 WordPress 发送带有附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21430389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com