Home Backend Development PHP Tutorial Two ways to send emails in php. PHP uses smtp to send emails.

Two ways to send emails in php. PHP uses smtp to send emails.

Jul 30, 2016 pm 01:31 PM
mail nbsp php quot smtp

Original text: http://www.jbxue.com/php/25502.html
This article introduces two methods of sending emails in PHP. They are to use PHP’s built-in mail() function to send emails, and to use the email class that encapsulates the SMTP protocol. To send emails, it is recommended to use the email class encapsulated by SMTP protocol to send emails.

How to send emails in php? There are many methods, but the most commonly used one is to use the SMTP protocol to send emails. Let’s learn together.
Special recommendation: php email code collection

1. Use PHP’s built-in mail() function

Copy the code Code example:

<?php
$to = "test@163. com"; //Recipient
$subject = "Test"; //Subject
$message = "This is a test mail!"; //Text
mail($to,$subject,$message);

Report an error directly:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() inD:/www /Zend/email/email.php on line 10

You need a local SMTP server, the code is modified to:

Copy the code Code example:

<?php
$to = "test@163. com";//Recipient
$subject = "Test";//Email subject
$message = "This is a test mail!";//Email text
ini_set('SMTP','smtp.163.com ');//Sending SMTP server
ini_set('smtp_port',25);//Sending SMTP server port
ini_set('sendmail_from',"admin@163.com");//Sender's email address
mail($to,$subject,$message);

Continue error:
Warning: mail() [function.mail]: SMTP server response: 553 authentication is required,smtp2,DNGowKD7v5BTDo9NnplVBA--.1171S2 1301220947 inD:/www/Zend/email/email.php on line 9

Verification information is required. How to write verification information? Where to configure it?

To use the mail() function to send emails, you must have a mail server that can send emails without SMTP authentication. But today's SMTP mail servers basically require authentication, so if you want to use it to send emails, you can only set up a local SMTP server that does not require authentication.
Conclusion: To use the mail() function to send emails, you must have an SMTP server that does not require authentication.

2. Use an email class that encapsulates the SMTP protocol
It is recommended to use the SMTP protocol to send emails.

It is recommended to use the Mail class in the PEAR extension. It has powerful functions: it can support emails in plain text and HTML formats; encoding can be set for each field, and correct configuration will not cause Chinese garbled characters; it can support attachments, etc.

You can use the pear install Mail command on the server to quickly install it. Students who do not have sufficient server permissions can also directly download the PHP source code of the class and include it.

Note: The Mail class depends on Net/SMTP.php and Mail/mime.php, which need to be downloaded together and included together when using them.

Detailed installation methods can be viewed on the official website, http://pear.php.net/package/Mail.

Example, Mail class method of sending emails.

Copy code Code example:

<?php
// Pear Mail extension
require_once('Mail.php');
require_once('Mail/mime.php');
require_once('Net/SMTP.php');

$smtpinfo = array();
$smtpinfo["host"] = "smtp.163.com";//SMTP server
$smtpinfo["port"] = "25"; //SMTP server port
$smtpinfo["username" ] = "username@163.com"; //Sender's email
$smtpinfo["password"] = "password";//Sender's email password
$smtpinfo["timeout"] = 10;//Network Timeout, seconds
$smtpinfo["auth"] = true;//Login verification
//$smtpinfo["debug"] = true;//Debug mode

// Recipient list
$mailAddr = array( 'receiver@163.com');

//Sender display information
$from = "Name <username@163.com>";

// Recipient display information
$to = implode(' ,',$mailAddr);

// Email title
$subject = "This is a test email";

// Email body
$content = "<h3>Write anything you like</h3> ";

//Email text type, format and encoding
$contentType = "text/html; charset=utf-8";

//Newline symbol Linux: n Windows: rn
$crlf = "n"; $mime = new Mail_mime($crlf);
$mime->setHTMLBody($content);

$param['text_charset'] = 'utf-8';
$param['html_charset'] = 'utf- 8';
$param['head_charset'] = 'utf-8';
$body = $mime->get($param);

$headers = array();
$headers["From"] = $from;
$headers["To"] = $to;
$headers["Subject"] = $subject;
$headers["Content-Type"] = $contentType;
$headers = $mime-&gt ;headers($headers);

$smtp =& Mail::factory("smtp", $smtpinfo);


$mail = $smtp->send($mailAddr, $headers, $body);
$smtp->disconnect();

if (PEAR::isError($mail)) {
//Sending failed
echo 'Email sending failed: ' . $mail->getMessage()."n";
}
else{
//Sent successfully
echo "success!n";
}

The above introduces the two methods of sending emails in php. PHP uses smtp to send emails, including some aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles