后台仪表盘邮件附件发送异常:图片上传仅存入oc_upload表未发送
Got it, your issue is that when uploading images through the admin dashboard, the files are successfully saved to the oc_upload table but aren't being sent as email attachments. Let's fix this by adding the email attachment logic right after the image upload completes—here's how to do it, assuming you're working with an OpenCart setup (given the oc_ table prefix):
Step 1: Grab the Uploaded Image Details
First, after the image is uploaded and inserted into oc_upload, you need to fetch the file's actual path and name. If you're using OpenCart's built-in upload tool, you can get this from the upload model or the newly inserted record:
// After successful upload & insertion into oc_upload $upload_id = $this->model_tool_upload->addUpload($data); // Returns the new upload ID $upload_info = $this->model_tool_upload->getUpload($upload_id); // Fetch full record details
Step 2: Add Email Attachment & Send Logic
Use OpenCart's native mail class to handle the email (this ensures compatibility with your existing admin mail settings):
// Load necessary models and initialize mail class $this->load->model('setting/mail'); $mail = new Mail(); // Pull your existing mail config from admin settings $mail->protocol = $this->config->get('config_mail_protocol'); $mail->parameter = $this->config->get('config_mail_parameter'); $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname'); $mail->smtp_username = $this->config->get('config_mail_smtp_username'); $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'); $mail->smtp_port = $this->config->get('config_mail_smtp_port'); $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout'); // Set email content and recipients $mail->setTo('recipient@example.com'); // Replace with your target email $mail->setFrom($this->config->get('config_email')); $mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8')); $mail->setSubject('New Image Uploaded to Admin Dashboard'); $mail->setText('A new image has been uploaded. The file is attached to this email for your reference.'); // Attach the uploaded image (use the full server file path) $full_file_path = DIR_UPLOAD . $upload_info['file_path'] . $upload_info['file_name']; if (file_exists($full_file_path)) { $mail->addAttachment($full_file_path); } else { $this->session->data['error'] = 'Image saved to database but file not found for email attachment.'; } // Send email with error handling try { $mail->send(); $this->session->data['success'] = 'Image uploaded successfully and notification email sent!'; } catch (Exception $e) { $this->session->data['error'] = 'Image uploaded successfully, but email failed to send: ' . $e->getMessage(); }
Step 3: Add Status Notifications to the Frontend
Update your template to show success/error messages to the admin user:
<?php if (isset($error)) { ?> <div class="alert alert-danger"><?php echo $error; ?></div> <?php } ?> <?php if (isset($success)) { ?> <div class="alert alert-success"><?php echo $success; ?></div> <?php } ?>
Key Things to Double-Check
- File Permissions: Ensure the server has read access to the uploaded file (check
DIR_UPLOADdirectory permissions—755 is typically safe). - Mail Config: Verify your admin panel's mail settings (SMTP/PHP Mail) are correctly configured, otherwise the email won't send.
- Path Accuracy: Confirm
$upload_info['file_path']and$upload_info['file_name']are correctly stored inoc_upload—the full path should point directly to the uploaded image file.
内容的提问来源于stack exchange,提问作者Nɪsʜᴀɴᴛʜ ॐ

