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

Join 86,392 Programmers. There are 1,404 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Send Emails Using Php (basic)

 
Reply to this topicStart new topic

> Send Emails Using Php (basic)

apg88
*



post 21 Jan, 2005 - 02: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


Register to Make This Ad Go Away!

h0meles
*



post 10 Sep, 2007 - 01:20 PM
Post #2
What about specifying a from or return addy? Would that go as the fourth string?

NeoGreen
**



post 24 Sep, 2007 - 07:56 PM
Post #3
Awesome tutorial man. smile.gif

nitestryker
*



post Yesterday, 02: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: Yesterday, 02:42 PM

nitestryker
*



post Yesterday, 02: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: Yesterday, 03:01 PM


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: 5/17/08 05:03AM

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