send email from unity?

Hi everybody!

I want to be able to send email from unity. I have the php script that i use to send emails from web page,i was wondering is it possible to use it from unity.

if i have this code (very simple): how could i send email from unity?

<?php
// Pick up the form data and assign it to variables
$object = $_POST['object'];
$text= $_POST['text'];
$email = $_POST['email'];


$to='message@domain.com';
$subject="$object";
$message="text: $text";
$headers = "From: $email";

// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);
?>

thanks!

Unity of course don’t have a build in email server. Allowing your users to send emails is not such a good idea. The “usual” way to send an email is to send it via your email provider like described here. However, it’s possible to directly send an email to the target email-providers smtp server. But in this case your computer is the sending email server. A lot email providers block emails from unknown sources to prevent spam(*). If you use a php script like you’ve mentioned, it’s executed on your web server (like all php scripts) and this one will act as mail server and send the mail directly. If your web hoster is a trusted server it’s always the best way. But you should implement some kind of spam prevention so users can’t send tons of emails through your server, or your server will end up on a global black list.

(*) If an email server receives an email from an unknown host it usually answers with a “bad request” error to test if you are a real server (that tries to resend after some time). Most email server don’t allow faked email source adresses (if they can tell). For example if you directly send an email to gmx and you insert your own gmx address as source address, the gmx server will refuse the mail since it know for certain that your IP is not a gmx server.

I’m just passing by and see some answers were proposed, but in case you want to know how to actually use the script you have. You’d need to upload that PHP to a web server, follow Bunny83’s advices.

Then, within Unity, you need to send a Post request to the PHP, using WWWForm and WWW:

var url:String = "www.myserver.com/myscript.php";
var form:WWWForm = new WWWForm();

form.AddField("object","MY OBJECT");
form.AddField("text","MY TEXT");
form.AddField("email","MY EMAIL");

var www:WWW = new WWW(url,form);
yield www;

remember that when dealing with php, user inputs and such, security/sanity checks is a must to prevent malicious user’s activity.

Hope that helps.

If you want to call a PHP script on the server use WWW

ExternalCall?