In a site or a webapp the capability to send an email is a must have. The PHP language is a very complete language and provides a lot of functions to simplify your devloping.
Inside the tons of functions provided by the PHP language (both versions 4 and 5) you can find also a dedicated function to send an email.
Naturally this function is the "mail()" function.
Let's give a look to this function:
SINOPSYS
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
The "to" parameter must comply to RFC2822 (i.e. user@example.com[, anotheruser@example.com])
The mail() function returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
A simple example could be useful:
<?php
$subject = "Subject of the email";
$body = "Body of the email\n\non another line";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed…</p>");
}
?>
That's all.
gg1
Nice blog. Thanks for explaining with example.
You really should include something about security. If the above code were used as is with the hard-coded variables replaced with $_POST vars, the form would be trivially hackable. It would be usable for both email spoofing and also mass-email spamming.
Not addressing basic security when giving code to new programmers is irresponsible. You know how in every movie someone hands someone else a gun for the first time and they stare down the barrel? That's what you're doing here. Handing someone a gun and not telling them which end can kill them.
Did you find something about security when you studied C language on the kern/ritch?
the security is not the objective of this post.