Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,411 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,348 people online right now. Registration is fast and FREE... Join Now!




Send Emails Using Php (basic)

 
Reply to this topicStart new topic

> Send Emails Using Php (basic)

apg88
*



post 21 Jan, 2005 - 01:50 PM
Post #1


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
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

h0meles
*



post 10 Sep, 2007 - 12:20 PM
Post #2
What about specifying a from or return addy? Would that go as the fourth string?
Go to the top of the page
+Quote Post

NeoGreen
**



post 24 Sep, 2007 - 06:56 PM
Post #3
Awesome tutorial man. smile.gif
Go to the top of the page
+Quote Post

nitestryker
*



post 16 May, 2008 - 01:35 PM
Post #4
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 ";
?>


This post has been edited by nitestryker: 16 May, 2008 - 01:42 PM
Go to the top of the page
+Quote Post

nitestryker
*



post 16 May, 2008 - 01:58 PM
Post #5
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 post has been edited by nitestryker: 16 May, 2008 - 02:01 PM
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/22/08 08:41AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month