php Send mail

Source: Internet
Author: User
Tags php send mail

1. PHP Built-in mail () function

PHP itself provides a mail send function mail, you can send e-mail directly in the program, but this function requires the server to support SendMail or must set up a mail sending server that does not need to relay, but now to find a message that does not require authentication to send relay is almost impossible, So using the mail function often fails to send email successfully. If you are familiar with the SMTP protocol, combined with the socket function can write efficient and stable mail-sending program, but for the general user is too difficult. Fortunately, the Internet has a lot of other people to write a good mail delivery module, we only need to download the simple call can, very convenient.

2. Message classes using the Encapsulated STMP protocol

Phpmailer recommended here

The first step: need to download the Phpmailer package The second step: Confirm that your server system has supported sockets such as, through Phpinfo (); see if support sockets if not, please note that the socket is part of the PHP extension and must be given a compile-time configuration options for the./configure--enable-sockets. The third step: Extract the files to your Web server directory, call the class can be, description: First include class.phpmailer.php, then create the object, set parameters, call member functions. See the sample code below for details:
<?PHPIni_set(' Date.timezone ', ' PRC ');require_once('./class.phpmailer.php ');include_once("class.smtp.php");//Optional, otherwise it will be included in the class.phpmailer.php$mail=NewPhpmailer (true);//instantiate the Phpmailer class, True indicates that an exception is thrown when an error occurs$mail-&GT;ISSMTP ();//using the SMTPTry {    $mail->charset = "UTF-8";//Set Message encoding    $mail->host = "smtp.163.com";//SMTP Server    $mail->smtpdebug = 1;//Enable SMTP Debugging 1 = errors 2 = Messages    $mail->smtpauth =true;//server requires authentication    $mail->port = 25;//default port//$mail->port = 465;         SSL authentication when the default port//$mail->smtpsecure = "SSL";                     $mail->smtpsecure = "SSL";     $mail->host = "smtp.gmail.com";     $mail->username = "[email protected]";//user account for the SMTP server    $mail->password = "fanyi303114";//user password for SMTP server    $mail->addreplyto (' [email protected] ', ' reply ');//when the recipient replies to this mailbox, the method can be executed multiple times//recipients if multiple people send a loop to execute the addaddress () method can also have a method to clear the recipient mailbox Clearaddresses ()    $mail->addaddress (' [email protected] ', ' Liuchuanwei '); $mail->setfrom (' [email protected] ', ' sender ');//Sender's email//$mail->addattachment ('./img/bloglogo.png '); Add an attachment that repeats the method if there are multiple attachments    $mail->subject = ' Here is the title of the message '.Date(' y-m-d h:i:s '); //The following is the message content    $mail->body = ' Date(' his '); $mail->ishtml (true); //$body = file_get_contents (' tpl.html ');//Get HTML page content//$mail->msghtml (str_replace (' \ \ ', ', $body));    $mail-Send (); Echo"Message Sent OK";} Catch(phpmailerexception$e) {    Echo $e->errormessage ();//catching exceptions from Phpmailer}Catch(Exception $e) {    Echo $e-getMessage ();}?>

php Send mail

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.