Being able to send emails automatically is very useful. You can remind site users about something or notify them of an event.
IMPORTANT: THIS CODE IS NEVER TO BE USED FOR MALICIOUS PURPOSES, E.G SPAMMING.
First we shall make some code for sending a very simple email.
mail(<recipent>, <subject>, <message>);
In this example Alex is being notified that he needs to pay his bills:
mail("email@example.com", "Bills", "You need to pay water bill today.");
But what if you want a changeable message? We'll add a number for his debt:
$debt; $debt = 100; mail("firstname.lastname@example.org", "Bills", "You owe Faketown water " $debt);
That's all very good, but what if you want a user to be able to edit what goes in the email? We'll add a HTML form...
<form action="send.php" method="post"> From: <input type="text" name="fname" /> Subject: <input type="text" name="age" /> Body: <br /> <textarea name='message' rows='15' cols='40'> </textarea><br /> <input type="submit" /> </form>
Then you would feed that into an email code like the ones you just learnt at send.php using $_post. This is great for customer feedback form, ect.
If you want to stop people spamming you through these form (which is a very good idea) you could add an image captcha from: reCAPTCHA. This link is not
Remember how earlier I told you about notifying users of a site about events? Well here is the bit that tells you about that. (P.S: For this bit you will need to know how to use MySQL.)
Let's say we want a user to be notified of a PM being sent to him/her. Once again we will use Alex as our example (he's probably getting a bit tired of this PHP stuff). We have a very simple PM form:
<form action="send.php" method="post"> Send a PM <br /> To: <input type="text" name="to"> Title: <input type="text" name="title"> Message: <br /> <textarea name='message' rows='15' cols='40'> </textarea> <input method="submit"/> </form>
And then at send.php we have something neat:
//Here send a PM as normal $main="You received a PM entitled: "; $title=$_post["title"]; $recipient=//Here you will have to get the recipient email from a MySQL database; $all=$main.$title; //Fuse the 2 together mail($recipient, "New PM", $all);
And if you do that right, the PM recipient would receive an email something like:
You received a PM entitled: Your tut was not approved.
Well, have fun, pay your bills, don't break your nose, wash the car, cook dinner and use PHP to send emails! Good luck!