Send Emails Using PHP (Basic). by apg88Sending emails with PHP is a lot easier than you would think.
First of all, we start with our PHP tags <?php and ?>
Then we use a simple function called mail() (Duh!).
The syntax for the mail function is
CODE
mail(string to, string subject, string message, string additional_headers);
For example, if I wanted to send an email to somebody@email.com, I would write:
CODE
mail("somebody@email.com", "Test E-Mail (This is the subject of the E-Mail)", "This is the body of the Email");
The first parameter tells the function who to send the email to.
The second parameter is the subject of the email.
The third parameter is the body of the email.
The fourth parameter is for more advanced uses, so we will ignore it for now.
So you save your PHP file and access it, but aah! You get a blank page. Was the email sent? Who knows...? That’s why we have the if-then function!
If you want to know if your email was sent or if an error occurred, you would type this in.
CODE
if(mail("somebody@email.com", "Test E-Mail (This is the subject of the E-Mail)", "This is the body of the Email")){
echo "The email was successfully sent.";
} else {
echo "The email was NOT sent.";
}
This function, like every other function also works with variables, here is an example of a fully functional email script.
CODE
<?php
$email_to = "somebody@email.com";
$email_subject = "Test E-Mail (This is the subject of the E-Mail)";
$email_body = "This is the body of the Email \nThis is a second line in the body!";
if(mail($email_to, $email_subject, $email_body)){
echo "The email($email_subject) was successfully sent.";
} else {
echo "The email($email_subject) was NOT sent.";
}
?>
That’s all there is to it!
www.apg88.com