Re: Send email via trigger
I am not a C lover - i hate it, but there is my source for C++ builder 2007 - for sending MAPI message.
for mysql 5.1 place dll in the lib folder. Then exec
create function _MySqlSendMail returns integer soname 'dllname.dll'
after that you can try to use it..But there is some problems..This is not working and function MAPISendMail itself returns 3 - logon failure.. But this is not the main problem also..(maybe it was outlook or antivirus...), second problem is that MySQL server crashes after first call to this function (it doesnt see any databases after one call, but continue to work..)
I dont know how to call normally in C this function..hate this language..so i wrote this in assembler,,
#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>
#include <m_ctype.h>
#include <m_string.h>
#include <windows.h>
#include <winbase.h>
#include <mapi.h>
#include <stdlib.h>
#define MAPIDLL "mapi32.dll"
#define SEND_MAIL_FUNC "MAPISendMail"
#define MSG_ARG_SUBJECT 0
#define MSG_BODY 1
#define MSG_FROM_NAME 2
#define MSG_FROM_EMAIL 3
#define MSG_TO_NAME 4
#define MSG_TO_EMAIL 5
#define MSG_ERROR_MAXLEN 31
//---------------------------------------------------------------------------
char *ErrorText;
__declspec( dllexport ) my_bool MySqlSendMail_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
int i;//loop
if (args->arg_count != 6) {
strcpy(message, "MySqlSendMail() must have exactly six arguments");
return 1;
}
for(i = 0; i < 6; i++)
if (args->arg_type != STRING_RESULT) {
strcpy(message, "MySqlSendMail() must have six string arguments");
return 1;
}
initid->maybe_null = 0;
initid->decimals = 0;
initid->max_length = MSG_ERROR_MAXLEN;
initid->const_item = 0;
ErrorText = (char *) calloc(MSG_ERROR_MAXLEN, sizeof(char));
return 0;
}
//---------------------------------------------------------------------------
__declspec( dllexport ) void MySqlSendMail_deinit(UDF_INIT *initid)
{
free(ErrorText);
}
//---------------------------------------------------------------------------
__declspec( dllexport ) char * MySqlSendMail(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
MapiMessage msg;
MapiRecipDesc Sender, Recipient;
void *SendMailProc;
int result;
HMODULE MAPIModule;
char *Subject, *Body, *SenderName, *SenderEmail;
char *RecipientName, *RecipientEmail, *Address;
//params
Subject = args->args[MSG_ARG_SUBJECT];
Body = args->args[MSG_BODY];
SenderName = args->args[MSG_FROM_NAME];
SenderEmail = args->args[MSG_FROM_EMAIL];
RecipientName = args->args[MSG_TO_NAME];
RecipientEmail = args->args[MSG_TO_EMAIL];
ZeroMemory(&msg, sizeof(MapiMessage));
msg.lpFiles = NULL;//no attachments
msg.lpszSubject = Subject;
msg.lpszNoteText = Body;
//filling sender data
if (strlen(SenderEmail) > 0) {
Sender.ulRecipClass = MAPI_ORIG;
if (strlen(SenderName) == 0)
Sender.lpszName = SenderEmail;
else
Sender.lpszName = SenderName;
//*assigning e-mal address
Address = (char *) calloc(strlen(SenderEmail) + 6, sizeof(char));
strcpy(Address, "SMTP:");
Sender.lpszAddress = strcat(Address, SenderEmail);
free(Address);
//email address now is assigned
Sender.ulReserved = 0;
Sender.ulEIDSize = 0;
Sender.lpEntryID = NULL;
msg.lpOriginator = &Sender;
}
//filling recipient data
if (strlen(RecipientEmail) > 0) {
Recipient.ulRecipClass = MAPI_ORIG;
if (strlen(RecipientName) == 0)
Recipient.lpszName = RecipientEmail;
else
Recipient.lpszName = RecipientName;
//*assigning e-mal address
Address = (char *) calloc(strlen(RecipientEmail) + 6, sizeof(char));
strcpy(Address, "SMTP:");
Recipient.lpszAddress = strcat(Address, RecipientEmail);
free(Address);
//email address now is assigned
Recipient.ulReserved = 0;
Recipient.ulEIDSize = 0;
Recipient.lpEntryID = NULL;
msg.nRecipCount = 1;
msg.lpRecips = &Recipient;
} else {
msg.lpRecips = NULL;
}
MAPIModule = LoadLibraryA(MAPIDLL);
if (!MAPIModule)
return -1;
SendMailProc = GetProcAddress(MAPIModule, SEND_MAIL_FUNC);
if (SendMailProc)
asm {
//pascal calling
push 0;//ulReserved
push 0;//flFlags
push offset msg;//lpMessage
push 0//ulUIParam
push 0//lhSession
call SendMailProc
lea ebx, result
mov [ebx], eax
mov [ebx + 4], edx
}
else
result = -2;
FreeLibrary(MAPIModule);
switch (result) {
case -1:
strcpy(ErrorText, "MAPI32.DLL not found.");
break;
case -2:
strcpy(ErrorText, "MAPISendMail export not found.");
case MAPI_E_USER_ABORT:
strcpy(ErrorText, "MAPI_E_USER_ABORT");
break;
case MAPI_E_FAILURE:
strcpy(ErrorText, "MAPI_E_FAILURE");
break;
case MAPI_E_LOGON_FAILURE:
strcpy(ErrorText, "MAPI_E_LOGON_FAILURE");
break;
case MAPI_E_DISK_FULL:
strcpy(ErrorText, "MAPI_E_DISK_FULL");
break;
case MAPI_E_INSUFFICIENT_MEMORY:
strcpy(ErrorText, "MAPI_E_INSUFFICIENT_MEMORY");
break;
case MAPI_E_ACCESS_DENIED:
strcpy(ErrorText, "MAPI_E_ACCESS_DENIED");
break;
case MAPI_E_TOO_MANY_SESSIONS:
strcpy(ErrorText, "MAPI_E_TOO_MANY_SESSIONS");
break;
case MAPI_E_TOO_MANY_FILES:
strcpy(ErrorText, "MAPI_E_TOO_MANY_FILES");
break;
case MAPI_E_TOO_MANY_RECIPIENTS:
strcpy(ErrorText, "MAPI_E_TOO_MANY_RECIPIENTS");
break;
case MAPI_E_ATTACHMENT_NOT_FOUND:
strcpy(ErrorText, "MAPI_E_ATTACHMENT_NOT_FOUND");
break;
case MAPI_E_ATTACHMENT_OPEN_FAILURE:
strcpy(ErrorText, "MAPI_E_ATTACHMENT_OPEN_FAILURE");
break;
case MAPI_E_ATTACHMENT_WRITE_FAILURE:
strcpy(ErrorText, "MAPI_E_ATTACHMENT_WRITE_FAILURE");
break;
case MAPI_E_UNKNOWN_RECIPIENT:
strcpy(ErrorText, "MAPI_E_UNKNOWN_RECIPIENT");
break;
case MAPI_E_BAD_RECIPTYPE:
strcpy(ErrorText, "MAPI_E_BAD_RECIPTYPE");
break;
case MAPI_E_NO_MESSAGES:
strcpy(ErrorText, "MAPI_E_NO_MESSAGES");
break;
case MAPI_E_INVALID_MESSAGE:
strcpy(ErrorText, "MAPI_E_INVALID_MESSAGE");
break;
case MAPI_E_TEXT_TOO_LARGE:
strcpy(ErrorText, "MAPI_E_TEXT_TOO_LARGE");
break;
case MAPI_E_INVALID_SESSION:
strcpy(ErrorText, "MAPI_E_INVALID_SESSION");
break;
case MAPI_E_TYPE_NOT_SUPPORTED:
strcpy(ErrorText, "MAPI_E_TYPE_NOT_SUPPORTED");
break;
case MAPI_E_AMBIGUOUS_RECIPIENT:
strcpy(ErrorText, "MAPI_E_AMBIGUOUS_RECIPIENT");
break;
case MAPI_E_MESSAGE_IN_USE:
strcpy(ErrorText, "MAPI_E_MESSAGE_IN_USE");
break;
case MAPI_E_NETWORK_FAILURE:
strcpy(ErrorText, "MAPI_E_NETWORK_FAILURE");
break;
case MAPI_E_INVALID_EDITFIELDS:
strcpy(ErrorText, "MAPI_E_INVALID_EDITFIELDS");
break;
case MAPI_E_INVALID_RECIPS:
strcpy(ErrorText, "MAPI_E_INVALID_RECIPS");
break;
case MAPI_E_NOT_SUPPORTED:
strcpy(ErrorText, "MAPI_E_NOT_SUPPORTED");
break;
default:
strcpy(ErrorText, "MAPI_E_UNKNOWN");
}
return ErrorText;
}
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
Edited 5 time(s). Last edit at 12/08/2007 05:17AM by Zigmund Bulinsh.