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 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测