Hardware Reference
In-Depth Information
How Email Works
Transferring mail also uses a client-server model. It
involves four applications: your email program and your
friend's, and your email server (also called the mail host)
and your friend's email server. Your email program adds a
header to your message to say that this is a mail message,
who the message is to and from, and what the subject
is. Next, it contacts your mail server, which then sends
the mail on to your friend's mail server. When your friend
checks her mail, her mail program connects to her mail
server and downloads any waiting messages. The mail
servers are online all the time, waiting for new messages
for all of their users.
The transport protocol for sending mail is called SMTP,
the Simple Mail Transport Protocol. It's paired with two
retrieval protocols: POP (Post Office Protocol) and IMAP
(Internet Message Access Protocol). Just like HTTP, it's
text-based. PHP has excellent functionality for sending
and retrieving mail, and you can use it as an intermediary
between any local application, like the Processing sketch
that will follow, or some microcontroller applications you'll
see later on.
Try It
Here's a PHP script that
sends a mail to you.
Save this script to your server as mailer.
php, and view it in a browser as you did
with the last script. You should get two
results. In the browser, you'll get a page
that says:
<?php
/*
mailer
Context: PHP
sends an email.
*/
I mailed you@example.com
From: you@example.com
Hello world!
// set up your variables for mailing. Change to and from
// to your mail address:
$to = " you@example.com ";
$subject = "Hello world!";
$from = "From: you@example.com ";
$message = "Hi there, how are you?";
Hi there, how are you?
In your mail client, you'll get a
message like this:
// send the mail:
mail($to, $subject, $message, $from);
From: You <you@example.com>
Subject: Hello world!
Date:
// give notification in the browser:
echo "I mailed " . $to . "<br>";
echo $from . "<br>";
echo $subject. "<br><br>";
echo $message;
?>
May 8, 2013 2:57:42 PM EDT
To:
you <you@example.com>
Hi there, how are you?
Make sure your server is properly configured to send mail here. If you're using a web hosting service, the settings for mail
will be part of your account settings. Also, make sure your from: address is one for which the mail server will relay mail.
That typically means you can only send mail from your own domain. For example, if your domain name is example.com ,
then you can't send mail from cat@ohaikitteh.com .
As you can see, sending mail from PHP is very simple. So in addition to using it to serve web pages, you can use it to send
messages via mail. As long as you have a device or program that can make a GET or POST, you can use PHP or other
server-side programming languages to start a sequence of messages in many different applications across the Net. Now
that you've got the basics of HTTP requests and mail sending, it's time to put them into action in a project.
 
Search WWH ::




Custom Search