Home >Backend Development >PHP Tutorial >Example of sending email using phpmailer

Example of sending email using phpmailer

WBOY
WBOYOriginal
2016-07-25 09:03:48950browse
  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 subject
  6. * @param string $body email content
  7. * @param string $extra_hdrs additional information
  8. * @param string $username recipient
  9. * @param string $replyname reply person
  10. * @param string $replymail reply address
  11. * @return array(bealoon,string) The returned array includes two elements, bealoon indicates whether it is successful, and string is the prompt message
  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(); //Email 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 username Note: Ordinary email authentication does not require adding @domain name
  19. $mail ->Password="******"; //SMTP user password
  20. $mail->From="send@send.com"; //Sender's email address
  21. $mail->FromName= "send"; //Sender
  22. $mail->CharSet="GB2312"; //Specify character set
  23. $mail->Encoding="base64";
  24. $mail->AddAddress($send_to_mail,$ username); //Add the sending destination address
  25. $mail->AddReplyTo($replymail,$replyname); //Add the reply address
  26. $mail->IsHTML(true); //The email type is HTML format
  27. $mail ->Subject=$subject; //Email 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. ?>

Copy Code

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 the content
    ';
  12. $address='379018082@qq.com';
  13. $username='me';

  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. < ;p>$mail->AddAddress($address,$username);

  28. if(!$mail->Send())

  29. {
  30. echo "Mail Error :".$ mail->ErrorInfo;
  31. }else
  32. {
  33. echo "Congratulations on successful sending! ";
  34. }

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn