Thursday, 18 June 2009

Mailto in AS3

This is an update from the previous Mailto button tutorial in Actionscript 2.0. In this tutorial I will show you the Actionscript 3.0 version of the mailto button. The mailto button will open up your email application to send emails to the selected email address. I have used a free stock image of a mail icon which is available from: www.sxu.hu

Mailto in AS3

Step 1

Open a Flash document.
Create an object for button on the stage, or alternatively you can use an image as a button like I have by selecting File > Import > Import to Stage.




Step 2

Convert your object/image into a symbol by selecting F8.
Give your symbol an appropriate name, check button and click ok.
Give your button an appropriate instance name e.g. mailto_btn.




Step 3

On the timeline insert a new layer called “Actions”. Right click on the 1st frame and select Actions and enter following code:

//1.Button event listener
mailto_btn.addEventListener(MouseEvent.CLICK, mailto);

//2.The mailto function open up email application to send email.
function mailto(event:MouseEvent):void {
var email:URLRequest = new URLRequest("mailto:example@yourdomain.com");
navigateToURL(email, "_blank");
}

Code:
1. This is an event listener for the button instance, ‘mailto_btn’, which contains a mouse click as the mouse event, and passes the mailto function.
2. This is a function which opens up your email application to send emails. You can change the email address to whatever you wish.


Step 4

Test your button Ctrl + Enter. Now click on the mail icon and your email application should open up with your desired email.



You should now be able to send emails in Actionscript 3.0.
Feel free to contact me for any questions or comments and remember to subscribe. You can download the source files here.

8 comments:

Studioet 11 October 2009 at 09:29  

The code is good, but for one thing. This open up a blank browser window when clicked on. This happens only in Safari, not Firefox or Opera. Any ideas?

iliketo 11 October 2009 at 14:11  

@Studioest

Do you have the lastest version of Safari?

Studioet 12 October 2009 at 00:56  

Hello

Yes, I always have latest version of Safari.

iliketo 12 October 2009 at 03:22  

@Studioet

What email reader applications do you have installed?

Studioet 12 October 2009 at 03:24  

Hello

I use the Mail application, native to OS X, and I run an intel mac with version 10.5.8. Have not yet upgraded to snow leopard.

iliketo 12 October 2009 at 09:01  

@Studioet

I think you might have a problem with the Flash player plugin for Safari. Try downloading the latest version of the Flash player, or even reinstalling your current version.

Joel Trevino 22 December 2009 at 09:19  

I am currently running IE 7.0.6001.18000 and am experiencing the same issues as Studioet (opening up a blank browser window with url "mailto:mail@mail.com"). And as a side issue, whenever I attempt to target "_self", nothing happens. It appears as though the button doesn't work. In FF 2.0 I get an error. Any ideas?

iliketo 22 December 2009 at 10:11  

@Joel Trevino

If your not running the SWF file inside a html file it will not work, since the SWF does not contain a web browser, so you will not be able to open an SWF. However, you can use "_blank" as this opens up a new browser window.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP