Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sendmail

Posted on 1998-07-29
14
450 Views
Last Modified: 2013-12-26
How do I send mail from my application to a group of people. I guess that I could use sendmail. How do I program sendmail to achieve this task ? I don't have much idea about sendmail
0
Comment
Question by:nramesh30
14 Comments
 
LVL 5

Expert Comment

by:ecw
ID: 1295197
Use a standard mail client (eg. mail) and popen (see example), though in production code add error checking.

FILE *mail = popen("/usr/bin/mail user1 user2 user3", "w");

if (mail) {
  fprintf(mail, "Subject: a report\n\n");
  fprintf(mail, "all ok.\n");
  pclose(mail);
}

0
 

Author Comment

by:nramesh30
ID: 1295198
The user list to whom I have to send mail to , will be generated in my application. How do handle this situation ? I heard that sendmail would be very flexible to use for my case.
0
 

Expert Comment

by:bjornh
ID: 1295199
Try this:

FILE *sendmail = popen("/usr/lib/sendmail -t");
if(sendmail)
{
  fprintf(sendmail, "To: %s, %s, %s\n", user[0], user[1], user[2]);
  fprintf(sendmail, "From: who@where.com\n");
  fprintf(sendmail, "Subject: test\n\n");
  fprintf(sendmail, "Body");
}
close(sendmail);
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:nramesh30
ID: 1295200
thanks guys. that works perfectly fine. Is it possible to hide the user list to be shown up on the To field in the mail. Is it possible to program in such a way that the To field shows some thing like 'To undisclosed recipeints'

Thanks
0
 

Author Comment

by:nramesh30
ID: 1295201
thanks guys. that works perfectly fine. Is it possible to hide the user list to be shown up on the To field in the mail. Is it possible to program in such a way that the To field shows some thing like 'To undisclosed recipeints'

Thanks
0
 

Author Comment

by:nramesh30
ID: 1295202
thanks guys. that works perfectly fine. Is it possible to hide the user list to be shown up on the To field in the mail. Is it possible to program in such a way that the To field shows some thing like 'To undisclosed recipeints'

Thanks
0
 

Author Comment

by:nramesh30
ID: 1295203
thanks guys. that works perfectly fine. Is it possible to hide the user list to be shown up on the To field in the mail. Is it possible to program in such a way that the To field shows some thing like 'To undisclosed recipeints'

Thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 1295204
Bcc:
0
 

Expert Comment

by:ijduggan
ID: 1295205
You can do this using perl to. The answer above in C is fine, but this includes all your options

#!/usr/bin/perl

$to = 'someone@somewhere.com';
@hidden_recipients('guy1@company.one','guy2@company.one');


open(SENDMAIL, "sendmail -t|");
print SENDMAIL, "To: $to", "\n";
print SENDMAIL, "Bcc: ", join(",",@hidden_recipients), "\n";
print SENDMAIL, "--------", "\n";
print SENDMAIL <<EOF
Hey,
This is an email I am sending you.
Later.
EOF

close(SENDMAIL);
exit;

OR YOU CAN ADD THE BCC PART TO THE C CODE FROM ABOVE

FILE *sendmail = popen("/usr/lib/sendmail -t");
if(sendmail)
{
   fprintf(sendmail, "To: %s, %s, %s\n", user[0], user[1], user[2]);
   fprintf(sendmail, "Bcc: %s, %s, %s\n", user[0], user[1], user[2]);
   fprintf(sendmail, "From: who@where.com\n");
   fprintf(sendmail, "Subject: test\n\n")
   fprintf(sendmail, "Body");
}
       close(sendmail);
0
 

Author Comment

by:nramesh30
ID: 1295206
Thanks ijduggan,

Can I write a program which reads all the email addresses from a flat file and send emails to all of them simultaneously using sendmail ?
0
 

Accepted Solution

by:
ijduggan earned 40 total points
ID: 1295207
Yes. You just need to send them to the STDIN of the program.

Using Perl:

To read off STDIN:

#!/usr/bin/perl

while(<STDIN>) {
    chop;
    $to = $_;

    open(SENDMAIL, "sendmail -t|");
    print SENDMAIL, "To: $to", "\n";
    print SENDMAIL, "--------", "\n";
    print SENDMAIL <<EOF
Hey,
This is an email I am sending you.
Later.
    EOF

    close(SENDMAIL);
}
exit;


This will read the files off of stdin. Put all of the addresses into a file, each on a seperate
line. Then call the program like this:

emailprogram < filewithaddress.txt

If you want to have the program read the file for you, open the file like this:

##!/usr/bin/perl

$namesfile = "filewithaddresses.txt";
open(NAMESFILE, ">filewithaddresses")
    or die "Couldn't open file $namesfile. $!";
while(<NAMESFILE>) {
    chop;
    $to = $_;

    open(SENDMAIL, "sendmail -t|");
    print SENDMAIL, "To: $to", "\n";
    print SENDMAIL, "--------", "\n";
    print SENDMAIL <<EOF
Hey,
This is an email I am sending you.
Later.
    EOF

    close(SENDMAIL);
}
exit;

0
 

Author Comment

by:nramesh30
ID: 1295208
That's wonderful. But i'am not a Perl person . what does chop and $_ do ? how do I do this in C?
0
 

Expert Comment

by:ijduggan
ID: 1295209
It would be much easier to answer your question if you would be more specific about your needs... C is pretty much the same. All you have to do is read the values off stdin or a from a file and use the C code above to send it. If you want to read the names from a file, look to the man pages if you don't know file io in C.

man fopen
man fdopen

STDIN would be the file descriptor you need.

0
 

Author Comment

by:nramesh30
ID: 1295210
Instead of sending mail to the email addressess in the list ONE BY ONE, is it possible to send a bulk email simultaneously, at a time to al the recipients ?

some thing like
sendmail < listofrecipeints
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 169
maven archtype selection in eclipse 1 57
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 826
Excel file not created as expected 7 79
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question