PHP Mail Send class Phpmailer usage example detailed

Source: Internet
Author: User
Tags foreach

The example of this article describes the Phpmailer usage of the PHP mail sending class, and details its detailed operation steps. Share to everyone for your reference. The specific steps are as follows:

1. Install SendMail on the server

1 sudo apt-get install SendMail

2. Start SendMail

1 Sudo/etc/init.d/sendmail start

3. Modify PHP.ini

1 2 3 4 [Mail Function] SMTP = localhost Smtp_port = Sendmail_from = me@example.com

The 4.Function SendMail function is as follows

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 <?php /* Call Phpmailer Send email   * @param string $receiver    recipient   * @param string $sender  &nbs p;  Sender   * @param string $sender _name sender's name if NULL replace   * @param String $subject    message subject   * @param String $content    message content   * @param boolean $ishtml     HTML email   * @param ARRAY&NB Sp $attachements Accessories   * @return boolean  */  function SendMail ($receiver, $sender, $sender _name, $subject, $co Ntent, $ishtml =true, $attachments =array ()) {    include_once "class-phpmailer.php";         if (Empty ($receiver) | | | empty ($sender) | | empty ($subject) | | empty ($content)) {      return false;    }           $mail New Phpmailer ();         //$mail->issmtp ();        //SMTP send      //$mail-&GT Host = "smtp.gmail.com"; SMTP server     //$mail->port = 465;      //SMTP port      $mail->smtpsecure = ' SSL ';  //encryption mode     //$mail->smtpauth = true;   & nbsp Open SMTP authentication     //$mail->username = "Username"; //username     //$mail->password = "Password"; //password          $mail->ismail ();      The    //using PHP mail () function may show that this email may not be prompted by the following users                 $mail->from = $sender;     //Senders        $mail->fromname = $sender _name; //Sender alias       $mail->addreplyto ($sender);    //reply      $mail->addaddress ($receiver);  //Recipient           //HTML to send      if ($ishtml) {       $mail->ishtml (True);    }         //Send attachment     if ($attachments) {      if Is_array ($ Attachments) {         $send _attachments = Array ();             $tmp _attachments = Array_slice ($attachments, 0,1);         if (!is_array (Array_pop ($tmp _attachments)) {           if (Isset ($attachments [' path ']) {            array_ Push ($send _attachments, $attachments);                     }else{             foreach ($attachments as $attachment) {              &nbsP;array_push ($send _attachments, Array (' path ' => $attachment));             }          }         }else{           $send _attachments = $attachments;         }            foreach ($send _ Attachments as $attachment) {           $attachment [' name '] = Isset ($ attachment[' name '])? $attachment [' name ']: null;           $attachment [' encoding '] = isset ($ attachment[' encoding '])? $attachment [' Encoding ']: ' base64 ';           $attachment [' type '] = Isset ($attachment [' type '])? $attachment [' type ']: ' Application/octet-stream ';          if (Isset ( $attachment [' path ']) && File_exists ($attachment [' path ']) {             $mail-> AddAttachment ($attachment [' Path '], $attachment [' name '], $attachment [' Encoding '], $attachment [' type ']);          }        }       }elseif (is_string ($attachments)) {        if (file_exists ($ Attachments) {           $mail->addattachment ($attachments);         }      }    }          $mail->subject = $subject; Mail title      $mail->body   = $content; Mail content     return $mail->send ();  }     //Demo example below:  $receiver = ' receiver@test.com ';  $sender = ' sender@test.com ';  $sender _name = ' sender name ';  $subject = ' Subjecct ';  $content = ' content ';     //Four forms can be   $attachments = ' attachment1.jpg '; nbsp $attachments = Array (' path ' => ' attachment1.jpg ', ' name ' => ' Attachment 1.jpg ');  $attachments = Array (' Attachment1.jpg ', ' attachment2.jpg ', ' attachment3.jpg ');  $attachments = Array (    array (' path ' = > ' attachment1.jpg ', ' name ' => ' Attachment 1.jpg '),    array (' path ' => ' attachment2.jpg ', ' name ' => ') Annex 2.jpg '),    array (' path ' => ' attachment3.jpg ', ' name ' => ' annex 3.jpg '), );  $flag = SendMail ($receiver, $sender, $sender _name, $subject, $content, True, $attachments);  echo $flag; ?>

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.