This is using the open source project PHPMailer to send emails. First download the file. I downloaded version 5.1 here, and then put the three class.***.php files under the project file. I first used gmail, but it failed. The problem was that the authentication failed. Later, I used QQ mailbox and the message was sent successfully.
- require("class.phpmailer.php"); //The downloaded file must be placed in the directory where the file is located
- $mail = new PHPMailer(); //Create an email sending class
- $address ="youbinliu@126.com";
- $mail->IsSMTP(); // Use SMTP to send
- $mail->Host = "smtp.qq.com"; // Your business post office domain name
- $ mail->SMTPAuth = true; // Enable SMTP authentication function
- $mail->Username = "843831601@qq.com"; // Post office username (please fill in the complete email address)
- $mail->Password = "***********"; //Post office password
- $mail->Port=25;
- $mail->From = "843831601@qq.com"; //Email sender Email address
- $mail->FromName = "liuyoubin";
- $mail->AddAddress("$address", "a");//The recipient address can be replaced with any email address you want to receive emails from , the format is AddAddress("recipient email", "recipient name")
- //$mail->AddReplyTo("", "");
-
- //$mail->AddAttachment("/var /tmp/file.tar.gz"); //Add attachment
- //$mail->IsHTML(true); // set email format to HTML //Whether to use HTML format
-
- $mail->Subject = "PHPMailer test email"; //Email title
- $mail->Body = "Hello, this is a test email"; //Email content
- $mail->AltBody = "This is the body in plain text for non- HTML mail clients"; //Additional information, you can omit
-
- if(!$mail->Send())
- {
- echo "Failed to send email.
";
- echo "Error reason: " . $ mail->ErrorInfo;
- exit;
- }
-
- echo "Mail sent successfully";
-
- /***************************************************
-
- Attachments :
- phpmailer Chinese Instructions for Use (Simplified Version)
- Start with A:
- $AltBody--Attribute
- From: PHPMailer::$AltBody
- File: class.phpmailer.php
- Note: The setting of this attribute is in the email body and does not support HTML The alternative display
- AddAddress--method
- comes from: PHPMailer::AddAddress(), file: class.phpmailer.php
- Description: Add recipients. Parameter 1 is the recipient's email address, and parameter 2 is the recipient's title. For example, AddAddress("eb163@eb163.com","eb163"), but parameter 2 is optional, and AddAddress(eb163@eb163.com) is also possible.
- Function prototype: public function AddAddress($address, $name = '') {}
- AddAttachment--Method
- From: PHPMailer::AddAttachment()
- File: class.phpmailer.php.
- Instructions: Add attachments.
- Parameters: path, name, encoding, type. Among them, the path is required, and the others are optional. Function prototype:
- AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream'){}
- AddBCC- -Method
- From: PHPMailer::AddBCC()
- File: class.phpmailer.php
- Description: Add a BCC. For the difference between CC and BCC, please see [Difference between BCC and CC in SMTP sending].
- Parameter 1 is the address, parameter 2 is the name. Note that this method only supports SMTP under win32 and does not support the mail function.
- Function prototype: public function AddBCC($address, $name = ''){}
- AddCC --Method
- from: PHPMailer::AddCC()
- file : class.phpmailer.php
- Description: Add a CC. For the difference between CC and BCC, please see [Difference between BCC and CC in SMTP sending].
- Parameter 1 is the address, parameter 2 is the name. Note that this method only supports SMTP under win32 and does not support the mail function.
- Function prototype: public function AddCC($address, $name = '') {}
- AddCustomHeader--Method
- From: PHPMailer::AddCustomHeader()
- File: class.phpmailer.php
- Description: Add a custom E-mail header.
- The parameter is the header information
- Function prototype: public function AddCustomHeader($custom_header){}
- AddEmbeddedImage --Method
- From: PHPMailer::AddEmbeddedImage()
- File: class.phpmailer.php
- Description: Add an embedded image
- Parameters: path, return handle [, name, encoding, type]
- Function prototype: public function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream ') {}
- Tips: AddEmbeddedImage(PICTURE_PATH. "index_01.jpg ", "img_01 ", "index_01.jpg ");
- Referenced in html
- AddReplyTo--method
- From: PHPMailer:: AddRepl
- **** **********************************************/
- ?>
-
Copy code
|