[Include] My Mailer Include
#1

Hey,
I noticed there are a couple of functions for sending e-mails on the forums and none of them really work properly (no offence).
This, I believe, should be able to handle most charsets (I'm not sure about multi-byte, please let me know how it goes).
It can handle quotes, slashes, special characters, etc.
It also runs relatively fast:
Code:
Bench for SendMail: executes, by average, 32 times/ms.
That doesn't mean you should send mails 32 times in a millisecond!

Usage
PHP Code:
SendMailto[], sender_email[], sender_name[], subject[], message[] ); 
Example
Create a filterscript with the contents below and load it.
pawn Code:
#define MAILER_URL "my-server.com/mailer.php" // This has to be defined BEFORE you include mailer.

#include <mailer>

public OnFilterScriptInit( )
{
    SendMail( "your.email@here.ok", "roleplay.server@example.com", "Roleplay \"Server\"", "My \"mепlлr\" & it's sьbjйct!", "Hиllц hцw еrл yoь? I'm \"writing\" weird simply to test if my mailer script can handle it without any problems. Gцt е prуblлm wпth thдt?" );
}
Download
Put mailer.inc in your pawno\include folder and upload mailer.php to a web server that has PHP (you may need to configure it's SMTP server).

mailer.php:
PHP Code:
<?php
    ini_set
'html_errors'false );
    if ( empty( 
$_POST't' ] ) || empty( $_POST'f' ] ) || empty( $_POST'n' ] ) || empty( $_POST's' ] ) || empty( $_POST'm' ] ) )
        die( 
'Error: Missing parameters.' );
    
mail(
        
$_POST't' ],
        
utf8_encode$_POST's' ] ),
        
$_POST'm' ],
        
implode(
            
"\r\n",
            array
            (
                
'From: "' addslashes$_POST'f' ] ) . "\" <{$_POST'n' ]}>",
                
"Reply-To: {$_POST['f']}",
                
"X-Mailer: PHP/" phpversion( ),
            )
        )
    );
?>
mailer.inc:
Code:
#include <a_samp>
#include <a_http>

#if ( !defined MAILER_MAX_MAIL_SIZE )
	#define MAILER_MAX_MAIL_SIZE  (1024)
#endif

#if ( !defined MAILER_URL )
	#error Please define MAILER_URL before including the mailer include.
#endif

stock SendMail( const szReceiver[ ], const szSenderMail[ ], const szSenderName[ ], const szSubject[ ], const szMessage[ ] )
{
	new
		szBuffer[ MAILER_MAX_MAIL_SIZE ] = "t=",
		iPos    = strlen( szBuffer ),
		iLength = strlen( szReceiver )
	;
	
	memcpy( szBuffer, szReceiver, iPos * 4, ( iLength + 1 ) * 4 );
	
	StringURLEncode( szBuffer[ iPos ], 1024 - iPos );
	
	strcat( szBuffer, "&f=" );
	
	iPos    = strlen( szBuffer );
	iLength = strlen( szSenderName );
	
	memcpy( szBuffer, szSenderName, iPos * 4, ( iLength + 1 ) * 4 );
	
	StringURLEncode( szBuffer[ iPos ], 1024 - iPos );
	
	strcat( szBuffer, "&n=" );
	
	iPos    = strlen( szBuffer );
	iLength = strlen( szSenderMail );
	
	memcpy( szBuffer, szSenderMail, iPos * 4, ( iLength + 1 ) * 4 );
	
	StringURLEncode( szBuffer[ iPos ], 1024 - iPos );
	
	strcat( szBuffer, "&s=" );
	
	iPos    = strlen( szBuffer );
	iLength = strlen( szSubject );
	
	memcpy( szBuffer, szSubject, iPos * 4, ( iLength + 1 ) * 4 );
	
	StringURLEncode( szBuffer[ iPos ], 1024 - iPos );
	
	strcat( szBuffer, "&m=" );
	
	iPos    = strlen( szBuffer );
	iLength = strlen( szMessage );
	
	memcpy( szBuffer, szMessage, iPos * 4, ( iLength + 1 ) * 4 );
	
	StringURLEncode( szBuffer[ iPos ], 1024 - iPos );
	
	HTTP( 0xD00D, HTTP_POST, MAILER_URL, szBuffer, "OnMailScriptResponse" );
}

