Sample SMTP Plug-in
Table of Contents | Previous | Next | Index

Messaging Server Plug-in API Guide


Chapter 4
Sample SMTP Plug-in

This sample SMTP plug-in DLL shows how to add a recipient to a message that goes through the server. This chapter takes you step by step through constructing a SMTP plug-in using the Messaging Server Plug-in API.

The SamplePlugin performs several operations:

If you would like to see this plug-in in action, save and build the code in "SMTP Plug-in Code."

This list summarizes the plug-in operations. Each operation links to the line in the DLL that performs it.

[Top]

The SamplePlugin DLL

Include header files

Be sure to include the msg4plugins.h header file and the standard input/output and allocation header files.

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "msg4plugins.h"
If you do not have the Messaging Server plug-in header file yet, see "Where to Find the SMTP Plug-in Header File."

Implement the initialization function

To initialize the SMTP plug-in, call the initialization function. It is called once at MTA start-up to initialize local server data, load configuration information off the disk, declare global variables, and return an integer that indicates whether the initialization function completed. This simple version of the initialization function only performs the last of these tasks.

SAMPLE_EXPORT int SamplePluginInit(pblock *Config)
{
return 1;
}
This is one of the two function prototypes that the SMTP plug-in must export in order to load. In this sample, the Config parameter is not referenced further.

Define the plug-in function

The plug-in function, SamplePlugin, loads the parameter block pointed to by the Config parameter and creates pointers to the input (InMessage) and output (OutMessage) messages. The output parameter represents the result of the execution of the plug-in code.

SAMPLE_EXPORT int SamplePlugin(pblock *Config,
                   Message **ppInMsg,
                   Message ***pppOutMsg)
This is one of the two function prototypes that the SMTP plug-in must export in order to load. In this sample, the Config and pppOutMsg parameters are not referenced further.

The SMTP plug-in code returns either MSG_CONTINUE or MSG_NOACTION to Messaging Server. MSG_CONTINUE indicates that the plug-in worked; message can go on to Messaging Server for further processing. MSG_NOACTION indicates that the plug-in did not do anything.

For information about defining this function, see "Writing the Plug-in Function."

Define the recipients of the message and its body and temporary files

{
   char *bodyfile;
   Recipient *pRecip;
   Recipient new_rcpt;
   FILE *f;
   FILE *body;
   char *ControlText;
   char *PostOfficePath;

Create a log file

Open a log file, which will be closed at the end of the sample. If any error occurs in opening the file, the plug-in returns MSG_CONTINUE, which causes message processing to continue.

   f = fopen ("log.out","w");
   if (f == NULL) {
      return MSG_CONTINUE;
      }

Find and print out the first recipient

To get a pointer to the first recipient in the message, use GetFirstRecipient. The fprintf routine calls GetRecipientAddress to get the address for printing.

pRecip = GetFirstRecipient (ppInMsg[0]);
if (pRecip) {
   fprintf (f,"Recip #1 is %s\n",GetRecipientAddress(pRecip));
}

Write the control data for the message

The SMTP plug-in next writes the control data for the message to the log file that was created earlier. Control data includes SMTP envelope and connection information gathered during message reception.

ControlText = GetControlData(ppInMsg[0]);
if (ControlText) {
   fprintf(f, "Control Data follows\n%s", ControlText);
}

Free the control data

After getting message control data, always free it with this command.

FreeControlData(ControlText);

Add a new recipient to the message

Define the new recipient with any required flags or an extension.

new_rcpt.Addr821 = (char*)malloc (strlen ("<testuser@test.com>") + 1);
strcpy(new_rcpt.Addr821, "<testuser@test.com>");
new_rcpt.Ext = NULL;
new_rcpt.flags = 0;
This is the tricky part - you must set this magic value.

new_rcpt.magic = 0xdeadbeef;
Make the AddRecipient call.

AddRecipient(ppInMsg[0], &new_rcpt);
free (new_rcpt.Addr821);
For more information, see "Add and remove recipients."

Add text to the body of the message

Open the body and add some text. This sample either adds, and prints, the lines of text. If it can't add the text for some reason, it prints an error message.

First, the SMTP plug-in gets the message:

bodyfile = GetMessageFile (*ppInMsg);
fprintf (f,"GetMessageFile = %s\n", bodyfile);
Then it opens the message body and adds text to the end:

body = fopen (bodyfile,"a");
if (body == NULL)
{
   fprintf (f,"Couldn't append to body\n");
}
else{
   fprintf (body,"This is a new body line\n");
   fprintf (body,"This is a new body line\n");
   fprintf (body,"This is a new body line\n");
   fprintf (body,"This is a new body line\n");
   fprintf (body,"This is a new body line\n");
   fprintf (body,"This is a new body line\n");
   fprintf (body,"This is a new body line\n");
   fprintf (body,"This is a new body line\n");
   fprintf (body,"This is a new body line\n");
   fprintf (body,"This is a new body line\n");
   fclose (body);
}

Return the post office directory

Get the path to the post office directory where the message lives. In this example, the path is simply printed to the log file opened in this sample.

PostOfficePath = GetPostOfficePath(*ppInMsg);
   fprintf(f, "PostOffice = %s\n", PostOfficePath);

Close the log file

Don't forget to close the log file.

fclose(f);

Send the message

If the SMTP plug-in returns MSG_CONTINUE, the message continues on its way. For other return messages, see "Result Codes."

return MSG_CONTINUE;
NOTE: You can find the code for this SMTP plug-in in "SMTP Plug-in Code." For information about building the plug-in, see "Building the SMTP Plug-in." §
[Top]


Table of Contents | Previous | Next | Index

Last Updated: 05/13/99 11:41:54

Copyright © 1999 Netscape Communications Corporation.