[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
#2

Nice, I'll read the code later xD :l
Reply
#3

Nice Tutorial , I like it.

Good Job Bro
Reply
#4

Thank you for the tutorial, although I think you should emphasize a bit more on making the code secure.
What if the player wants to include the &-character in the e-mail? It will get cut off. Or if the player writes a - in the e-mail, it will be converted to a space by the PHP script. I don't understand if this tutorial is necessary at all, at least in such form. I have seen a script that has both issues covered with URL encoding. You could as well send the data as a POST request instead of GET.

Also, code like this:
pawn Код:
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;
}
... can be simplified to this:
pawn Код:
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
    }
    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;
}
Also, for those who are using sscanf, you have given a great idea that came with the functions of SA-MP 0.3b. But why not a working code representation using sscanf?

Good luck!
Reply
#5

Its not for user input, its more like to send registrations email to player, or validation code from the game, that /email cmd was just as example, about url, it can be formated different etc.

And you saved just 1 script line which with simplying, while I tried to keep it as close as possible to original one from reference, its tutorial about using HTTP not about script optimizing, and about sscanf its not default SA:MP function, while strtok is included by default.
Reply
#6

Hello i have problem because when i want to use this script i have error 500
My code
Код:
<?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("slonecznyots@gmail.com", "Email from server", $message,"From: \"SA:MP server\" <slonecznyots@gmail.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 
        } 
    } 
} 
?>
Help it is very important for me.

My site is imaginerp.xaa.pl
Reply
#7

Nice idea , well explained I liked it , good job
Reply
#8

I Would like to make it work for my registration system, could I get some help?
Reply
#9

This is neat!
Reply
#10

All you're doing is getting params from the URL, parsing it into variables in the PHP script and using it to send an email. Not really sure how this could be used effectively for communicating with a database.

Also, if somebody should find the URL to that PHP file, they can spam your inbox like crazy, causing you a lot of issues.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)