Witam nie uzyskalem odpowiedzi w dziale newbie wiec pytam:
Przeszukalem forum i nie znalazlem odpowiedzi: jak wyslac maila bez zalacznika w vc++
Jakie funkcje sa potrzebne aby to zrobic ??
Z gory dzieki za odpowiedz.
kwestia opanowania socketow
http://www.php.net/manual/pl/ref.mail.php#66705
php: if(substr($res,0,3) != "221") ... c: if(strncmp(res+0,"221",3) // to '+0' moesz sobie darowac.
// wlasciwie jak bede mial troche czasu chetnie to chocby dla siebie przepisze.
Kwestia tylko obslugi socketow i wysylania, moze np. write/read zamiast send/recv
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#define DEFAULTHEADER "MIME-Version: 1.0\r\nContent-Type: text/plain; charset=\"ISO8859-2\"\r\nContent-Transfer-Encoding: 8bit\r\n"
void encodeblock(unsigned char* in,unsigned char* out,int len){
// na podstawie http://base64.sourceforge.net/b64.c
static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
out[0]=cb64[in[0]>>2];
out[1]=cb64[((in[0]&0x03)<<4)|((in[1]&0xf0)>>4)];
out[2]=(unsigned char)(len>1?cb64[((in[1]&0x0f)<<2)|((in[2]&0xc0)>>6)]:'=');
out[3]=(unsigned char)(len>2?cb64[in[2]&0x3f]:'=');
}
char* base64encode(char* ins,char* outs,int len){
// outs has to be (4/3*len)+1 (len is length of ins)
char* p=outs;
unsigned char in[3];
int i=0;
while(i<len){
int j=0;
while(j<3 && i<len)in[j++]=ins[i++];
int k=j;
while(k<3)in[k++]=0;
encodeblock(in,(unsigned char*)p,j);
p+=4; // move out ;]
}
*p=0; // you know, '\0' at the end of string
return outs;
}
int mail(char* smtpaddr,char* login,char* pass,char* from,char* to,char* subject,char* message,unsigned port=25,char* msgheader=DEFAULTHEADER){
int sd=socket(AF_INET,SOCK_STREAM,0); // lub PF_INET jeden wal
if(sd<0)return 1;
struct hostent *smtp=gethostbyname(smtpaddr);
if(!smtp)return 2;
struct sockaddr_in sa;
bzero(&sa,sizeof(sa)); // memset(&sa,0,sizeof(sockaddr_in));
sa.sin_port=htons(port);
sa.sin_family=AF_INET;
memcpy(&sa.sin_addr.s_addr,smtp->h_addr,smtp->h_length);
if(connect(sd,(struct sockaddr*)&sa,sizeof(sa))<0)return 3;
char buf[512];
int n=recv(sd,buf,512,0);
int ret=4+(n==-1); // od tej pory zwrocona wartosc: nieparzysta=blad socketa, parzysta=zla odpowiedz smtp
if(n!=-1 && !strncmp(buf,"220",3)){ // Failed to connect ?
sprintf(buf,"HELO %s\r\n",smtpaddr); // strcat(strcat(strcpy(buf,"HELO "),smtpaddr),"\r\n");
send(sd,(const void*)buf,strlen(buf),MSG_NOSIGNAL); // no need SIGPIPE handling :)
ret=6+(n==-1);
n=recv(sd,buf,512,0);
if(n!=-1 && !strncmp(buf,"250",3)){ // Failed to Introduce ?
sprintf(buf,"auth login\r\n"); // strcpy(buf,"auth login\r\n");
send(sd,(const void*)buf,strlen(buf),MSG_NOSIGNAL);
n=recv(sd,buf,512,0);
ret=8+(n==-1);
if(n!=-1 &&(!strncmp(buf,"334",3)||!strncmp(buf,"504",3))){ // Failed to Initiate Authentication ? 504 5.3.3 AUTH mechanism login not available
if(strncmp(buf,"504",3)){
strcat(base64encode(login,buf,strlen(login)),"\r\n"); // tu sie sprintf po prostu nie da uzyc
send(sd,(const void*)buf,strlen(buf),MSG_NOSIGNAL);
n=recv(sd,buf,512,0);
}
ret=10+(n==-1);
if(n!=-1 &&(!strncmp(buf,"334",3)||!strncmp(buf,"504",3))){ // Failed to Provide Username for Authentication ?
if(strncmp(buf,"504",3)){
strcat(base64encode(login,buf,strlen(pass)),"\r\n");
send(sd,(const void*)buf,strlen(buf),MSG_NOSIGNAL);
n=recv(sd,buf,512,0);
}
ret=12+(n==-1);
if(n!=-1 &&(!strncmp(buf,"235",3)||!strncmp(buf,"504",3))){ // Failed to Authenticate ?
sprintf(buf,"MAIL FROM: %s\r\n",from); // strcat(strcat(strcpy(buf,"MAIL FROM: "),from),"\r\n");
send(sd,(const void*)buf,strlen(buf),MSG_NOSIGNAL);
n=recv(sd,buf,512,0);
ret=14+(n==-1);
if(n!=-1 && !strncmp(buf,"250",3)){ // MAIL FROM failed ?
sprintf(buf,"RCPT TO: %s\r\n",to); // strcat(strcat(strcpy(buf,"RCPT TO: "),to),"\r\n");
send(sd,(const void*)buf,strlen(buf),MSG_NOSIGNAL);
n=recv(sd,buf,512,0);
ret=16+(n==-1);
if(n!=-1 && !strncmp(buf,"250",3)){ // RCPT TO failed ?
sprintf(buf,"DATA\r\n"); // strcpy(buf,"DATA\r\n");
send(sd,(const void*)buf,strlen(buf),MSG_NOSIGNAL);
n=recv(sd,buf,512,0);
ret=18+(n==-1);
if(n!=-1 && !strncmp(buf,"354",3)){ // DATA failed ?
sprintf(buf,"To: %s\r\nFrom: %s\r\nSubject: %s\r\n",to,from,subject);
send(sd,(const void*)buf,strlen(buf),MSG_NOSIGNAL);
send(sd,(const void*)msgheader,strlen(msgheader),MSG_NOSIGNAL);
sprintf(buf,"\r\n\r\n");
send(sd,(const void*)buf,strlen(buf),MSG_NOSIGNAL);
send(sd,(const void*)message,strlen(message),MSG_NOSIGNAL);
sprintf(buf,"\r\n.\r\n");
send(sd,(const void*)buf,strlen(buf),MSG_NOSIGNAL);
n=recv(sd,buf,512,0);
ret=20+(n==-1);
if(n!=-1 && !strncmp(buf,"250",3)){ // message body failed ?
sprintf(buf,"QUIT\r\n");
send(sd,(const void*)buf,strlen(buf),MSG_NOSIGNAL);
n=recv(sd,buf,512,0);
ret=22+(n==-1);
if(n!=-1 && !strncmp(buf,"221",3))ret=0; // QUIT failed ? // ret=0 - everything is just ok
}
}
}
}
}
}
}
}
}
close(sd);
return ret;
}
int main(){
int i;
if(i=mail("131.202.32.19","login","haslo","sysop@somwhere.pl","johhnny@o2.pl","elemeledudki - test mail()","ALA MA KOTA\r\nalamakota\r\nAla ubila kota\r\n")){
printf("mail() dalo dupy, return code: %d\n",i);
}
}
a wiadomosc otrzymales:
log mailserwera:
Feb 28 13:34:09 mail sendmail[8945]: l1SCY8WF008938: to=johhnny@o2.pl, delay=00:00:01, xdelay=00:00:00, mailer=esmtp, pri=120212, relay=[10.131.1.41] [10.131.1.41], dsn=2.0.0, stat=Sent (l1SCY9ue002356 Message accepted for delivery)
log relay'a:
Feb 28 13:34:10 viruswall sendmail_out[2359]: l1SCY9ue002356: to=johhnny@o2.pl, delay=00:00:01, xdelay=00:00:01, mailer=esmtp, pri=120549, relay=mx8.go2.pl. [193.17.41.48], dsn=2.0.0, stat=Sent (Ok)