A useful feature of PHP is that users of the site can send an email from your website! This could be to you, a specific person, or whoever they specify!
This is done using the mail() function! The syntax of this function is shown below:
mail(recipient of email (required), subject of email (required), message of email (required), headers of email (optional), parameters of email(optional));
The header can be used for things like From, BC, CC, and other things like that.
The simplest way to do this is to make some variables, give them values, then put them into the mail function:
$to = "you@email.com"; $subject = "Hello!"; $message = "How are you?"; $sender = "Your PHP Teacher"; $header = "From: " . $sender; mail($to, $subject, $message, $header);
You can also set up an email form for users to your website. An if/else statement can do this job. You can check if the form has been filled out and, if it has, send the email with the specified properties. It can be done like this:
if (isset(_$POST['to']) && isset(_$POST['subject']) && isset(_$POST['message']) { $to = _$POST['to'];
So on so forth, continuing with what we just did...
else { echo '< form method="post" action="mailform.php > Email: < input name="to" type="text" >< br > Subject: < input name="subject" type="text" >< br > Message: < br > < textarea name="message" rows="15" cols="40" > < /textarea >< br > < input type="submit" > < /form >'; }
*spaces added before/after brackets so as to make the code visible to you
To clear up things a little bit, the isset() function checks if the form requirements have already been set and sends the email if it has. If it hasn't it displays the email form instead. Once the submit button is pressed the emails sends. The action links to a separate PHP file, which can do something like say to the user that they have sent the email.
Now you have learned how to make an email form for your PHP page!