The following summarizes three implementation codes for sending mails in php, including phpmailer, mail (), and JMail (). If you need them, refer to them. Use the phpmail function to send emails using the mail () function... the following summarizes three implementation codes for sending mails in php, including phpmailer, mail (), and JMail (). If you need them, refer to them.
Use the php mail function to send
If you use the mail () function to send emails, you must have an SMTP server that does not need to be verified. in this way, the configuration will work a little more, but it is easier to use, the code is as follows:
$to = "test@163.com"; $subject = "Test"; $message = "This is a test mail!"; mail($to,$subject,$message);
JMail sends emails
The account must have the smtp sending permission. it is best to set $ jmail-> silent = true to prevent jmail from throwing error messages by itself. Common accounts include NetEase, sogou, QQ, and GMail mail. the code is as follows:
Silent = true; // block exception errors $ jmail-> charset = 'gb2312'; // Otherwise, Chinese characters will be garbled $ jmail-> From = 'web535000 @ 163.com '; // theoretically, it can not be the same as the sender address, but it cannot be used for several attempts with 163. you can change it to the same one. $ jmail-> FromName = 'botest '; $ jmail-> AddRecipient ('abc1 @ 163.com '); // you can add multiple email recipients. // $ jmail-> AddRecipient ('abc2 @ 163.com '); // $ jmail-> AddRecipient ('abc3 @ 163.com '); $ jmail-> Subject = 'Email test. '; $ jmail-> Body =' this is test email. '; $ jmail-> MailServerUserNam E = 'web535000 @ 163.com '; // email sending account $ jmail-> MailServerPassword = '2016 '; // try {$ email = $ jmail-> Send ('smtp .163.com '); if ($ email) echo 'sent successfully '; else echo 'sending failed';} catch (Exception $ e) {echo $ e-> getMessage () ;}?>
The mail account must have the smtp sending permission. for NetEase's mailbox, the new mailbox registered after 06 years does not seem to work. in the early stage, it is best to set $ jmail-> silent = true, jmail is not allowed to throw error messages by itself. when the setting is normal, some errors may occur when sending emails frequently. it is estimated that the sending interval is set on the sending server.
Phpmailer sends emails, which are simple and commonly used,The code is as follows:
CharSet = "UTF-8"; // Set the mail encoding, the default ISO-8859-1, this must be set if you send Chinese, otherwise garbled $ mail-> IsSMTP (); // Set the SMTP Service $ mail-> SMTPDebug = 1; // enable SMTP debugging // 1 = errors and messages // 2 = messages only $ mail-> SMTPAuth = true; // enable the SMTP authentication function $ mail-> SMTPSecure = "ssl"; // security protocol $ mail-> Host = "smtp.googlemail.com "; // SMTP server $ mail-> Port = 465; // SMTP server Port $ mail-> Username = "SMTP server user name "; // SMTP server username $ mail-> Password = "SMTP server Password "; // SMTP server password $ mail-> SetFrom ('sender address, for example, admin # jiucool.com # Replace with @ ', 'sender name '); $ mail-> AddReplyTo ("email reply address, such as admin # jiucool.com # changed to @", "email reply recipient name"); $ mail-> Subject = $ subject; $ mail-> AltBody = "To view the message, please use an HTML compatible email viewer! -From www.111cn.net "; // optional, comment out and test $ mail-> MsgHTML ($ body); $ address = $ to; $ mail-> AddAddress ($ address, "recipient name"); // $ mail-> AddAttachment ("images/phpmailer.gif"); // attachment // $ mail-> AddAttachment ("images/phpmailer_mini.gif "); // attachment if (! $ Mail-> Send () {echo "Mailer Error:". $ mail-> ErrorInfo;} else {echo "Message sent! Congratulations, the email is sent successfully! ";}}?>
Main features of PHPMailer:
1. supports mail s/mime-encrypted digital signatures
2. support multiple emails, such as TOs, CCs, BCCs, and REPLY-TOs.
3. you can work on any server platform, so you don't have to worry about the problem that Windows cannot send emails.
4. support text/HTML format emails
5. images can be embedded
6. support for HTML reading not supported by the Mail client
7. powerful Mail sending debugging function debug
8. customize the mail header
9. support for redundant SMTP servers
10. 8-bit, base64, binary, and quoted-printable encoding is supported.
11. text wrap
12. supports sending multiple attachments
13. support for SMTP server verification
14. tests on Sendmail, qmail, Postfix, Gmail, Imail, Exchange, and other platforms are successful.
15. the downloaded files include detailed instructions and examples, so you don't have to worry about getting started!
16. PHPMailer is very small, simple, convenient, and fast
Tutorial link:
Reprint at will ~ However, please keep the tutorial address★