Example of using phpmailer to send emails

Source: Internet
Author: User
Example of using phpmailer to send emails

  1. Require_once (dirname (_ FILE _). "/../PHPMailer/class. phpmailer. php ");

  2. // Contains class. phpmailer. php

  3. /**
  4. * @ Param string $ send_to_mail target email
  5. * @ Param stinrg $ subject topic
  6. * @ Param string $ body content
  7. * @ Param string $ extra_hdrs additional information
  8. * @ Param string $ username recipient
  9. * @ Param string $ replyname reply
  10. * @ Param string $ replymail reply address
  11. * @ Return array (bealoon, string) the returned array contains two elements. bealoon indicates whether the operation is successful. string indicates the prompt information.
  12. */
  13. Function SendMail ($ send_to_mail, $ subject, $ body, $ extra_hdrs, $ username, $ replyname = "reply", $ replymail = "reply@reply.com "){
  14. $ Mail = new PHPMailer ();
  15. $ Mail-> IsSMTP (); // mail sending method
  16. $ Mail-> Host = "smtp.host.com"; // SMTP server Host address
  17. $ Mail-> SMTPAuth = true; // whether it is a trusted SMTP
  18. $ Mail-> Username = "reply@reply.com"; // SMTP user name Note: normal mail authentication does not need to add @ domain name
  19. $ Mail-> Password = "******"; // SMTP user Password
  20. $ Mail-> From = "send@send.com"; // The sender's email address
  21. $ Mail-> FromName = "send"; // sender
  22. $ Mail-> CharSet = "GB2312"; // specify the character set
  23. $ Mail-> Encoding = "base64 ";
  24. $ Mail-> AddAddress ($ send_to_mail, $ username); // add the target sending address
  25. $ Mail-> AddReplyTo ($ replymail, $ replyname); // add a reply address
  26. $ Mail-> IsHTML (true); // The mail type is in HTML format.
  27. $ Mail-> Subject = $ subject; // mail Subject
  28. // Email content
  29. $ Mail-> Body ="
  30. ". $ Body ."
  31. ";
  32. $ Mail-> AltBody = "text/html"; // content text format
  33. If (@! $ Mail-> Send ()){
  34. $ Results = array ("result" => false, "message" => $ mail-> ErrorInfo );
  35. Return $ results;
  36. } Else {
  37. $ Results = array ("result" => true, "message" => "the email has been sent to {$ send_to_mail }! ");
  38. Return $ results;
  39. }
  40. }

  41. $ Send_mail = SendMail ($ to, $ subject, $ content, $ headers, $ name );

  42. If ($ send_mail ["result"]) {
  43. Echo $ send_mail ["message"];
  44. } Else {
  45. Echo $ send_mail ["message"];
  46. }
  47. Exit ();
  48. ?>

Example 2:

  1. Include ('class/class. phpmailer. php ');

  2. $ Config = array (

  3. 'Host' => 'smtp .163.com ',
  4. 'Port' => '25 ',
  5. 'User' => '***',
  6. 'Passwd' => '****',
  7. 'From' => 'juva _ zz@163.com ',
  8. 'Fromname' => 'zhengzhou ',

  9. );

  10. $ Subject = 'This is a test mail ';
  11. $ Body ='
    Test Content
    This is content
    ';
  12. $ Address = '1970 @ qq.com ';
  13. $ Username = 'I ';

  14. $ Mail = new PHPMailer ();

  15. $ Mail-> CharSet = 'gb2312 ';
  16. $ Mail-> IsSMTP ();
  17. $ Mail-> Host = $ config ['host'];
  18. $ Mail-> Port = $ config ['port'];

  19. $ Mail-> From = $ config ['from'];

  20. $ Mail-> FromName = $ config ['fromname'];
  21. $ Mail-> SMTPAuth = true;

  22. $ Mail-> Username = $ config ['user'];

  23. $ Mail-> Password = $ config ['passwd'];

  24. $ Mail-> Subject = $ subject;

  25. $ Mail-> AltBody = "text/html ";
  26. $ Mail-> MsgHTML ($ body );

  27. $ Mail-> AddAddress ($ address, $ username );

  28. If (! $ Mail-> Send ())

  29. {
  30. Echo "Mail Error:". $ mail-> ErrorInfo;
  31. } Else
  32. {
  33. Echo "Congratulations! ";
  34. }

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.