Full Version: Send Emails Using Php (basic)
Dream.In.Code > Programming Tutorials > PHP Tutorials
apg88
Send Emails Using PHP (Basic). by apg88

Sending 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
h0meles
What about specifying a from or return addy? Would that go as the fourth string?
NeoGreen
Awesome tutorial man. smile.gif
nitestryker
Here my code
you can use variables from a form such
<input type="text" name="to" size=25">
as $to = $_POST['to']; etc etc....


CODE
<?php
$to = "blah@blah.com";
$subject = "mmmmm coffee";
$message = "mmmmm I love Coffee";
$from = "Iluvcoffee@lol.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent ";
?>
nitestryker
Here is an example from one i wrote

[ index.html ] <--- HTML FILE
CODE

<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  </head>
  <body>
<center>
  <table border="1" bgcolor="red">
  <tr>
  <td>
<form name="form" method="post" action="mail.php">
<center>To: <input type="text" name="to"><br></center>
<center>From: <input type="text name="from"><br></center>
<center>Subject: <input type="text" name="subject"><br></center>
<center>Message:<br><textarea name="message" rows=4 cols=25></textarea></center><br>
<center><input type="submit" value="send"></center>
</td>
</tr>  
</body>
</html>


[mail.php] <---- Php Code
CODE

<?php
$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = $_POST ['from'];
$headers = "From: $from";
mail($to,$subject,$message,$headers);
// more php n echo or include whatever you want
?>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.