简体   繁体   中英

Mailx from within a C program?

I'm hoping to send a message via mailx to a list of recipients from within my C code.

I want to send an email containing the contents of the 'message' variable to everyone in the file /home/me/Email_List.txt.

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

I need help with both the C program and the mailx command. Here's my mailx command that doesn't quite work:

//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" 

I could write my text to a file before sending it, but that seems inefficient.

Thoughts?

Working at the command line, I got this to work for me (with my normal corporate email address in place of me@example.com , of course):

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

The -s option subject line was ignored, as hinted in the body text. (This is a mailx version 12.5 6/20/10 on a machine running a derivative of Ubuntu 12.04 LTS.)

Note that the To: line is case-sensitive and space-sensitive (at least there must be a space after the colon). This is standard notation for the headers defined by RFC-822 (or whatever its current incarnation is). When I tried with the -s option but no Subject: line, I got a message without a subject in my inbox.

The following should work for you:

$ 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
$

Note that blank line! Or you could use a plain echo; before the echo "Test text"; . The semicolons are needed in the { ... } notation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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