Last active
December 30, 2015 05:59
-
-
Save abdullahbutt/7786495 to your computer and use it in GitHub Desktop.
mail() function in php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Definition and Usage | |
| The mail() function allows you to send emails directly from a script. | |
| This function returns TRUE if the email was successfully accepted for delivery, otherwise it returns FALSE. | |
| Syntax | |
| mail(to,subject,message,headers,parameters) | |
| 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. | |
| Windows note: If a full stop is found on the beginning of a line in the message, it might be removed. To solve this problem, replace the full stop with a double dot: | |
| <?php | |
| $txt = str_replace("\n.", "\n..", $txt); | |
| ?> | |
| headers Optional. | |
| Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n). | |
| Note: When sending an email, it must contain a From header. This can be set with this parameter or in the php.ini file. | |
| parameters Optional. | |
| Specifies an additional parameter to the sendmail program (the one defined in the sendmail_path configuration setting). (i.e. this can be used to set the envelope sender address when using sendmail with the -f sendmail option) | |
| Tips and Notes | |
| Note: Keep in mind that just because the email was accepted for delivery, it does NOT mean the email is actually sent and received. | |
| ---------------------------------------------------------------------------- | |
| Example 1 | |
| Send a simple email: | |
| */ | |
| ?> | |
| <?php | |
| $txt = "First line of text\nSecond line of text"; | |
| // Use wordwrap() if lines are longer than 70 characters | |
| $txt = wordwrap($txt,70); | |
| // Send email | |
| mail("somebody@example.com","My subject",$txt); | |
| /* | |
| ------------------------------------------------------------------------------ | |
| Example 2 | |
| Send an email with extra headers: | |
| */ | |
| $to = "somebody@example.com"; | |
| $subject = "My subject"; | |
| $txt = "Hello world!"; | |
| $headers = "From: webmaster@example.com" . "\r\n" . | |
| "CC: somebodyelse@example.com"; | |
| mail($to,$subject,$txt,$headers); | |
| /* | |
| ------------------------------------------------------------------------------ | |
| Example 3 | |
| Send an HTML email: | |
| */ | |
| <?php | |
| $to = "somebody@example.com, somebodyelse@example.com"; | |
| $subject = "HTML email"; | |
| $message = " | |
| <html> | |
| <head> | |
| <title>HTML email</title> | |
| </head> | |
| <body> | |
| <p>This email contains HTML Tags!</p> | |
| <table> | |
| <tr> | |
| <th>Firstname</th> | |
| <th>Lastname</th> | |
| </tr> | |
| <tr> | |
| <td>John</td> | |
| <td>Doe</td> | |
| </tr> | |
| </table> | |
| </body> | |
| </html> | |
| "; | |
| // Always set content-type when sending HTML email | |
| $headers = "MIME-Version: 1.0" . "\r\n"; | |
| $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; | |
| // More headers | |
| $headers .= 'From: <webmaster@example.com>' . "\r\n"; | |
| $headers .= 'Cc: myboss@example.com' . "\r\n"; | |
| mail($to,$subject,$message,$headers); | |
| ?> | |
| <?php | |
| /* | |
| --------------------------------------------------------------------------------- | |
| 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: | |
| */ | |
| $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, | |
| $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='#'> | |
| 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> | |
| <?php | |
| /* | |
| 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. | |
| PHP Mail Reference | |
| For more information about the PHP mail() function, visit our PHP Mail Reference: | |
| http://www.w3schools.com/php/php_ref_mail.asp | |
| */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment