T
T
Teoring2016-04-12 12:17:49
C++ / C#
Teoring, 2016-04-12 12:17:49

How to connect and send mail using Postfix and Unix domain socket?

I managed to set up the server and implement sending letters via TCP socket (I write in C ++), but due to the fact that the server will only be outgoing (incoming messages are not provided) and in order to improve performance (thousands of letters, now I fork the process to send them calling for each letter popen (sendmail)), I need to implement the connection and sending letters through the unix domain socket. In the documentation, I found information on how to switch the smtp mode from tcp to unix service type, a lot of information about the Policy server and more, but I could not figure out where this very Unix Domain Socket lies on the file system and how to actually connect and send letters ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Teoring, 2016-04-12
@Teoring

I solved this problem.
After switching the Service Type in master.cf, the socket appeared at the following path
/var/spool/postfix/public/smpt
The code for connecting and sending a letter used the following:

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <cstdlib>
#include <iostream>
#include <netdb.h>
#include <stdio.h>

#define DATA "Half a league, half a league . . ."

using namespace std;

#define HELO "HELO 192.168.1.1\r\n"
#define DATA "DATA\r\n"
#define QUIT "QUIT\r\n"


//#define h_addr h_addr_list[0]
//FILE *fin;
int sock;
struct sockaddr_un server;
struct hostent *hp, *gethostbyname();
char buf[BUFSIZ+1];
int len;
char *host_id="192.168.1.10";
char *from_id="[email protected]";
char *to_id="[email protected]";
char *sub="testmail\r\n";
char wkstr[100]="hello how r u\r\n";

/*=====Send a string to the socket=====*/

void send_socket(char *s)
{
    write(sock,s,strlen(s));
    write(1,s,strlen(s));
    //printf("Client:%s\n",s);
}

//=====Read a string from the socket=====*/

void read_socket()
{
    len = read(sock,buf,BUFSIZ);
    write(1,buf,len);
    //printf("Server:%s\n",buf);
}

/*=====MAIN=====*/
int main(int argc, char* argv[])
{

    /*=====Create Socket=====*/
    sock = socket(AF_UNIX, SOCK_STREAM, 0);;
    if (sock==-1)
    {  
        perror("opening stream socket");
        exit(1);
    }
    else
        cout << "socket created\n";

    /*=====Verify host=====*/
    server.sun_family = AF_UNIX;
    strcpy(server.sun_path, "/var/spool/postfix/public/smtp");

    if (connect(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) 
    {
          close(sock);
          perror("connecting stream socket");
          exit(1);
     }
    else
        cout << "Connected\n";
    /*=====Write some data then read some =====*/
    read_socket(); /* SMTP Server logon string */
    send_socket(HELO); /* introduce ourselves */
    read_socket(); /*Read reply */
    send_socket("MAIL FROM: "); 
    send_socket(from_id);
    send_socket("\r\n");
    read_socket(); /* Sender OK */
    send_socket("VRFY ");
    send_socket(from_id);
    send_socket("\r\n");
    read_socket(); // Sender OK */
    send_socket("RCPT TO: "); /*Mail to*/
    send_socket(to_id);
    send_socket("\r\n");
    read_socket(); // Recipient OK*/
    send_socket(DATA);// body to follow*/
    send_socket("Subject: ");
    send_socket(sub);
    read_socket(); // Recipient OK*/
    send_socket(wkstr);
    send_socket(".\r\n");
    read_socket(); 
    send_socket(QUIT); /* quit */
    read_socket(); // log off */

    //=====Close socket and finish=====*/
    close(sock);
    exit(0);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question