PHP combined with PHPMailer calls SMTP to send emails.
Published: 2011-09-06
php itself provides a mail sending function mail, which can directly send emails in a program. However, this function requires the server to support sendmail or requires setting up a mail sending server that does not need a relay, but now it is almost impossible to find a mail sending relay that does not require authentication.
Therefore, using the mail function often cannot successfully send email.
If you are familiar with the SMTP protocol yourself, combined with socket functionality, you can write an efficient and stable mail sending program, but it is too difficult for ordinary users. Fortunately, there are already many mail sending modules written by others on the Internet, and we only need to download them and call them simply, which is very convenient.
Here we recommend to users a very powerful, easy-to-use and free SMTP class module under php - PHPMailer, (download address http://downinfo.myhostadmin.net/mail/PHPMailer-BMH_v5.0.0.zip) PHPMailer is a PHP function package used to send emails. Its provided features include:
*. Specify multiple recipients, CC addresses, BCC addresses and reply addresses when sending mail
*. Supports multiple mail encodings including: 8bit, base64, binary and quoted-printable
*. Supports SMTP authentication
*. Supports redundant SMTP servers
*. Supports emails with attachments and Html format emails
*. Custom email headers
*. Supports embedding images in emails
*. Flexible debugging
*. Tested compatible SMTP servers include: Sendmail,Postfix,Imail,Exchange, etc.
*. Can run on any platform
http://downinfo.myhostadmin.net/mail/phpmail_test.rar (You can download this compressed package, unzip it and upload it to the website root directory, then modify the email information in send.php to test sending mail)
One: Front-end form
<html>
<body>
<h3>phpmailer Unit Test</h3>
Please enter the <font color="#FF6666">recipient</font> email address:
<form name="phpmailer" action="send.php" method="post">
<input type="hidden" name="submitted" value="1"/>
Email address: <input type="text" size="50" name="address" />
<br/>
<input type="submit" value="Send"/>
</form>
</body>
</html>
Two: Back-end PHP program
<?php
require("class.phpmailer.php"); //The downloaded file must be placed in the directory where this file is located
$mail = new PHPMailer(); //Create mail sending class
$address = $_POST['address'];
$mail->IsSMTP(); // Send using SMTP
$mail->Host = "mail.xxxxx.com"; // Your enterprise mail domain
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = "user@xxxx.com"; // Mail username (please fill in the complete email address)
$mail->Password = "******"; // Mail password
$mail->From = "user@xxxx.com"; //Sender email address
$mail->FromName = "Your Name";
$mail->AddAddress("$address", "");//Recipient address, can be replaced with any email mailbox that you want to receive mail, 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 subject
$mail->Body = "Hello, this is a test email"; //Email body
$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //Additional information, can be omitted
if(!$mail->Send())
{
echo "Email sending failed. <p>";
echo "Error reason: " . $mail->ErrorInfo;
exit;
}
echo "Email sent successfully";
?>
Reference URL: http://blog.51yip.com/php/910.html/comment-page-1
```