[英]Send Email with Attachment in wordpress
I am trying to send email from wordpress via ajax, containing an attachment. 我试图通过ajax从wordpress发送电子邮件,其中包含附件。 But can not send reach to the email. 但无法将范围发送到电子邮件。 Following are the code. 以下是代码。
//PHP Code
//=======================================================
$fileName=$_FILES["fileName"];
// $attachments = array( WP_CONTENT_DIR . '/plugins/widget/uploads/'.$_FILES["fileName"]["tmp_name"]); tried not working
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output); //exit script outputting json data
}
//Sanitize input data using PHP filter_var().
$user_name = "";
$user_email = "xyz@yahoo.co.in";
$country_code = "";
$phone_number = "";
$subject ="CV";
$message = "Test Mail";
if(strlen($subject)<1){ //check emtpy subject
$output = json_encode(array('type'=>'error', 'text' => 'Subject is required'));
die($output);
}
if(strlen($message)<1){ //check emtpy message
$output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}
$message_body = $message."\r\n\r\n";
### Attachment Preparation ###
$file_attached = false;
if(isset($_FILES['fileName'])) //check uploaded file
{
//get file details we need
$file_tmp_name = $_FILES['fileName']['tmp_name'];
$file_name = $_FILES['fileName']['name'];
$file_size = $_FILES['fileName']['size'];
$file_type = $_FILES['fileName']['type'];
$file_error = $_FILES['fileName']['error'];
//exit script and output error if we encounter any
if($file_error>0)
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
$output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
die($output);
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
//now we know we have the file for attachment, set $file_attached to true
$file_attached = true;
}
if($file_attached) //continue if we have the file
{
# Mail headers should work with most clients
$headers = "MIME-Version: 1.0\r\n";
$headers = "X-Mailer: PHP/" . phpversion()."\r\n";
// $headers .= "From: ".$from_email."\r\n";
$headers .= "Subject: ".$subject."\r\n";
//$headers .= "Reply-To: ".$user_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=".md5('boundary2')."\r\n\r\n";
$headers .= "--".md5('boundary2')."\r\n";
$headers .= "Content-Type: text/plain; charset=utf-8\r\n\r\n";
$headers .= $message_body."\r\n\r\n";
$headers .= "--".md5('boundary2')."--\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: ".$file_type."; ";
$headers .= "name=\"".$file_name."\"\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";
$headers .= "Content-Disposition:attachment; ";
$headers .= "filename=\"".$file_name."\"\r\n";
$headers .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
$headers .= $encoded_content."\r\n";
$headers .= "--".md5('boundary1')."--";
}else{
//proceed with PHP email.
$headers = 'From: '.$user_name.'' . "\r\n" .
'Reply-To: '.$user_email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
}
//$send_mail = mail("xyz@yahoo.co.in", $subject, $message_body, $headers);
$subject="CV-For Job";
$message_body="This the message for test";
$send_mail=wp_mail("xyz@yahoo.co.in",$subject,$message_body, $headers);
///JavaScript Code //=================================== /// JavaScript代码// ==================================
Query("body").on("click","#sendEmail",function(e)
{
e.preventDefault();
jQuery("#divError").remove();
var uploader =$('#file');
alert(uploader);
if (uploader.val()=="") {
jQuery("#flUpload").append("<div id='divError' style='color:red' class='error'>Please select file.</div>");
}
else
{
var data=new FormData();
data.append("action","sendEmail");
data.append('fileName',$("#file")[0].files[0]);
jQuery.ajax({
type: "post",
url:ajaxurl,
processData: false,
contentType: false,
data: data,
success: function(msg){
alert(msg);
},
error: function(xhr, status, error)
{
alert("Error->"+xhr.responseText);
}
});
}
});
Now emails send successfully but when try to attach file following happened: 现在,电子邮件发送成功,但是尝试附加文件时发生以下情况:
I looked into junk folder also but nothing 我也看着垃圾文件夹但是什么也没有
What am I doing wrong? 我究竟做错了什么?
All there was problem in header. 头文件全部有问题。 I remove and process with attachments 我删除并处理附件
move_uploaded_file($_FILES["fileName"]["tmp_name"],WP_CONTENT_DIR .'/plugins/widget/uploads/'.basename($_FILES['fileName']['name']));
$attachments = array( WP_CONTENT_DIR . '/plugins/widget/uploads/'.$_FILES["fileName"]["name"]);
and it is working fine 而且工作正常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.