The following summarizes three implementation codes for sending mails in php, including phpmailer, mail (), and JMail (). If you need them, refer to them. Use phpmail functions
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:
-
- $ Jmail = new COM ('jmail. message') or die ('unable to call the JMail components ');
- $ Jmail-> silent = true; // block exception errors
- $ Jmail-> charset = 'gb2312'; // Otherwise, Chinese characters will be garbled.
- $ Jmail-> From = 'web535000 @ 163.com '; // theoretically, it is not the same as the sender address, but it cannot be used several times after 163, just 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-> MailServerUserName = 'web535000 @ 163.com '; // mail account
- $ Jmail-> MailServerPassword = '000000'; // account password
- 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:
- Function postmail_jiucool_com ($ to, $ subject = "", $ body = ""){
- // Author: Jiucool WebSite: http://www.phpfensi.com
- // $ To indicates the recipient address $ subject indicates the Mail Title $ body indicates the mail body
- // Error_reporting (E_ALL );
- Error_reporting (E_STRICT );
- Date_default_timezone_set ("Asia/Shanghai"); // Set the time zone UTC + 8.
- Require_once ('class. phpmailer. php ');
- Include ("class. smtp. php ");
- $ Mail = new PHPMailer (); // A new PHPMailer object is displayed.
- $ Body = eregi_replace ("[]", '', $ body); // filter the email content.
- $ Mail-> CharSet = "UTF-8"; // Set the mail encoding, the default ISO-8859-1, this must be set if you send Chinese, otherwise garbled
- $ Mail-> IsSMTP (); // you can specify the SMTP service.
- $ Mail-> SMTPDebug = 1; // enable the SMTP debugging function
- // 1 = errors and messages
- // 2 = messages only
- $ Mail-> SMTPAuth = true; // enable SMTP verification
- $ Mail-> SMTPSecure = "ssl"; // security protocol
- $ Mail-> Host = "smtp.googlemail.com"; // SMTP server
- $ Mail-> Port = 465; // SMTP server Port number
- $ Mail-> Username = "SMTP server Username"; // 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 = $;
- $ 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