forward OnMailScriptResponse( iIndex, iResponseCode, const szData[ ] );
public  OnMailScriptResponse( iIndex, iResponseCode, const szData[ ] )
{
	if ( szData[ 0 ] )
		printf( "Mailer script says: %s", szData );
}

stock StringURLEncode( szString[ ], iSize = sizeof( szString ) )
{
	for ( new i = 0, l = strlen( szString ); i < l; i++ )
	{
		switch ( szString[ i ] )
		{
			case '!', '(', ')', '\'', '*',
			     '0' .. '9',
			     'A' .. 'Z',
			     'a' .. 'z':
			{
				continue;
			}
			
			case ' ':
			{
				szString[ i ] = '+';
				
				continue;
			}
		}
		
		new
			s_szHex[ 8 ]
		;
		
		if ( i + 3 >= iSize )
		{
			szString[ i ] = EOS;
			
			break;
		}
		
		if ( l + 3 >= iSize )
			szString[ iSize - 3 ] = EOS;
		
		format( s_szHex, sizeof( s_szHex ), "%02h", szString[ i ] );
		
		szString[ i ] = '%';
		
		strins( szString, s_szHex, i + 1, iSize );
		
		l += 2;
		i += 2;
		
		if ( l > iSize - 1 )
			l = iSize - 1;
	}
}
Reply
#2

A lot better, nice one.
Reply
#3

Amazing, like always! Thank you so much. ;>
Reply
#4

Yeah,thanks much,now I can flood a mail from sa-mp
just joking,nice script
Reply
#5

Nice Work.
Reply
#6

Very good !!!
Reply
#7

nice job
Reply
#8

Nice work man, i gonna use it.
Reply
#9

Great Job!
Reply
#10

Sweet, but can someone test it for me?
Reply
#11

Quote:
Originally Posted by [FU]Victious
View Post
Sweet, but can someone test it for me?
Well I tested it and it works. What exactly do you mean by test it for you?
Reply
#12

Quote:
Originally Posted by Slice
View Post
Well I tested it and it works. What exactly do you mean by test it for you?
I havn't got any money in my paypal atm and if you could use your own website to send me a email =D
Reply
#13

Man, that's nice.
Reply
#14

Nice Include ... but: Why i get "Error: Missing parameters." in the .php file ?
Reply
#15

This is a Very Useful include! thanks
Reply
#16

C:\Users\Tom\Desktop\samp03bsvr_R2_win32\pawno\inc lude\mailer.inc(9) : fatal error 111: user error: Please define MAILER_URL before including the mailer include.
Reply
#17

Quote:
Originally Posted by r3k1lLz_
View Post
C:\Users\Tom\Desktop\samp03bsvr_R2_win32\pawno\inc lude\mailer.inc(9) : fatal error 111: user error: Please define MAILER_URL before including the mailer include.
...no words, just

Bump because this is underrated, great work!
Reply
#18

It's nice 'Slice' ! (ej, that rhymes xD).
I'm gonna use it for my server. Do you mind if I edit it? (Yes I'll keep credits in the CREDITS.txt)
Quote:
Originally Posted by SAMP/MafiaRP/gamemodes/CREDITS.txt
Slice:
* Hold studio - For the weapon holdster script
* Mailer include (converted into GM) [https://sampforum.blast.hk/showthread.php?tid=197755]
* Some tutorials
Thanks,
Kevin
Reply
#19

Cool. Let me know if you run into any problems.
Reply
#20

It's working
Thank you.
Reply


Forum Jump:


Users browsing this thread: 10 Guest(s)