PHP
If you dont have access to a PHP enabled server you can easily install one locally.
WampServer and XAMPP are the most common ones.
Head over to http://swiftmailer.org/ and download the latest library (PHP 5)
.
The current version is 4.0.6 and hasn't been updated in a long time, but according to a post on Google Groups the project is far from dead and updates are expected soon. Furthermore, this is a very stable release.
Extract the archived files and place the lib folder which is inside Swift-4.0.6 in the desired folder.
Create a PHP file, e.g.: script.php and add the line
require_once 'lib/swift_required.php';
This will make the library available to you project.
Here's the basic script using Swift Mailer to send an email (Mail Transport):
require_once 'lib/swift_required.php';
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setFrom(array('myname@mydomain.com' => 'My Name'))
->setSubject('sending emails')
->setTo(array('myfriend@hisdomain.com'))
->setBody('Here goes my message')
;
$result = $mailer->send($message);
print_r($result);
This is equivalent to using the mail() function:
mail('myname@mydomain.com', 'sending emails', 'Here goes my message');
One of the benefits of using this library is that you can use an SMTP server which allows you to send emails even when testing locally.
You just need the following details:
- username (e.g.: myname@mydomain.com)
- password (e.g.: mypassword)
- smtpserver (e.g.: smtp.mydomain.com)
This is the basic script to send an email using your SMTP server (SMTP Transport):
require_once 'lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.mydomain.com')
->setUsername('myname@mydomain.com')
->setPassword('mypassword')
;
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setFrom(array('myname@mydomain.com' => 'My Name'))
->setSubject('sending emails')
->setTo(array('myfriend@hisdomain.com'))
->setBody('Here goes my message')
;
$result = $mailer->send($message);
print_r($result);
As everyone has a GMail account, here's how you can use GMail's SMTP. Notice the use of port 465 and the tls protocol.
require_once 'lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'tls')
->setUsername('myname@gmail.com')
->setPassword('mypassword')
;
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setFrom(array('myname@gmail.com' => 'My Name'))
->setSubject('sending emails')
->setTo(array('myfriend@hisdomain.com'))
->setBody('Here goes my message')
;
$result = $mailer->send($message);
print_r($result);
In order to be able to send emails using your GMail account, as tls is being used, your PHP installation has to have openssl enabled. In the output by phpinfo() you should see something like:
For example, in WampServer, it's not enabled by default, but enabling openssl is really straightforward:
Just go to PHP -> PHP extensions and enable php_openssl.
Now you easily send emails from your application running on localhost.
If you test the script directly in your browser, you should see 1 being output because of print_r(). This means that the email was set successfully.
Obviously you may want to provide a better message:
$result = $mailer->send($message);
if($result)
echo 'Email sent successfully.';
else
echo 'Error sending email.';
After you've made sure that everything is working as expected and you can indeed send emails, it's time to turn those hardcoded dummy values into more flexible variables. Notice that you'll be sending the email for yourself so the to and from may be the same.
If you're thinking that you can use the visitor's email address in the from field, let me tell you that this is a really bad idea as it may not work because the SMTP server may consider it as spam. Use reply-to instead. This is achieved with the setReplyTo() method.
Here's a script which sends a message built using the variables $name, $email and $comment. You can run it for testing purposes.
$name = 'Visitor';
$email = 'visitor@domain.com';
$comment = 'I like your website.';
$subject = 'New comment!';
$body = '';
$body .= "\n";
$body .= "Name: $name";
$body .= "\n\n";
$body .= "E-mail: $email";
$body .= "\n\n";
$body .= "Comment:\n\n$comment";
require_once 'lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'tls')
->setUsername('myname@gmail.com')
->setPassword('mypassword')
;
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setFrom(array('myname@gmail.com' => 'My Name'))
->setReplyTo($email)
->setSubject($subject)
->setTo(array('myname@gmail.com'))
->setBody($body)
;
$result = $mailer->send($message);
if($result)
echo 'Email sent successfully.';
else
echo 'Error sending email.';
It's working fine, so move on: you want the variables coming from Flash, via POST, so you should grab them in the $_POST array:
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
You should also make sure they exist and are valid (for example, make sure the email address is indeed an email address).
if (!isset ($_POST['name']) || $_POST['name'] == '')
{
exit ('Error! Name missing.');
}
if (!isset ($_POST['email']) || $_POST['email'] == '')
{
exit ('Error! Email missing.');
}
if (!isset ($_POST['comment']) || $_POST['comment'] == '')
{
exit ('Error! Comment missing.');
}
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
The server side script is ready to receive the variables coming from Flash.
» Level Intermediate |
Added: 2011-03-18 Rating: 1 Votes: 1 |
» Author |
Nuno Mira has been a Flash Developer for 9 years. He loves teaching, and learning. When he isn't coding he may be surfing or snowboarding. |
» Download |
Download the files used in this tutorial. |
Download (37 kb) |
» Forums |
More help? Search our boards for quick answers! |