duansanzi5265 2016-10-12 13:47
浏览 48
已采纳

来自外部来源的电子邮件附件不起作用

I've recently created a page on our site where users can upload an image and email it to an email address set up specifically to keep the uploaded documents.

I've tested this myself and it works, with the attachments arriving in gmail as expected.

However, whenever someone from outside uses this feature the attachment in the email is unavailable, or not could not be loaded, when we try to open it.

The code is split between 2 files, a controller and a helper. Here's the code (For the sake of saving some space I've removed all error checks, but in the actual code they are all still in place and not picking up any errors whatsoever):

controller

$helper = [GET HELPER];
/** Upload the file to a temp location so that we can attach it to an email */
$uploader = new Varien_File_Uploader('filename');
$uploader->setAllowedExtensions(array(
    'image/jpeg',
    'image/jpg',
    'image/png',
    'application/pdf'
))
    ->setAllowRenameFiles(true)
    ->setFilesDispersion(false);
$path = $helper->getFileStorageLocation(); // Will store files in /tmp

if (!is_dir($path))
{
    mkdir($path, 0775, true);
}
$uploader->save($path, $_FILES['filename']['name']);

$result = $helper->sendMail($_FILES['filename']['name']);

if ($result)
{
    $uploadSuccess = true;
    /** Remove the temp file */
    unlink($path . DS . $_FILES['filename']['name']);
}

helper

/** Declare variables */
$order = Mage::getModel('sales/order')->load($orderId);
$file_incremented_id = $order->getIncrementId();
$copyTo = $this->getCopyTo();
$copyFrom = $this->getCopyFrom();
$subject = 'proof of upload for ' . $file_incremented_id;
$copyTo = explode(',', $copyTo);
$body = '<span>Please see attachment</span>';
$file = $this->getFileStorageLocation() . DS . $filename; // function receives filename from whatever is calling it
$attachment = file_get_contents($file);
$extension = pathinfo($file, PATHINFO_EXTENSION);

if (!$copyTo)
{
    return false;
}

$mail = Mage::getModel('core/email_template');
$mail->setSenderName('Uploader');
$mail->setSenderEmail($copyFrom);
$mail->setTemplateSubject($subject);
$mail->setTemplateText($body);
$mail->getMail()->createAttachment(
    $attachement,
    Zend_Mime::TYPE_OCTETSTREAM,
    Zend_Mime::DISPOSITION_ATTACHMENT,
    Zend_Mime::ENCODING_BASE64,
    $file_incremented_id . '.' . $extension // Set order number as file name
);

try
{
    $mail->send($copyTo);
    return true;
}
catch (Exception $e)
{
    return false;
}

Can anyone see anything that might be causing the issue, or think of what it might be based on my explanation of the setup?

  • 写回答

1条回答 默认 最新

  • dongxie9448 2016-10-14 11:46
    关注

    So the problem, in the end, was filesize. My fault for not posting the $_FILES variable.

    I saw it a bit later and the variable had error = 1, meaning that the file's size was larger than what was allowed by the max_upload_filesize in the php.ini

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备