[Tutorial] Send an email from the game
#1

Hello

I would like to show how you can send a email or any other data from SA:MP server to php file which later can be used as you wish.
You can even make connection to remote mysql Database to save/load data, response speed will be faster then direct connect to remote DB from the SA:MP server.

I will show how to send a email from the game. Useful to receive reports if there is no admins online etc.


First of you need web host with mail() function enabled, mostly all web hosts have its enabled by default


First we need to build *.php file which will handle request from SA:MP server

Use Notepad++ and make new file called test.php
PHP код:
<?php
if (isset($_GET['code']))//checks if request from SA:MP server contains code
{
    
$code $_GET['code'];//Redefine just to look better :)
    
if($code == 44514)// Compare code from SA:MP request, a.k.a password to avoid spam or unauthorized requests
    
{
        if (isset(
$_GET['msg']) && !empty($_GET['msg']))//Checks if request from SA:MP server contains messag you want to send
        
{
            
$message $_GET['msg'];//Redefine just to look better :)
            
$message str_replace("-"," ",$message);//Replace all - back to blank space as url cant contains blank spaces
            
mail("info@c-roleplay.com""Email from server"$message,"From: \"SA:MP server\" <info@ikey07.com>\r\n" ."X-Mailer: PHP/" phpversion());//Send an email, format: To,Subject,Message,From
            
echo "Email delivered";//Response message to SA:MP server, so server knows request was valid
        
}
    }
}
?>
Thats all about PHP things, rest is usual pawn script, as reference I used https://sampwiki.blast.hk/wiki/HTTP

pawn Код:
#include <a_samp>
#include <a_http>//This is needed if you actualy want make this all

#define COLOR_SAMP 0xA9C4E4FF //Just define some color, Im using SAMP default color

forward EmailDelivered(index, response_code, data[]); //forward response callback, so pawno doesnt say there is some undefinded symbol

public EmailDelivered(index, response_code, data[])//data[] Will contain "Email delivered" see in PHP file
{
    new buffer[128];
    if(response_code == 200)
    {
        format(buffer, sizeof(buffer), "Status: %s", data);//In game will print Status: Email delivered
        SendClientMessage(index, COLOR_SAMP, buffer);
    }
    else
    {
        format(buffer, sizeof(buffer), "Status: Undelivered Response Code: %d", response_code);//Else will print error code, see reference
        SendClientMessage(index, COLOR_SAMP, buffer);
    }
    return 1;
}


public OnPlayerCommandText(playerid, cmdtext[])
{
    new cmd[256],idx;
    cmd = strtok(cmdtext, idx);//You should be able to find this function in common.inc file in SAMP server folder
    new string[256];
    if(strcmp(cmd, "/email", true) == 0)
    {
        new text[128];
        GetStringText(cmdtext,idx,text);//Custom function to get whole text after first blank space
        if(!strlen(text)) { SendClientMessage(playerid,COLOR_SAMP," /email [Text]"); return 1; }//We dont want send empty email so we add usage
        for(new i; i < strlen(text); i ++)//Scan the text line to add some symbol because URL cant contain blank spaces
        {
            if(strfind(text[i]," ", true) == 0)//If scan process found blank space its gets replaced with "-" in my case, you can use any other symbol
            {
                text[i] = '-';//Result is Hello-World-!
            }
        }
        format(string,sizeof(string), "c-roleplay.com/test.php?code=44514&msg=%s",text);//Format request URL to PHP file we made before
//As you can see url contains code which is used later in PHP file we made
        HTTP(playerid, HTTP_GET,string, " ", "EmailDelivered");//Sends a request to test.php file
        return 1;
    }
    return 0;
}

stock GetStringText(const string[],idx,text[128])//Abit modified strtok function to return string after 1st blank space
{
    new length = strlen(string);
    while ((idx < length) && (string[idx] <= ' ')) { idx++; }
    new offset = idx; new result[128];
    while ((idx < length) && ((idx - offset) < (sizeof(result) - 1))) { result[idx - offset] = string[idx]; idx++; }
    result[idx - offset] = EOS;
    text = result;
    return result;
}

This can be used to send a validation link to players email on registrations, to avoid fake registrations etc

I hope someone find it useful, because for me its very, as I even get SMS to my real mobile phone when I receive email, so in basic for me its SMSing to real phone from SAMP server, if need any other explenation about some things, let me know here.
Reply


Messages In This Thread
Send an email from the game - by ikey07 - 14.06.2012, 20:47
Re: Send an email from the game - by FireCat - 14.06.2012, 20:56
Re: Send an email from the game - by Kirollos - 29.06.2012, 20:34
Re: Send an email from the game - by AndreT - 29.06.2012, 21:01
Re: Send an email from the game - by ikey07 - 01.07.2012, 11:03
Re: Send an email from the game - by Unista - 31.12.2012, 21:12
Re: Send an email from the game - by RedCrossER - 31.12.2012, 21:29
Re: Send an email from the game - by Matt - 12.02.2013, 23:59
Re: Send an email from the game - by freddy smyth - 23.02.2013, 04:09
Re: Send an email from the game - by DLR - 23.02.2013, 11:04

Forum Jump:


Users browsing this thread: 1 Guest(s)