Example of using phpMailer to send mails in php

Source: Internet
Author: User
Tags word wrap
Example of using phpMailer to send mails in php

  1. Require ("phpmailer/class. phpmailer. php ");
  2. Function smtp_mail ($ sendto_email, $ subject, $ body, $ extra_hdrs, $ user_name ){
  3. $ Mail = new PHPMailer ();
  4. $ Mail-> IsSMTP (); // send via SMTP
  5. $ Mail-> Host = "smtp.163.com"; // SMTP servers
  6. $ Mail-> SMTPAuth = true; // turn on SMTP authentication
  7. $ Mail-> Username = "xuchao842363331"; // SMTP username note: for normal mail authentication, @ domain name is not required. here is my 163 mailbox.
  8. $ Mail-> Password = "password"; // enter the email password here
  9. $ Mail-> From = "jbxue123@163.com"; // sender's mailbox
  10. $ Mail-> FromName = "administrator"; // sender
  11. $ Mail-> CharSet = "UTF-8"; // specify the character set here! After the UTF-8 is specified, the Mail title and sender will not be garbled, and if it is GB2312 title will be garbled
  12. $ Mail-> Encoding = "base64 ";
  13. $ Mail-> AddAddress ($ sendto_email, "username"); // recipient's email address and name
  14. $ Mail-> AddReplyTo ("yourmail@yourdomain.com", "yourdomain.com ");
  15. // $ Mail-> WordWrap = 50; // set word wrap
  16. // $ Mail-> AddAttachment ("/var/tmp/file.tar.gz"); // attachment
  17. // $ Mail-> AddAttachment ("/tmp/image.jpg", "new.jpg ");
  18. // $ Mail-> IsHTML (true); // send as HTML
  19. // Email subject
  20. $ Mail-> Subject = $ subject;
  21. // Email content
  22. $ Mail-> Body = "hello! PHPMailer ";
  23. // $ Mail-> AltBody = "text/html ";
  24. If (! $ Mail-> Send ())
  25. {
  26. Echo "error

    ";

  27. Echo "error:". $ mail-> ErrorInfo;
  28. Exit;
  29. }
  30. Else {
  31. Echo "success! ";
  32. }
  33. }
  34. // Parameter description (sent to, subject, content, additional information, user name)
  35. ?>

(When the character set is specified as GB2312 title will be garbled, here designated as UTF-8 will not appear garbled. In fact, PHPMailer has many other functions, such as adding attachments. we will not describe it here.

This function can be called when the Mail function is required:

  1. Require ("mail. php ");
  2. Smtp_mail ("790896688@qq.com", "Welcome to the programmer's House", "username ");
  3. ?>

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.