【问题标题】:Attach xlsx to email with MIME::Lite使用 MIME::Lite 将 xlsx 附加到电子邮件
【发布时间】:2014-08-21 16:20:46
【问题描述】:

我正在尝试使用Excel::Writer::XLSXMIME::Lite 发送附有xlsx 文件的电子邮件。 excel文件的生成工作,因为我可以scp并在Excel中打开文件而没有任何问题。当我尝试从我的电子邮件客户端(Outlook 2013)打开附件时,我收到此错误:

“Excel 无法打开文件“from_2014-06_to_2014-07.xlsx”,因为 文件格式或文件扩展名无效。确认文件有 未损坏且文件扩展名与 文件。”

outlook 显示的文件大小是 444B,但实际上是 95K。我之前一直使用Spreadsheet::WriteExcel 和 mime 类型“application/vnd.ms-excel”发送 xls 文件,没有任何问题。

这是我尝试发送的电子邮件:

sub send_mail{
    my $filename = shift;
    my $to_email = shift;
    my $from_email = shift;
    my $date = shift;
    $filename = shift;

    my $mail = MIME::Lite->new(
        'From'          => '$from_email',
        'To'            => $to_email,
        'Subject'       => "Radio/TV stats $date",
        'Type'          => 'multipart/mixed',
        #'content-type'  => 'application/zip',
        #'Data'       => "Here is your stuff",
    );

    $mail->attach(
        'Type'          => 'TEXT',
        'Data'          => "Here is your stuff",
    );

    $mail->attach(
        #'Type'          => 'application/vnd.ms-excel',
        'Type'          => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        'Path'          => $filepath,
        'Filename'      => $filename,
        'Disposition'   => 'attachement',
    );

    $mail->send('sendmail');
}

谁能帮我附上 xlsx 文件?

【问题讨论】:

  • 试试'Data' => "File size:". -s $filepath
  • '数据' => "文件大小"。 -s $filepath 返回 0。ls -la 返回 97403。而且附件仍然不起作用。
  • 所以 $filepath 无论是绝对路径还是相对路径都是错误的。还有use strict;

标签: excel perl mime-types


【解决方案1】:

shift 两次到 $filename(第 2 个和第 6 个字符串)并且变量 $filepath 没有被声明。可能是这里出错了?

【讨论】:

  • 啊啊啊……一定要到凌晨。谢谢你帮助我!
  • 不能在“Path => $filepath”中使用绝对路径
  • 附加 xlsx 文件时,我还必须先关闭 excel::writer。如果我没有关闭它,绝对路径不起作用。
猜你喜欢
  • 1970-01-01
  • 2018-01-16
  • 2013-09-09
  • 2019-04-06
  • 2011-05-23
  • 2017-11-07
  • 2017-08-30
  • 1970-01-01
  • 2015-07-09
相关资源
最近更新 更多