Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

PHP + Lua help: Sending email

edited July 2014 in General Posts: 2,820

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\nLine2\nLine 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, "\n", "\r\n")
            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.."\nReply-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\nData 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\n Reply-To: -";

//$message = str_replace("/r/n", "\r\n", $message);
//$headers = str_replace("/r/n", "\r\n", $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.

Comments

  • Posts: 2,043

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

  • edited December 2013 Posts: 2,043

    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\nLine2\nLine 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.."\nReply-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\nData 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.

  • Posts: 2,820

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

  • Posts: 2,820

    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!

  • edited December 2013 Posts: 547

    nevermind, stupid response xd

  • edited December 2013 Posts: 2,820

    Sooo... Muuuuuccchhh..... Sssppppaaammmm....

  • edited December 2013 Posts: 2,043

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

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

    function setup()
        sendEmail("toenail","fromemail","Subject","Line 1\nLine 2\nLine 3\nI 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, "\n", "%0A")
    
        local function urlEncode(str)
            if (str) then
                str = string.gsub(str, "\n", "\r\n")
                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.."\nReply-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\nData received: "..data)
                    end,
                    function(error)
                        -- Fail
                        print("Fail with error: "..error)
                    end)
    
        print("Request sent")
    end
    
  • Posts: 2,820

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

  • edited December 2013 Posts: 2,043

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

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

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

  • Posts: 2,820

    @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.

  • edited December 2013 Posts: 2,043

    @Zoyt - removed them.

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

  • Posts: 2,820

    @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.

  • Posts: 1,976

    @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.)

Sign In or Register to comment.