PHP + Lua help: Sending email

Hello,
I’m probably making a stupid mistake, but I’m having issues sending an email from Lua with PHP. I poromised this to someone who wanted PHP tutorials earlier, but I ran into issues. I needed it for my own app, so I tried to fix the issue, but I can’t. Can someone help please?

function setup()
    sendEmail("<deleted>","<deleted>","Subject","Line 1\
Line2\
Line 3")
end

function draw()
    background(40, 40, 50)
end

function sendEmail(to,from,subject,message)
    -- Functino for encoding string as URL (from lua-users.org/wiki/StringRecipes)
    local function urlEncode(str)
        if (str) then
            str = string.gsub (str, "\
", "\\r\
")
            str = string.gsub (str, "([^%w ])",
            function (c) return string.format ("%%%02X", string.byte(c)) end)
            str = string.gsub (str, " ", "+")
        end
        return str    
    end

    
    -- Set up initial URL
    local url = "<deleted>"
    
    -- Set up headers (to spoof the email coming from an email and setting up the reply-to feild
    local headers = "From: "..from.."\
Reply-To: "..from
    
    -- Tell PHP what to send in the email
    urlVars = "?to="..to.."&subject="..subject.."&message="..message.."&headers="..headers

    -- Encode URL variables and join with main URL
    url = url..urlEncode(urlVars)
    
    print(url)
    
    -- Sends request
    http.request(url,
                function(data)
                    -- Success
                    print("Success\
Data received: "..data)
                end,
                function(error)
                    -- Fail
                    print("Fail with error: "..error)
                end)
    
    print("Request sent")
end

and the PHP:

<?php
$to = $_GET["to"];
$subject = $_GET["subject"];
$message = $_GET["message"];
$headers = $_GET["headers"];
//$headers = "From: - \\r\
 Reply-To: -";

//$message = str_replace("/r/n", "\\r\
", $message);
//$headers = str_replace("/r/n", "\\r\
", $headers);

if(mail($to, $subject,$message,$headers)){
	echo 'Email sent';
}
else{
	echo 'Error in sending email';
}
?>

My issue is that I’m getting only the first line of the email.
I can do this perfectly in JavaScript with RegEx.

Thanks!
( @Briarfox - I know you know stuff about PHP )

Also, please do not spam my email or servers. Thank you. I will be moving this file soon, so don’t plan on being able to use it.

Why not use post, or $_REQUEST to allow both post and get?

If you change the server code to use $_POST instead of $_GET, I believe this will work

function setup()
    sendEmail("johnappleseed@icloud.com","johnappleseed@icloud.com","Subject","Line 1\
Line2\
Line 3")
end

function draw()
    background(40, 40, 50)
end

function sendEmail(to,from,subject,message)
    -- Set up initial URL
    local url = "server address"

    -- Set up email headers (to spoof the email coming from an email and setting up the reply-to feild
    local emailHeaders = "From: "..from.."\
Reply-To: "..from

    local headers = {}
    headers["Accept"]="text/plain"
    headers["Content-Type"]="application/x-www-form-urlencoded"
    headers["Accept-Charset"]="utf-8"
    
    local data = "to="..to.."&subject="..subject.."&message="..message.."&headers="..emailHeaders
    
    -- Sends request
    http.request(url,
                function(data)
                    -- Success
                    print("Success\
Data received: "..data)
                end,
                
                function(error)
                    -- Fail
                    print("Fail with error: "..error)
                end,
                
                { method = "POST", headers = headers, data = data })

    print("Request sent")
end

P.S. I know some PHP Server <-> Codea, but I’m hardly an expert and I don’t have my laptop to try this right now. If it doesn’t work, let me know.

@JakAttak - Sorry if I wasn’t clear, but my issue is that it’s only sending the first line.
Thanks!

Some more details: The PHP script isn’t even getting the second line of the message, so it’s an issue with the URL encoding. I added stuff to the PHP to send back to you some more info.
Thanks!

nevermind, stupid response xd

Sooo… Muuuuuccchhh… Sssppppaaammmm…

@Zoyt, does my method send the second and third lines?

EDIT: Just in case this encodes it correctly (
needs to be %0A):

function setup()
    sendEmail("toenail","fromemail","Subject","Line 1\
Line 2\
Line 3\
I Fixed It!")
end

function draw()
    background(40, 40, 50)
end

function string.replace(str, pattern, replacement)
    local start = 0
    local newStr = ""
    
    while (start ~= nil) do
        local a, b = str:find(pattern, start, true)
        local before = str:sub(start, a - 1)
        local after = str:sub(b + 1, str:len())
        
        newStr = newStr .. before .. replacement
        
        local j,k = str:find(pattern, b + 1, true)
        if j == nil then
            start = nil
            newStr = newStr .. after
        else
            start = b + 1
        end
    end
    
    return newStr
end

function sendEmail(to,from,subject,message)
    -- Functino for encoding string as URL (from lua-users.org/wiki/StringRecipes)
    message = string.replace(message, "\
", "%0A")
    
    local function urlEncode(str)
        if (str) then
            str = string.gsub(str, "\
", "\\r\
")
            str = string.gsub(str, "([^%w ])",
            function (c) return string.format ("%%%02X", string.byte(c)) end)
            str = string.gsub(str, " ", "+")
        end
        return str    
    end


    -- Set up initial URL
    local url = "serveraddress"

    -- Set up headers (to spoof the email coming from an email and setting up the reply-to feild
    local headers = "From: "..from.."\
Reply-To: "..from

    -- Tell PHP what to send in the email
    urlVars = "?to="..to.."&subject="..subject.."&message="..message.."&headers="..headers

    -- Encode URL variables and join with main URL
    url = url..urlEncode(urlVars)

    print(url)

    -- Sends request
    http.request(url,
                function(data)
                    -- Success
                    print("Success\
Data received: "..data)
                end,
                function(error)
                    -- Fail
                    print("Fail with error: "..error)
                end)

    print("Request sent")
end

@Zoyt - removed them.

EDIT: I just realized it is incredibly easy to send email from anyone’s email… Worrisome

@JakAttak - =D> =D> =D> What exactly did you do? I can’t find the difference.

string.gsub doesn’t seem to support special characters, so I wrote a string.replace function and used it to replace the \ with %0A This is supported by the url encoding

Look at the second line of sendMail
message = string.replace(message, "\ ", "%0A")

P.S. Just tested my earlier version using Post and it also works. IMO it’s cleaner, but use whichever you wish

@JakAttak - Thanks. Could you remove the link and my email in the code please? Spam and server overloads are 2 things I don’t want. And with all the bots here, it’s a bit risky.

@JakAttak - Welcome to the world of how phishing is done. But it’s useful as [something really useful] for support emails, because you just need to click “Reply” and you’re done.
Thanks!
P.S. What is “toenail”? JK.

@JakAttak @Zoyt On a side notes string.gsub DOES work with special characters, but you were accidentally using patterns with them. It should be %%A0 rather than %A0. (% is a capture thing for letters, use %% when you just want one % without special behavior.)