09 Sept 2014Unity15 Comments

Sending emails from game

This is a short tutorial. Sometimes you have to send emails if you are developing a of social game. Basically we can send mails in two ways,

  • Sending directly through the game.
  • Using simple web-service. (php)

What is a web service ?

1). Sending Directly through the game

Let's write a class to send emails using a Simple Mail Transfer Protocol (SMTP). First create new Unity project and add new C# script. lets call it MailManager.cs. copy and paste the following code to the newly created .cs file

Replace email address and password with actual values. Lets call above method from MailManager.Start(); . Then add your .cs file to the camera component. Now its time to run your project :)

2). Sending via Web Service (php)

Php has a inbuilt method called mail(). it allows you to send emails directly form a script. Create a new php script using your favorite editor and name it as you wished. in my case i called it index.php. copy and paste the following code.

Host this any php and webmail enabled servers. Back end part is done.Test your simple web service by typing its address on web browser and don’t forget to pass get parameters.

http://praveeen88.com/sendMail.php?to=praveee88@gmail.com&subject=TestEmail&message=YourMessage&headers=From:praveee@ymail.com

if you are not able to send mails. you need to configure your server. Lets create functions to call this php script. using unity’ s WWW we can easily access web contests. This class can use to send both GET and POST requests to the server. The WWW class will use GET by default and POST if you supply a postData parameter.

Learn more about www

Add following method to previously created .cs file

Its time to save. make use of .net’s System.IO methods easily save created image into device.

File.WriteAllBytes

Download complete unity project

Tags: