简体   繁体   English

Perl-如何发送本地邮件?

[英]Perl - How to send local mail?

I would like to integrate the following terminal command into a Perl script. 我想将以下终端命令集成到Perl脚本中。

Terminal command: 终端命令:

mutt -s "User Monitoring" -a "/home/mipa/Documents/System_Monitoring/protocol_name.csv" -- mipa@localhost.localdomain

The command sends local mail containing a file attachment to a user on the same system. 该命令将包含文件附件的本地邮件发送到同一系统上的用户。

I have a small problem with the command though. 我的命令虽然有一个小问题。 It does seem to require more user interaction than just the command listed here. 它似乎确实需要更多的用户交互,而不仅仅是这里列出的命令。 The command requires the user to follow a menu to confirm the values and hit the "y" key to send. 该命令要求用户遵循菜单来确认值并按“ y”键进行发送。

My question here is two-folded. 我的问题有两个方面。 Is there a similar mail command that does not require user interaction and works by just following a single command with predefined flags? 是否有类似的邮件命令不需要用户交互并且仅需遵循带有预定义标志的单个命令即可工作? And how would I integrate this command into a Perl script where I would be able to choose the file name, and the receiving user followed by issuing the command? 以及如何将该命令集成到Perl脚本中,在该脚本中我可以选择文件名和接收用户,然后发出命令?

Any guidance regarding a possible solution is highly appreciated. 高度赞赏有关可能解决方案的任何指导。

  1. there are a few ways to send command line emails in Linux: How do I send a file as an email attachment using Linux command line? 在Linux中有几种发送命令行电子邮件的方法: 如何使用Linux命令行作为电子邮件附件发送文件?
  2. why is the -- in your command? 为什么--在您的命令中? that maybe confusing mutt . 也许混淆mutt
  3. https://unix.stackexchange.com/questions/108916/automatically-attach-a-file-to-a-mail-with-mutt has a few more suggestions for sending mail with mutt . https://unix.stackexchange.com/questions/108916/automatically-attach-a-file-to-a-mail-with-mutt还有一些关于使用mutt发送邮件的建议。

I prefer to use MIME::Lite to send emails, rather than spawning an external command, which avoids the problems you are having. 我更喜欢使用MIME :: Lite发送电子邮件,而不是生成外部命令,这样可以避免出现问题。 MIME::Lite is able to handle sending emails with attachments. MIME :: Lite能够处理带有附件的电子邮件发送。

Here is a quick example: 这是一个简单的示例:

#!/usr/bin/perl

use strict;
use MIME::Lite;

my $msg = MIME::Lite->new(
    To      => 'foo.bar@foobar.com',
    Subject => 'Test message with attachments',
    Type    => 'multipart/mixed'
);

$msg->attach(
    Type     => 'TEXT',
    Data     => "Here's the file you wanted"
);
$msg->attach(
    Type     => 'image/png',
    Path     => 'somefile.png',
    Filename => 'somefile.png',
    Disposition => 'attachment'
);

$msg->send();

This would send a message containing a small amount of text and a single attachment. 这将发送一条包含少量文本和单个附件的消息。

There are a lot more examples given in the POD for MIME::Lite . POD中为MIME :: Lite提供了更多示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM