The PHP mail() Function
The PHP mail() function is used to send emails from inside a script.
Syntax
mail(to,subject,message,headers,parameters)
| Parameter | Description |
|---|---|
| to | Required. Specifies the receiver / receivers of the email |
| subject | Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters |
| message | Required. Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters |
| headers | Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n) |
| parameters | Optional. Specifies an additional parameter to the sendmail program |
Note: For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file.
PHP Simple E-Mail
The simplest way to send an email with PHP is to send a text email.
In the example below we first declare the variables ($to, $subject, $message, $from, $headers), then we use the variables in the mail() function to send an e-mail:
<?php $to = "someone@example.com"; $subject = "Test mail"; $message = "Hello! This is a simple email message."; $from = "someonelse@example.com"; $headers = "From: $from"; mail($to,$subject,$message,$headers); echo "Mail Sent."; ?>
PHP Mail Form
With PHP, you can create a feedback-form on your website. The example below sends a text message to a specified e-mail address:
<html>
<body>
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail( "someone@example.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
</body>
</html>
This is how the example above works:
- First, check if the email input field is filled out
- If it is not set (like when the page is first visited); output the HTML form
- If it is set (after the form is filled out); send the email from the form
- When submit is pressed after the form is filled out, the page reloads, sees that the email input is set, and sends the email
Note: This is the simplest way to send e-mail, but it is not secure. In the next chapter of this tutorial you can read more about vulnerabilities in e-mail scripts, and how to validate user input to make it more secure.
It\’s taken the USA by storm and people are earning big bucks with through healthy chocolate…sounds good to me!
Thank you for your help!
Hi there I like your post
Substantially, the post is really the sweetest topic on this related issue. I concur with your conclusions and will thirstily look forward to your coming updates. Saying thanks will not just be adequate, for the tremendous clarity in your writing. I will instantly grab your rss feed to stay abreast of any updates.
Seems like that you have put a lot of effort and hard work into your article and I need additional of these on the web in recent times. I sincerely got a kick from your post. I don’t truly have much to talk about responding, I only wanted to comment to reply wonderful work.
Thanks for thenice post. I really find it very communicative.it issupport for me . I would now go on this blog evermoreday!
Hiya, I was reading another thing about this on another blog. Interesting. Your perspective on it is diametrically contradicted from what I read earlier. I’m still pondering with the opposite points of view, but I’m leaning to a great extent toward yours. And irrespective, that’s what is so perfect about modern-day democracy and the marketplace of ideas online.
I really enjoyed this post, especially the “examples in this post” portion which made it really easy for me to SEE what you were talking about without even having to leave the article. Thanks
It sounds like you’re creating problems yourself by seeking to solve this issue instead of looking at why their is usually a problem in the 1st place. thanks !!! really very helpful post!
I would be grateful if you continue with the quality of what we are doing now with your blog … I really enjoyed it
Love the layout. Graphics look good. Hope to land one…..
I just needed to say that I found your blog via Goolge and I am glad I did. Keep up the good work and I will make sure to bookmark you for when I have more free time away from the books. Thanks again!
I often read your blog and always find it very interesting. Thought it was about time i let you know…Keep up the great work
I just needed to say that I found your blog via Goolge and I am glad I did. Keep up the good work and I will make sure to bookmark you for when I have more free time away from the books. Thanks again!
Cool, I read blogs on a similar topic, but i never visited your blog. I added it to favorites and …
Wonderful information about management. I have bookmarked this blog for my continued information.
Between me and my husband we’ve owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I’ve settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated (and widely mocked) Zunes are.
Nice post! I want to subscribe this blog, but I don’t know how to do, can anybody give me a hand?