• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

Sending E-Mail in HP-UX using C++

Dear experts,

I would like to write a C++ program in HP-UX to send e-mails to outside. How can I write such a program without using a third party tool or library and just using the supplied default environment?

BR,
0
GurcanK
Asked:
GurcanK
  • 2
3 Solutions
 
evilrixSenior Software Engineer (Avast)Commented:
If you're prepared to learn to communicate with an SMTP server and talk SMTP you can do this by just writing a sockets program that connects directly to the MTA and talks SMTP with it. The SMTP protocol is pretty simple and easy to learn and code.

http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol

A list of the SMTP commands: http://www.cs.cf.ac.uk/Dave/PERL/node175.html

You'll also need to know how to construct MIME headers but, again, this isn't too hard.
http://en.wikipedia.org/wiki/MIME
0
 
GurcanKAuthor Commented:
Hi, that's good feedback. However, I need some code example. Could you please provide some?
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> However, I need some code example. Could you please provide some?
I don't have any to hand -- if I get a chance later I'll try and knock up a quick example for you.
0
 
jkrCommented:
If you don't want to go down to the SMTP protocol level, sending a mail by shelling out 'sendmail' is quite handy also. The following snippet does exactly that (originally written to automatically notify webmasters of dead links):
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>

/*
   gcc  -Wall -pedantic -pipe -O5 -o err404 err404.c; strip err404
*/  


#define MAIL            "/path/to/sendmail"        
#define EDITORS         "/path/to/notfound.editors"

#define W3ADMIN         "www@example.org"


int mail2admin(char* url, char* link)
{ FILE *pipe, *fp;  
  char *buff, *email;
  int found=0;  if (!url||!link ) return -1;
  buff =(char*)malloc(1024);
  email=(char*)malloc(1024);
  if (!!(fp=fopen(EDITORS,"r")))
  { for (;!found;)
    { if (!fgets(buff,1000,fp)) break;
      if (*buff=='#' ||!strchr(buff,'\t')) continue;
      strcpy(email,strrchr(buff,'\t')+1);
      *strchr(buff,'\t')='\0';;          
      if (strstr(url,buff)) found++;
    }
    fclose(fp);
  }
  sprintf(buff,"%s -f %s %s",MAIL,W3ADMIN,found?email:W3ADMIN);
  pipe=popen(buff,"w");
  fprintf(pipe,"Subject: Missing link  ;-))\n");
  fprintf(pipe,"Reply-to: %s\n",W3ADMIN);
  fprintf(pipe,"Full-name: Web-Server\n\n");
  fprintf(pipe,"Hello,\n\nHost *%s* has just accessed\n%s\n\n"
          ,getenv("REMOTE_HOST")?getenv("REMOTE_HOST"):""
          ,url);
  fprintf(pipe,"where an image is referenced ");
  fprintf(pipe,"%s that das not exist.\n",link);
  fprintf(pipe,"\n\nRegards,\n\nYour Server.\n\n");
  if (getenv("HTTP_USER_AGENT"))
  { fprintf(pipe,"PS. Browser: %s",getenv("HTTP_USER_AGENT"));
  }
  pclose(pipe);
  free(buff);
  free(email);
  return 0;
}

Open in new window

0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now