PHP send e-mail code

Source: Internet
Author: User
Tags configuration settings mail code php send email

PHP allows us to send email directly from the script.

Mail () function

The Mail () function is used to send e-mail from a script.

Syntax:

    1. /**
    2. * To: Required. Specify email recipients.
    3. * Subject: Required. Specify the subject of email. Note: This parameter cannot contain any new line characters.
    4. * Message: Required. Defines the message to be sent. You should use LF (\ n) to separate the rows.
    5. * Headers: Optional. Specify additional headings, such as from, Cc, and BCC. You should use CRLF (\ r \ n) to separate additional headings.
    6. * Parameters: Optional. Specify additional parameters for the mail sender.
    7. */
    8. Mail (to,subject,message,headers,parameters)
Copy Code

Note: PHP requires an installed and running messaging system to make the mail function available. The program used is defined by the configuration settings in the php.ini file.

Example:

The simplest way to send an email via PHP is to send a text email.
In the following example, we first declare variables ($to, $subject, $message, $from, $headers), and then we use these variables in the mail () function to send an e-mail message:

    1. $to = "someone@example.com";
    2. $subject = "Test mail";
    3. $message = "hello! A simple email message. ";
    4. $from = "someonelse@example.com";
    5. $headers = "From: $from";
    6. Mail ($to, $subject, $message, $headers);
    7. echo "Mail Sent.";
    8. ?>
Copy Code

With PHP, we are able to make a feedback form on our site. The following example sends a text message to the specified e-mail address:

    1. 1. First, check whether the Message entry box is filled in
    2. if (isset ($_request[' email '))
    3. 2. If it is filled out (after the form is filled out), send the message from the form
    4. {
    5. Send email
    6. $email = $_request[' email '];
    7. $subject = $_request[' subject ');
    8. $message = $_request[' message '];
    9. Send To Dream Mailbox
    10. 4. When the Submit button is clicked, reload the page to show the message that the message was sent successfully
    11. Mail ("someone@zmit.cn", "Subject: $subject",
    12. $message, "From: $email");
    13. echo "Thank you to using our mail form";
    14. }
    15. Else
    16. 3. If not filled out (for example, when the page is first accessed), Export the HTML form
    17. {
    18. echo "";
    19. }
    20. ?>
Copy Code

Reprint: PHP Send Email "original"

Send email, PHP
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.