Common sending methods in PHP

Source: Internet
Author: User
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:

  1. $ To = "test@163.com ";
  2. $ Subject = "Test ";
  3. $ Message = "This is a test mail! ";
  4. 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:

  1. $ Jmail = new COM ('jmail. message') or die ('unable to call the JMail components ');
  2. $ Jmail-> silent = true; // block exception errors
  3. $ Jmail-> charset = 'gb2312'; // Otherwise, Chinese characters will be garbled.
  4. $ 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.
  5. $ Jmail-> FromName = 'botest ';
  6. $ Jmail-> AddRecipient ('abc1 @ 163.com '); // you can add multiple email recipients.
  7. // $ Jmail-> AddRecipient ('abc2 @ 163.com ');
  8. // $ Jmail-> AddRecipient ('abc3 @ 163.com ');
  9. $ Jmail-> Subject = 'Email test .';
  10. $ Jmail-> Body = 'This is test email .';
  11. $ Jmail-> MailServerUserName = 'web535000 @ 163.com '; // mail account
  12. $ Jmail-> MailServerPassword = '000000'; // account password
  13. Try {
  14. $ Email = $ jmail-> Send ('smtp .163.com ');
  15. If ($ email) echo 'sent successfully ';
  16. Else echo 'sending failed ';
  17. } Catch (Exception $ e ){
  18. Echo $ e-> getMessage ();
  19. }
  20. ?>

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:

  1. Function postmail_jiucool_com ($ to, $ subject = "", $ body = ""){
  2. // Author: Jiucool WebSite: http://www.phpfensi.com
  3. // $ To indicates the recipient address $ subject indicates the Mail Title $ body indicates the mail body
  4. // Error_reporting (E_ALL );
  5. Error_reporting (E_STRICT );
  6. Date_default_timezone_set ("Asia/Shanghai"); // Set the time zone UTC + 8.
  7. Require_once ('class. phpmailer. php ');
  8. Include ("class. smtp. php ");
  9. $ Mail = new PHPMailer (); // A new PHPMailer object is displayed.
  10. $ Body = eregi_replace ("[]", '', $ body); // filter the email content.
  11. $ Mail-> CharSet = "UTF-8"; // Set the mail encoding, the default ISO-8859-1, this must be set if you send Chinese, otherwise garbled
  12. $ Mail-> IsSMTP (); // you can specify the SMTP service.
  13. $ Mail-> SMTPDebug = 1; // enable the SMTP debugging function
  14. // 1 = errors and messages
  15. // 2 = messages only
  16. $ Mail-> SMTPAuth = true; // enable SMTP verification
  17. $ Mail-> SMTPSecure = "ssl"; // security protocol
  18. $ Mail-> Host = "smtp.googlemail.com"; // SMTP server
  19. $ Mail-> Port = 465; // SMTP server Port number
  20. $ Mail-> Username = "SMTP server Username"; // SMTP server Username
  21. $ Mail-> Password = "SMTP server Password"; // SMTP server Password
  22. $ Mail-> SetFrom ('sender address, for example, admin # jiucool.com # Replace with @ ', 'sender name ');
  23. $ Mail-> AddReplyTo ("email reply address, such as admin # jiucool.com # changed to @", "email reply recipient name ");
  24. $ Mail-> Subject = $ subject;
  25. $ Mail-> AltBody = "To view the message, please use an HTML compatible email viewer! -From www.111cn.net "; // optional, comment out and test
  26. $ Mail-> MsgHTML ($ body );
  27. $ Address = $;
  28. $ Mail-> AddAddress ($ address, "recipient name ");
  29. // $ Mail-> AddAttachment ("images/phpmailer.gif"); // attachment
  30. // $ Mail-> AddAttachment ("images/phpmailer_mini.gif"); // attachment
  31. If (! $ Mail-> Send ()){
  32. Echo "Mailer Error:". $ mail-> ErrorInfo;
  33. } Else {
  34. Echo "Message sent! Congratulations, the email is sent successfully! ";
  35. }
  36. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.