The PHP mail () function sends an example of an e-mail usage

Source: Internet
Author: User
Tags qmail
First, Introduction
Mail () function, you can use this function to send e-mail.
Demand
In order to be able to use the mail function, PHP must have the power to compile and send binary mail files in your system. If you use other mail programs, such as QMail or postfix, you must be sure to use them to send mail packets. PHP will first look for sendmail on your path, so the following path::/usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib is recommended. Users who also compile PHP must have access to binary sendmail power.
Installation
These functions are part of the PHP core and can be used without being installed.
Run-time configuration
The behavior of these functions is affected by the global configuration file php.ini.
Table 1. Mail Basic configuration options:

Name default value can change value

SMTP "localhost" php_ini_all

Smtp_port "Php_ini_all"

Sendmail_from NULL Php_ini_all

Sendmail_path Default_sendmail_path Php_ini_system

See Ini_set () function For more information about the configuration options for mail. The following is a brief explanation of this configuration option.
SMTP string
Use only the DNS name or IP address of the WINDOWS:SMTP server. PHP uses the SMTP server when sending mail using the mail () function.
Smtp_portint
Use only in Windows: Set the port number to connect to the specified SMTP server when sending mail with the mail () function, default: 25. Available only after PHP 4.3.0.
Sendmail_from string
PHP will use the "from:" E-mail address when sending mail in a Windows system.
Sendmail_path string
Where to find the sending mail program. Usually in:/usr/sbin/sendmail or/usr/lib/. In this configuration option a default value is set for you to work properly. But if it fails, you can set it up here.
If the system cannot use SendMail, you should follow this instruction to set up the SendMail wrapper/replace the mail system they provide. For example, the QMail user can set it to a new path:/var/qmail/bin/sendmail or/var/qmail/bin/qmail-inject.
Qmail-does not require any options to properly process the message.
Resource type
The extension does not have any resource types defined.
Pre-defined constants
The extension does not define any constants

Ii. Methods of Use
Grammar:

BOOL Mail (string to, string subject, String message [, String additional_headers [, String additional_parameters]])

The mail () function can send the specified message (string message) to the specified e-mail address (string to). Multiple mail addresses are separated by commas. You can use this function to send mail attachments and special types.
The various parameters in the above syntax represent the meaning:
String to--the recipient's email address,
The subject of string subject--email,
The body of string message--email,
String additional_headers--Additional information from the email to the end of the letter.
If the message is sent successfully, the mail () function returns True, otherwise false is returned. Attention!

The Mail () function is implemented in Windows in many different ways than Unix systems.
1, it does not use the local binary system to form the message body;
2, similar from:, Cc:, BCC: and date: The headers head element, first not to be interpreted by the MTA, but can be interpreted by PHP. php < 4.3 supports only Cc: and case-sensitive, php >= 4.3 supports all mentioned headers header elements and is case-insensitive.
Two. i. Example 1, sending mail:

Mail ("joecool@example.com", "My Subject", "line 1nLine 2nLine 3");

Two. If you use the fourth parameter, the parameter string will be inserted at the end of the letter header. This is a typical insert used to illustrate additional letter header information. Multiple additional letter header information is separated by a carriage return R and a newline n character.
Example 2. Send additional letter Header message:

Mail ("nobody@example.com", "the subject", $message, "from:webmaster@{$_server[' server_name ']}rn". " Reply-t webmaster@{$_server[' server_name ']}rn. " x-mailer:php/". Phpversion ());

Two. If the additional_parameters parameter is used, the program sets an additional parameter with the Sendmail_path configuration when sending the message. For example, this can be used to set the sender address envelope when the-f option is used before the message. When you use this method to set the sender envelope, you might need to add the user to your Web server need to add the user that your Web server runs as to your SendMail configuratio N to prevent a ' x-warning ' header from being added to the message when you set the envelope sender using this method.
Example 3. Send an additional message header message and attach an instruction parameter.

Mail ("nobody@example.com", "the subject", $message, "from:webmaster@{$_server[' server_name ']}", "-fwebmaster@{$_ser ver[' server_name '} ");

Note: The fifth parameter is appended to the PHP 4.0.5 version. In a later version of PHP 4.2.3, the security mode is disabled, and if it is used it will return a warning message and return a value of false.
You can also create a complex mail message with a simple string construction technique.
Example 4. Send a complex message

/* Recipient */$to = "Mary". ","; Note the comma $to. = "Kelly"; /* Theme */$subject = "Birthday Reminders for August";/* body */$message = ' Here is the birthdays upcoming in august! '; /* You can set the header content: Content-type to send HTML-formatted messages. */$headers = "Mime-version:1.0rn"; $headers. = "content-type:text/html; Charset=iso-8859-1rn "; /* Add header message */$headers. = "From:birthday Reminder rn"; $headers. = "CC:BIRTHDAYARCHIVE@EXAMPLE.COMRN"; $headers. = "Bcc:birth DAYCHECK@EXAMPLE.COMRN ";/* Send it */mail ($to, $subject, $message, $headers) @example .com> @example. com>@ Example.com>

Attention:
1. Do not have a newline symbol in the recipient's address or subject, or the message may not be sent out.
2. The recipient address parameter (string to) cannot exist as "Something <someone@example.com>", otherwise the mail command may not be parsed correctly when using MTA.
3, PHP use the Mail function to send the message header garbled problem PHP program using the Mail () function when sending mail, the title of Chinese words will appear garbled.
Workaround:
First Use the function Base64_encode ()-encode the data using the MIME base64 before the header string with the encoding type such as: =? UTF-8? B? Title string after add:? = Message Header Description content-type-prevent message body also garbled
Example:

 $to = ' xinple@example.com '; $subject = "=? UTF-8? B? ". Base64_encode (' mail header '). "? = "; $headers = ' mime-version:1.0 '. "RN"; $headers. = ' content-type:text/html; Charset=utf-8 '. "RN";//Additional headers$headers. = ' To:xinple '. "RN"; $headers. = ' From:admin '. "RN"; $headers. = ' Reply-to:xinple '. "RN"; Mail ($to, $subject, $message, $headers) @example > @example .com> @example. com 

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.