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

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

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页