Use PHP to send email 2

Source: Internet
Author: User
The mail () function is very simple: there are only five parameters, and two of them are optional. these parameters are: Other file information headers (optional) other configuration options (optional) of the SMTP Service program. additional header parameters such as CC, BCC, mail functions such as Reply-To, or other SyntaxHighlighter. all (

The mail () function is very simple: there are only five parameters, and two of them are optional. these parameters are: Other file information headers (optional) other configuration options (optional) of the SMTP Service program. additional header parameters such as CC, BCC, reply-To or other mail functions that follow the SMTP protocol. in this example, I only use the From and Reply-To information headers. if you want to send me an email but you are using a non-Windows system, the program code should be as follows: If you are using a Windows-based SMTP service, you may not need to use the fifth parameter and add it to the header information parameter (that is, the fourth parameter ), you need to separate them for writing-use instead. therefore, the code for sending the same email through the Windows-based SMTP service is as follows: The echo statement in the script enables your Web browser to display a message to you when the script is executed. if you do not write the echo statement, you will get a "empty file" dialog box, because no output can be sent to the browser. as long as you can connect to the specified SMTP server mail () function, the true value will be returned. however, this does not mean that the email has successfully arrived at the receiver. the mail () function does not wait for or report the successful/error code sent by the SMTP server. the mail () function may return a false value, and then warn you that "cannot connect, in Row x" or "unknown error, in Row x. "If any of the two messages appears, you should check php. SMTP value in ini. there are two possible causes for these messages: the SMTP server is paralyzed, or PHP cannot connect to it. no matter which of the two cases, your email cannot be sent out. this script uses hard-coded values for these parameters. using a simple HTML form, you can insert some values in these parameters and have a good feedback form. once you know how to send emails to a person, you may not be able to send the same email to many recipients-just like a news letter. all of these involve a nice and lightweight loop to traverse a list of recipients and send emails to them. the mail function is in the same format: mail ([recipient], [subject], [email subject], [header information]); you only need to replace the [receiver] with the next name in the list. suppose you already have an array of email addresses: $ addresses = array ("me@mycompany.com", "you@yourcompany.com", "someone@otherplace.com"); all you need to do is loop through this array, get the new address and send the email. if you have a list of email addresses in the database, the same principle applies: traverse them cyclically. you have learned the essentials. obviously, the same concept applies to your specific database type and table structure, although the code will need to be modified. when people traverse an address list in a loop, they seem to ignore one point: if you have more than 50 addresses to process, your script may time out before it is completed, because PHP has a default time limit of 30 seconds for running each script. you can modify the time limit, but be careful when doing so. this modification should be limited to a specific script, for example, traversing a script for sending emails in the address list. otherwise, you may manually overload the server thread and your computer will crash. to set a time limit in the script, you can use the set_time_limit () function. you can set a time limit, for example, 60 seconds (set_time_limit (60), or you can keep it running until it completes (set_time_limit (0 )). after you add this code to your script, your email address traversal cycle will continue to work happily until the email is sent to all the addresses in the list. it also seems difficult to send HTML-format emails. once you learned how to do this, you may kick yourself because you thought it was hard. we need to make two changes to the email script we have prepared: Your email should be in HTML format. you must add the content-type header. this is all you need to do. the recipient with an email client that can read the HTML format will see their emails in large, bold format. if the HTML-format email you send includes image tagging, you must remember to use the complete URL in the src attribute, for example :. you should also remember that not everyone can read emails in HTML format, and those who cannot read this format can only see the HTML code itself. this is annoying, so it is best to think twice. conclusion you can use PHP to send emails in any way, method, or any form you can imagine. this tutorial only provides the basic knowledge: Configure your server and send simple emails. you can also send attachments along with emails generated by using the mail () function to modify a lot of different header information, there are also other tasks that can be done by the email client program and the mail list Sender program.

Related Article

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.