| iMatix home page | << | < | > | >> |
![]() Version 1.91 |
#include "sflmail.h" int smtp_send_mail ( char *strSmtpServer, char *strMessageBody, char *strSubject, char *strSenderUserId, char *strDestUserIds, char *strCcUserIds, char *strBccUserIds, char *strRetPathUserId, char *strRrcpUserId, char *strMsgComment, char *strMailerName, char *strBinFiles, char *strTxtFiles)
Format and send a SMTP message. This function gives you the options of sneding to multi receivers, CC's, Bcc's and also send UUencoded attachments. Receivers and files are ";" or "," terminated. NOTE: The sock_init function should be called before use of this function.
{ FILE *fpin; int iCnt; sock_t iSocket; char strOut[514], strFile[256], strRetBuff[513]; char strUUEFile[256], *strRcptUserIds; int iOld_ip_nonblock = ip_nonblock; /* Make sure we do not block. */ ip_nonblock = FALSE; /* Open up the SMTP port (25 most of the time). */ iSocket = connect TCP (strSmtpServer, "smtp"); if (getreply (iSocket) > 400 || iSocket < 1) { return -1; } /* Format a SMTP meassage header. */ /* Just say hello to the mail server. */ xstrcpy (strOut, "HELO ", strSmtpServer, "\n", NULL); smtp_send_data (iSocket, strOut); if (getreply (iSocket) > 400) return -2; /* Tell the mail server who the message is from. */ xstrcpy (strOut, "MAIL FROM:<", strSenderUserId, ">\n", NULL); smtp_send_data (iSocket, strOut); if (getreply (iSocket) > 400) return -3; strRcptUserIds = (char *) malloc (strlen (strDestUserIds) + strlen (strCcUserIds) + strlen (strBccUserIds) + 1); sprintf (strRcptUserIds, "%s;%s;%s", strDestUserIds, strCcUserIds, strBccUserIds); /* The following tells the mail server who to send it to. */ iCnt = 0; while (1) { getstrfld (strRcptUserIds, iCnt++, 0, ",;", strRetBuff); if (*strRetBuff) { xstrcpy (strOut, "RCPT TO:<", strRetBuff, ">\r\n", NULL); smtp_send_data (iSocket, strOut); if (getreply (iSocket) > 400) return -4; } else break; } free (strRcptUserIds); /* Now give it the Subject and the message to send. */ smtp_send_data (iSocket, "DATA\r\n"); if (getreply (iSocket) > 400) return -5; /* The following shows all who it was sent to. */ replacechrswith (strDestUserIds, ";", ','); xstrcpy (strOut, "TO: ", strDestUserIds, "\r\n", NULL); /* Set up the Reply-To path. */ if (!strRetPathUserId || !*strRetPathUserId) { strRetPathUserId = strSenderUserId; } xstrcat (strOut, "Reply-To:<", strRetPathUserId, ">\r\n", NULL); smtp_send_data (iSocket, strOut); *strOut = '\0'; /* Post any CC's. */ if (strCcUserIds && *strCcUserIds) { replacechrswith (strCcUserIds, ";", ','); xstrcat (strOut, "Cc:", strCcUserIds, "\r\n", NULL ); } /* Post any BCC's. */ if (strBccUserIds && *strBccUserIds) { replacechrswith (strBccUserIds, ";", ','); xstrcat (strOut, "Bcc:", strBccUserIds, "\r\n", NULL); } /* Post any Return-Receipt-To. */ if (strRrcpUserId && *strRrcpUserId) { xstrcat (strOut, "Return-Receipt-To:", strRrcpUserId, ">\r\n", NULL); } if (strMailerName && *strMailerName) { xstrcat (strOut, "X-Mailer: ", strMailerName, "\r\n", NULL); } else { strcat (strOut, "X-Mailer: sflmail function\r\n"); } /* Set the mime version. */ strcat (strOut, "MIME-Version: 1.0\r\n"); strcat (strOut, "Content-Type: Multipart/Mixed; boundary=Message-Boundary-21132\r\n"); smtp_send_data (iSocket, strOut); /* Write out any message comment included. */ xstrcpy (strOut, "Comments: ", strMsgComment, "\r\n", NULL); /* Send the subject and message body. */ xstrcat (strOut, "Subject:", strSubject, "\n\r\n", NULL); /* Keep rfc822 in mind with all the sections. */ if (strMessageBody && *strMessageBody) { strcat (strOut, "\r\n--Message-Boundary-21132\r\n"); strcat (strOut, "Content-Type: text/plain; charset=US-ASCII\r\n"); strcat (strOut, "Content-Transfer-Encoding: 7BIT\r\n"); strcat (strOut, "Content-description: Body of message\r\n"); xstrcat (strOut, "\r\n", strMessageBody, "\r\n", NULL); } smtp_send_data (iSocket, strOut); /* Include any Text type files and Attach them to the message. */ if (strTxtFiles && *strTxtFiles) { iCnt = 0; while (1) { getstrfld (strTxtFiles, iCnt++, 0, ",;", strFile); trim (strFile); if (*strFile) { fpin = fopen (strFile, "rb"); if (!fpin) { return -6; } strcpy (strOut, "\r\n--Message-Boundary-21132\r\n"); strcat (strOut, "Content-Type: text/plain; charset=US-ASCII\r\n"); strcat (strOut, "Content-Transfer-Encoding: 7BIT\r\n"); xstrcat (strOut, "Content-Disposition: attachment; filename=", getfilename (strFile), "\r\n\n", NULL); smtp_send_data (iSocket, strOut); while (!feof (fpin)) { memset (strRetBuff, 0, 513); fread (strRetBuff, sizeof (char), 512, fpin); smtp_send_data (iSocket, strRetBuff); } fclose (fpin); } else break; } } /* UUencode any bin files and Attach them to the message. */ if (strBinFiles && *strBinFiles) { iCnt = 0; while (1) { getstrfld (strBinFiles, iCnt++, 0, ",;", strFile); trim (strFile); if (*strFile) { strcpy (strUUEFile, strFile); if (strchr (strUUEFile, '.')) *((strchr (strUUEFile, '.')))= (char)NULL; strcat (strUUEFile, ".uue"); uuencode (strFile, strUUEFile); fpin = fopen (strUUEFile, "rb"); if (!fpin) { return -6; } strcpy (strOut, "\r\n--Message-Boundary-21132\r\n"); xstrcat (strOut, "Content-Type: application/octet-stream; name=", getfilename (strFile), "\r\n", NULL); strcat (strOut, "Content-Transfer-Encoding: x-uuencode\r\n"); xstrcat (strOut, "Content-Disposition: attachment; filename=", strFile, "\r\n\n", NULL); smtp_send_data (iSocket, strOut); while (!feof (fpin)) { memset (strRetBuff, 0, 513); fread (strRetBuff, sizeof (char), 512, fpin); smtp_send_data (iSocket, strRetBuff); } fclose (fpin); unlink (strUUEFile); } else break; } } /* This ends the message. */ smtp_send_data (iSocket, ".\r\n"); if (getreply (iSocket) > 400) return -7; /* Now log off the SMTP port. */ smtp_send_data (iSocket, "QUIT\n"); if (getreply (iSocket) > 400) return -8; /* Clean-up. */ /* Close the port up. */ close socket (iSocket); /* If a clean send, then reset and leave. */ ip_nonblock = iOld_ip_nonblock; return 0; }
| << | < | > | >> |
![]() |