【问题标题】:Mailx from within a C program?来自 C 程序的 Mailx?
【发布时间】:2014-06-13 17:59:19
【问题描述】:

我希望通过 mailx 从我的 C 代码中向收件人列表发送消息。

我想向 /home/me/Email_List.txt 文件中的每个人发送一封包含“message”变量内容的电子邮件。

if(send_email)
{
  char* message = "Testing email";
  //send contents of  'message' to everyone in /home/me/Email_List.txt
}

我需要 C 程序和 mailx 命令方面的帮助。这是我的 mailx 命令,它不太有效:

//This works, but I don't want to send the contents of Email_List.txt
cat /home/me/Email_List.txt /home/me/Email_List.txt | mailx -t -s "Test"

//This doesn't work, error:
//cat: cannot open Test Text
cat /home/me/Email_List.txt "Test Text" | mailx -t -s "Test" 

我可以在发送之前将文本写入文件,但这似乎效率低下。

想法?

【问题讨论】:

  • 这与mailx无关。 cat 需要一个文件名列表来打开并吐出它们的内容。 cat "Test Text" 将直接查找名为 Test Text 的文件并尝试打开它 - 但失败了。
  • 在第二个命令中使用 echo "Test Text" 而不是 cat。它正在尝试打开具有该名称的文件
  • 将 cat 更改为 echo 让我“没有指定收件人”
  • @user3704313 没错。您尚未指定接收方。 echo "hi" | mailx -t -s "Test" user@example.com 使用得当。使用它你应该(希望)能够编写一个循环并做你想做的事。
  • 你必须在你的 mailx 命令中提到收件人。我认为它像这样 mailx -s "Test" user_name

标签: c shell concatenation mailx


【解决方案1】:

在命令行工作,我得到了这个为我工作(当然,用我的普通公司电子邮件地址代替 me@example.com):

mailx -s "Just a test" -t <<EOF
To: me@example.com
Subject: Just a test with a subject

Just testing mailx -t which seems to ignore -s options too
-=JL=-
EOF

-s 选项主题行被忽略,如正文中所示。 (这是运行 Ubuntu 12.04 LTS 衍生版本的机器上的 mailx 版本 12.5 6/20/10。)

注意To: 行区分大小写和空格(至少冒号后必须有空格)。这是 RFC-822(或其当前版本)定义的标头的标准符号。当我尝试使用-s 选项但没有Subject: 行时,我的收件箱中收到了一条没有主题的消息。

以下内容应该适合您:

$ cat /home/me/Email_list.txt
To: My.email@company.com
Subject: Test email

$ { cat /home/me/Email_List.txt; echo "Test Text"; } | mailx -t
$

注意空行!或者你可以在echo "Test text"; 之前使用一个普通的echo;{ ... } 表示法中需要分号。

【讨论】:

  • 行得通!现在我需要在我的 C 程序中调用该行。我可以使用 system() ,对吧?
  • 是的;您应该能够使用system() 将该命令行作为字符串参数(文字字符串,或在运行时从片段构建的字符串。
猜你喜欢
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 2023-04-06
  • 2018-05-21
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
相关资源
最近更新 更多