Thread: need some help on email sending in C programming

  1. #1

    Unhappy need some help on email sending in C programming


    itslike this, we're working on this project a evaluation program for students and teachers to evaluate done courses etc, and one of the demands was that the program would send a mail to the teacher running the course when 90% of the registered students had done a evaluation, thing is I cant find anything on how to send a mail through C-code, I've tried custom System(" ") commands with pine and sendmail, but none will work, sendmail requiers root, and that means entering a password etc, pine requires you to type a msg every time, also tried "sudo" commando, and still requires you to enter a password. I really need your help and I would really appreciate it.

    A guy in need of help.

    ps ..

    We're running linux red hat.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    I dont do Linux coding, need to learn how to work with sockets

    Then a quick & dirty method;

    Connect to your SMTP server on port 25 .....(server could be

    send the following strings and end each with '\r\n'..

    MAIL FROM:<your email address>
    RCPT TO:<reciever's email address>
    FROM:Your name
    TO:reciever's name
    Blah blah blah
    Then send '\r\n' twice and send;

    Substitute the italic parts for info specific to the email...

    This is off the top of my head, so I havent tested it.....try it on Telnet and substitute each '\r\n' for the ENTER key

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    What about 'mail', 'Mail' or 'mailx' via a system("") call?

    I seem to remember something like this...
    system( "mail -s subject user@host < message.txt" );

    I would be mildly surprised if say pine did not have a non-interactive mode - getting programs to send email is a fairly common thing.

  4. #4
    Registered User
    Join Date
    Jan 2002

    Cool Using sendmail from C

    Don't mess with sockets and crap! It's really a very simple problem.

    Sendmail doesn't require root, of course it doesn't since the mail system and each user calls it! I use sendmail from my C programs all the time.

    Find out where sendmail is on your system.
    Typically /usr/lib/sendmail but sometimes /usr/bin/sendmail or /usr/sbin/sendmail (sometimes it's linked from one to the other so it doesn't matter).

    To avoid dangerous security situations where an argument from a user (passed from a web page for example) includes escape sequences that can execute arbitrary commands (a BAD THING) don't pass any user arguments to sendmail. Instead, call sendmail with the -t option, this means you will have the recipient specified in the file. So, write a temp file with the email header and body in it, and then pass it to sendmail.

    The temp message file should look like this:

    Subject: This is my email program

    This is the message body blah blah blah

    NOTE: The temp message file includes the actual MIME headers that sendmail expects, so it's important that they are written in the file just like that -- Initial capital letter, space, colon, etc. Also, the blank line after the Subject: is very important -- the blank line signals the end
    of the MIME header and the beginning of the body.

    Once your program has generated the temp file, use a system call to sendmail to push it out the door:

    system( "/usr/lib/sendmail -t < mytempfile.txt" );

    Note the "less than" sign means for sendmail to
    read its input from that file.

    For the system() call, make sure you #include <stdlib.h>

    Thats it -- let me know if it works out for you.


  5. #5

    Lightbulb Mail in C code

    To Walter:

    Tnx for the help, it works ! I dunno how many hours I've spent trying to find a solution that would work. thanks all you guy who helped me out on this one.

  6. #6
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Fordy, can you tell little more about your way of doing that?
    Any reference doc from we.. and sample code.. will be helpfull.

    How / where to put your given code?
    [ Never code before desk work ]
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C code for Sending Email with attachment
    By Mr coder in forum C Programming
    Replies: 5
    Last Post: 03-06-2009, 08:15 AM
  2. Spam Filters. ISP based email is dying.
    By Mario F. in forum A Brief History of
    Replies: 14
    Last Post: 03-05-2008, 12:05 PM
  3. Sending Email via C++?
    By Monte in forum C++ Programming
    Replies: 0
    Last Post: 07-30-2002, 01:14 PM
  4. sending strings from program to email
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2002, 06:23 PM
  5. sending email from (C++)exe file through ASP
    By priya in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2002, 10:14 AM