如何在wordpress中将文件上传到自定义文件夹并发送附件到电子邮件中

问题描述 投票:0回答:2

我在理解上传和发送附件时遇到了一些麻烦。例如:

$zip_code = $_POST['zip_code'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$attachments = '';
if (!empty($_FILES['attachment']['tmp_name'])) {
    $path = $_FILES['attachment']['name'];
    if (copy($_FILES['attachment']['tmp_name'], $path)) $attachments = $path;
}

$subject = 'You have message from ' . get_bloginfo('name');
$message = '<table width="100%" cellspacing="0" cellpadding="5" border="0">';
$message .= '<tr><td width="150px"><b>Zip Code:</b></td><td>' . $zip_code . '</td></tr>';
$message .= '<tr><td width="150px"><b>Phone:</b></td><td>' . $phone . '</td></tr>';
$message .= '<tr><td width="150px"><b>Email:</b></td><td>' . $email . '</td></tr>';
$message .= '<tr><td width="150px"><b>Attachments:</b></td><td>' . $attachments . '</td></tr>';
$message .= '</table>';
//php mailer variables
$to = get_option('admin_email');
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Disposition: attachment; filename = \"" . $attachments . "\"\n\n";
$sent = wp_mail($to, $subject, $message, $headers, $attachments);

此代码无法正常工作。电子邮件和附件发送正确,但文件已在根目录中上传。当我上传相同文件时,新文件会覆盖旧文件。我不明白如何在文件名中添加一些哈希标签。

也许您能帮我吗?任何想法或我可以在其中阅读如何解决此问题的方法。

谢谢!

wordpress email file-upload attachment
2个回答
1
投票

什么不起作用?为了帮助您找出不起作用的地方,请尝试以下操作:

$zip_code = $_POST['zip_code'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$attachments = '';
if (!empty($_FILES['attachment']['tmp_name'])) {
    $path = $_FILES['attachment']['name'];
    if (copy($_FILES['attachment']['tmp_name'], $path)) $attachments = $path;
}

$subject = 'You have message from ' . get_bloginfo('name');
$message = '<table width="100%" cellspacing="0" cellpadding="5" border="0">';
$message .= '<tr><td width="150px"><b>Zip Code:</b></td><td>' . $zip_code . '</td></tr>';
$message .= '<tr><td width="150px"><b>Phone:</b></td><td>' . $phone . '</td></tr>';
$message .= '<tr><td width="150px"><b>Email:</b></td><td>' . $email . '</td></tr>';
$message .= '<tr><td width="150px"><b>Attachments:</b></td><td>' . $attachments . '</td></tr>';
$message .= '</table>';
//php mailer variables
$to = get_option('admin_email');
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Disposition: attachment; filename = \"" . $attachments . "\"\n\n";

echo 'Headers:  . $headers . '<br>/r/n';
echo 'To: ' . $to .'<br>/r/n';
echo 'Subject: ' . $subject . '<br>/r/n';
echo 'Message: ' . $message . '<br>/r/n';
echo 'Image: <img src="' . $attachments .'" />';


// $sent = wp_mail($to, $subject, $message, $headers, $attachments);

如果有任何遗漏,您将能够看到遗失的东西,并且您可以通过这种方式来纠正您的代码。


0
投票

成功上传后,WordPress自定义代码可以上传文件并将数据保存到数据库中,并将所有文件作为附件发送给用户。

if(isset($_POST['status'])){

    $uploaddir = '/home/public_html/wp-content/uploads/order-doc-folder/';

    foreach($_FILES['docat'] as $key => $doc){

      if($_FILES['docat']['name'][$key] != ''){

            $temp = explode(".", $_FILES['docat']['name'][$key]);
            $newfilename = $_REQUEST['order_id'].'-'.round(microtime(true)) . '-'.$temp[0].'.' . end($temp);

            $uploadfile = $uploaddir . basename($newfilename);

            if( move_uploaded_file($_FILES['docat']['tmp_name'][$key], $uploadfile)){
                $filename[] = $newfilename;
                $filenamed[] = $uploadfile;
            }
        }
    }

    $subject = 'Your Order status update';
    $message = '



Dear Customer,
Your order no: '.$result->u_id.' current status is '.$_POST['status'].'
'.$_POST['comment'].'
Thanks
';

    $senderName = "Talk For WEB";
    $senderEmail = "[email protected]";  
    $from = $senderName." <".$senderEmail.">"; 
    $headers = "From: $from"; 

    // Boundary  
    $semi_rand = md5(time());  
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  

    // Headers for attachment  
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";  

    // Multipart boundary  
    $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" . 
    "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";  

    // Preparing attachment 
    if(!empty($filenamed)){ 
        for($i=0;$iemail, $subject, $message, $headers, $returnpath);  
    $updateo = true; 
    // Return true, if email sent, otherwise return false 
    if($mail){ 
        return true;
    }else{ 
        return false;
    } 

Working code example check here

© www.soinside.com 2019 - 2024. All rights reserved.