Welcome to the Linux community forum and interact with 2 million technical staff. In php, a function mail function is provided to send emails. It can send emails directly in the program, however, it requires that the server support sendmail or set up an email sending server that does not require relay, but now you need to find an email that does not require authentication.
Welcome to the Linux community forum, interact with 2 million technical staff> enter a function for sending emails in php. It can send emails directly in the program, however, it requires that the server support sendmail or set up an email sending server that does not require relay, but now you need to find an email that does not require authentication.
Welcome to the Linux community forum and interact with 2 million technicians>
The mail function is provided in php. It can send emails directly in the program, but it requires the server to support sendmail.
Or you must set up an email sending server that does not require relay, but it is almost impossible to find an email sending relay that does not require authentication.
Therefore, emails cannot be sent successfully using the mail function.
If you are familiar with the SMTP protocol and use the socket function, you can write an efficient and stable mail sending program, but it is too difficult for general users. Fortunately, there are already many email sending modules compiled by others on the Internet. We just need to download the module and call it easily, which is very convenient.
Here, we recommend a very powerful, easy-to-use and free SMTP class module-PHPMailer,
PHPMailer is a PHP function package used to send emails. It provides the following functions:
* Specify multiple recipients, cc addresses, hidden addresses, and reply addresses when sending mail.
* Multiple email codes are supported, including 8bit, base64, binary, and quoted-printable.
*. Supports SMTP authentication.
*. Support for redundant SMTP servers
*. Supports attachments and Html-format emails.
*. Customize the mail header
* Images can be embedded in emails.
* Flexible debugging
*. Compatible SMTP servers include Sendmail, qmail, Postfix, Imail, and Exchange.
*. Can run on any platform
After downloading this component, write the code as follows to enable php to send emails online.
I. html Program
Phpmailer Unit Test
Enter the email address for receiving the email:
Ii. PHP Program
Require ("class. phpmailer. php"); // The downloaded file must be in the directory where the file is located.
$ Mail = new PHPMailer (); // create a mail sending class
$ Address = $ _ POST ['address'];
$ Mail-> IsSMTP (); // send using SMTP
$ Mail-> Host = "mail.xxxxx.com"; // your enterprise Post Office Domain Name
$ Mail-> SMTPAuth = true; // enable SMTP Verification
$ Mail-> Username = "user@xxxx.com"; // Post Office Username (enter the complete email address)
$ Mail-> Password = "******"; // post office Password
$ Mail-> From = "user@xxxx.com"; // email address of the sender
$ Mail-> FromName = "Your name ";
$ Mail-> AddAddress ("$ address", ""); // The recipient address, which can be replaced with any email address in the format of AddAddress ("recipient email ", "Recipient Name ")
// $ Mail-> AddReplyTo ("","");
// $ Mail-> AddAttachment ("/var/tmp/file.tar.gz"); // Add an attachment
// $ Mail-> IsHTML (true); // set email format to HTML // whether HTML format is used
$ Mail-> Subject = "PHPMailer test mail"; // mail title
$ Mail-> Body = "Hello, this is the test mail"; // The mail content
$ Mail-> AltBody = "This is the body in plain text for non-HTML mail clients"; // additional information, which can be omitted
If (! $ Mail-> Send ())
{
Echo "failed to send the email.
";
Echo "error cause:". $ mail-> ErrorInfo;
Exit;
}
Echo "email sent successfully ";
?>