PHPÓʼþ·¢ËÍÀàPHPMailerÓ÷¨ÊµÀýÏê½â

5年以前  |  阅读数:763 次  |  编程语言:PHP 

±¾ÎÄʵÀý½²ÊoÁËPHPÓʼþ¢ËÍÀaPHPMailerÓ裬²¢Ïeϸ½²ÊoÁËÆa¾ßÌaµÄ²Ù×÷²½Öe¡£*ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼¡£¾ßÌa²½ÖeÈçÏ£º

1.ÔÚ*þÎñÆ÷°²×° sendmail


    sudo apt-get install sendmail 

2.Æo¶¯ sendmail


    sudo /etc/init.d/sendmail start 

3.ÐÞ¸Ä php.ini


    [mail function] 
    SMTP = localhost 
    smtp_port = 25 
    sendmail_from = me@example.com 

4.Function sendMailº¯ÊýÈçÏÂ


    <?php 
    /* µ÷ÓÃPHPMailer*¢Ë͵çÓÊ 
    * @param String $receiver   ÊÕ¼þÈË 
    * @param String $sender    *¢¼þÈË 
    * @param String $sender_name *¢¼þÈËÃu³ÆÈçΪ¿ÕÔoÓÃ*¢¼þÈ˵ØÖ*´uÌae 
    * @param String $subject   ÓʼþÖ÷Ìa 
    * @param String $content   ÓʼþÄÚÈÝ 
    * @param boolean $ishtml    ÊÇ*ñhtmlµçÓÊ 
    * @param Array  $attachements ¸½¼þ 
    * @return boolean 
    */ 
    function sendMail($receiver, $sender, $sender_name, $subject, $content, $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*¢ËÍ  
      //$mail->Host = "smtp.gmail.com"; // SMTP *þÎñÆ÷ 
      //$mail->Port = 465;       // SMTP ¶Ë¿Ú 
      //$mail->SMTPSecure = 'ssl';   // ¼ÓÃÜ*½Ê½ 
      //$mail->SMTPAuth = true;     // ´o¿ªSMTPÈÏÖ¤ 
      //$mail->Username = "username";  // Óû§Ãu 
      //$mail->Password = "password";  // ÃÜÂe 

      $mail->IsMail();         // using PHP mail() function ÓпÉÄÜ•þ³o¬Fß@*aa]¼þ¿ÉÄܲ»ÊÇÓÉÒÔÏÂʹÓÃÕßËu‚÷Ë͵ÄÌaʾ 

      $mail->From = $sender;      // *¢ÐÅÈË  
      $mail->FromName = $sender_name;  // *¢ÐÅÈ˱ðÃu  
      $mail->AddReplyTo($sender);    // »Ø¸²ÈË 
      $mail->AddAddress($receiver);   // ÊÕÐÅÈË  

      // ÒÔhtml*½Ê½*¢ËÍ 
      if($ishtml){ 
        $mail->IsHTML(true); 
      } 

      // *¢Ë͸½¼þ 
      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){ 
                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; // Óʼþ±eÌa 
      $mail->Body   = $content; // ÓʼþƒÈÈÝ 
      return $mail->Send();  
    } 

    // DEMOʾÀýÈçÏ£º 
    $receiver = 'receiver@test.com'; 
    $sender = 'sender@test.com'; 
    $sender_name = 'sender name'; 
    $subject = 'subjecct'; 
    $content = 'content'; 

    // ËÄÖÖ¸ñʽ¶¼¿ÉÒÔ 
    $attachments = 'attachment1.jpg'; 
    $attachments = array('path'=>'attachment1.jpg', 'name'=>'¸½¼þ1.jpg'); 
    $attachments = array('attachment1.jpg','attachment2.jpg','attachment3.jpg'); 
    $attachments = array( 
      array('path'=>'attachment1.jpg', 'name'=>'¸½¼þ1.jpg'), 
      array('path'=>'attachment2.jpg', 'name'=>'¸½¼þ2.jpg'), 
      array('path'=>'attachment3.jpg', 'name'=>'¸½¼þ3.jpg'), 
    ); 
    $flag = sendMail($receiver, $sender, $sender_name, $subject, $content, true, $attachments); 
    echo $flag; 
    ?> 

Ô´Âeµa»÷´Ë´¦±¾Õ¾ÏÂÔØ¡£

Ï£Íu±¾ÎÄËuÊo¶Ô´o¼ÒPHP³ÌÐoÉe¼ÆµÄѧϰÓÐËu°iÖu¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
贪吃蛇
文件下载
获取IMEI
双位运算符
发送邮件
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
提取后缀名
年的日历图
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例