Mail() From Start To Finish
(Page 1 of 3 )
Ever wanted to know how to send basic text emails/Html emails and emails with attachments? In this tutorial you will learn how to send emails in text, html and with attachments.
A text email is the simplest email you can possibly send. The code you can use is easy to create and send one is easy too. Firstly I will show you how to send basic text emails from strings coded into the script, then I will finish off this section with a example that uses forms to send form data to an email.
<?php /* First of all we define the variables which are the message, the from address, the to address and the subject along with the headers which are used to show who sent the email. */ $message = "Hello Someone\n this is a simple text email message\n"; $from = "someone@domain.com"; $to = "someone@anotherdomain.com"; $subject = "Simple Text Email"; $headers = "From: $from"; /* Now we are ready to send the email so we call php's mail() function with the appropriate variables from above included in the brackets */ mail($to,$subject,$message,$headers); ?> |
That’s all's that their is to sending basic text email messages using php's built in mail() function. Now what if you have a form that website users can use you to contact the webmaster. How do you send the information from the form to an email address you may be asking. Well its not that hard as i will show in my example below:
<?php /* First we need to check if the form has been submitted by the user */ if(!isset($submit)){ /* If form hasn’t been submitted */ echo "<form action='$_SERVER[PHP_SELF]' method='post'> Name:<input type='text' name='name'><br> Subject:<input type='text' name='subject'><br> Email:<input type='text' name='email'><br> Message:<br> <textarea name='message' cols='40' rows='15'></textarea><br> <input type='submit' name='submit' value='send'> </form>"; }else{ /* If form has been submitted */ $to="myemail@mydomain.com"; /* Get the form date from the variable $_POST which stores everything from the form since the method of the form is post if it was get you would use $_GET instead of $_POST. You can reference it directly e.g. $email but it may not work because the setting register_globals is off by default which means you have to use $_POST or $_GET to access form data */ $from=$_POST['email']; $message=$_POST['message']; $subject=$_POST['subject']; $submit=$_POST['submit']; /* Send The Email*/ $headers = "From: $from"; if(mail($to,$subject,$message,$headers)){ echo "Email Sent"; }else{ echo "Email Sending Failed"; } /* Notice that I have added an if statement to determine if the mail() function was run successfully, this does not mean the email has been sent. It may bounce off the mail server and will be sent to the email address it was sent from which in this case is the email specified in $from */ } ?> |
Wasn’t that easy? Lets Carry on to look at sending html emails and Sending emails with attachments.
Next: Sending Html Emails >>
More Miscellaneous Articles
More By Andrew Walsh