dokeos
[ class tree: dokeos ] [ index: dokeos ] [ all elements ]

Source for file phpmailer_test.php

Documentation is available at phpmailer_test.php

  1. <?php
  2. /*******************
  3.   Unit Test
  4.   Type: phpmailer class
  5. ********************/
  6.  
  7. require("phpunit.php");
  8. require("../class.phpmailer.php");
  9.  
  10. /**
  11.  * Performs authentication tests
  12.  */
  13. class phpmailerTest extends TestCase
  14. {
  15.     /**
  16.      * Holds the default phpmailer instance.
  17.      * @private
  18.      * @type object
  19.      */
  20.     var $Mail = false;
  21.  
  22.     /**
  23.      * Holds the SMTP mail host.
  24.      * @public
  25.      * @type string
  26.      */
  27.     var $Host = "";
  28.     
  29.     /**
  30.      * Holds the change log.
  31.      * @private
  32.      * @type string array
  33.      */
  34.     var $ChangeLog = array();
  35.     
  36.      /**
  37.      * Holds the note log.
  38.      * @private
  39.      * @type string array
  40.      */
  41.     var $NoteLog = array();   
  42.  
  43.     /**
  44.      * Class constuctor.
  45.      */
  46.     function phpmailerTest($name{
  47.         /* must define this constructor */
  48.         $this->TestCase$name );
  49.     }
  50.     
  51.     /**
  52.      * Run before each test is started.
  53.      */
  54.     function setUp({
  55.         global $global_vars;
  56.         global $INCLUDE_DIR;
  57.  
  58.         $this->Mail = new PHPMailer();
  59.  
  60.         $this->Mail->Priority 3;
  61.         $this->Mail->Encoding "8bit";
  62.         $this->Mail->CharSet "iso-8859-1";
  63.         $this->Mail->From "Frederik PHPMAILER";
  64.         $this->Mail->FromName "Frederik PHP";
  65.         $this->Mail->Sender "";
  66.         $this->Mail->Subject "AMAI";
  67.         $this->Mail->Body "";
  68.         $this->Mail->AltBody "";
  69.         $this->Mail->WordWrap 0;
  70.         $this->Mail->Host $global_vars["mail_host"];
  71.         $this->Mail->Port 25;
  72.         $this->Mail->Helo "localhost.localdomain";
  73.         $this->Mail->SMTPAuth false;
  74.         $this->Mail->Username "";
  75.         $this->Mail->Password "";
  76.         $this->Mail->PluginDir $INCLUDE_DIR;
  77.         $this->Mail->AddReplyTo("no_reply@derakkers.be""NIET REPLYEN");
  78.         $this->Mail->Sender "nobody@example.com";
  79.  
  80.         if(strlen($this->Mail->Host0)
  81.             $this->Mail->Mailer "smtp";
  82.         else
  83.         {
  84.             $this->Mail->Mailer "mail";
  85.             $this->Sender "unit_test@phpmailer.sf.net";
  86.         }
  87.         
  88.         global $global_vars;
  89.         $this->SetAddress($global_vars["mail_to"]"Test User");
  90.         if(strlen($global_vars["mail_cc"]0)
  91.             $this->SetAddress($global_vars["mail_cc"]"Carbon User""cc");
  92.     }     
  93.  
  94.     /**
  95.      * Run after each test is completed.
  96.      */
  97.     function tearDown({
  98.         // Clean global variables
  99.         $this->Mail = NULL;
  100.         $this->ChangeLog = array();
  101.         $this->NoteLog = array();
  102.     }
  103.  
  104.  
  105.     /**
  106.      * Build the body of the message in the appropriate format.
  107.      * @private
  108.      * @returns void
  109.      */
  110.     function BuildBody({
  111.         $this->CheckChanges();
  112.         
  113.         // Determine line endings for message        
  114.         if($this->Mail->ContentType == "text/html" || strlen($this->Mail->AltBody0)
  115.         {
  116.             $eol "<br/>";
  117.             $bullet "<li>";
  118.             $bullet_start "<ul>";
  119.             $bullet_end "</ul>";
  120.         }
  121.         else
  122.         {
  123.             $eol "\n";
  124.             $bullet " - ";
  125.             $bullet_start "";
  126.             $bullet_end "";
  127.         }
  128.         
  129.         $ReportBody "";
  130.         
  131.         $ReportBody .= "---------------------" $eol;
  132.         $ReportBody .= "Unit Test Frederik   " $eol;
  133.         $ReportBody .= "---------------------" $eol;
  134.         $ReportBody .= "phpmailer version: " $this->Mail->Version $eol;
  135.         $ReportBody .= "Content Type: " $this->Mail->ContentType $eol;
  136.         
  137.         if(strlen($this->Mail->Host0)
  138.             $ReportBody .= "Host: " $this->Mail->Host $eol;
  139.         
  140.         // If attachments then create an attachment list
  141.         if(count($this->Mail->attachment0)
  142.         {
  143.             $ReportBody .= "Attachments:" $eol;
  144.             $ReportBody .= $bullet_start;
  145.             for($i 0$i count($this->Mail->attachment)$i++)
  146.             {
  147.                 $ReportBody .= $bullet "Name: " $this->Mail->attachment[$i][1", ";
  148.                 $ReportBody .= "Encoding: " $this->Mail->attachment[$i][3", ";
  149.                 $ReportBody .= "Type: " $this->Mail->attachment[$i][4$eol;
  150.             }
  151.             $ReportBody .= $bullet_end $eol;
  152.         }
  153.         
  154.         // If there are changes then list them
  155.         if(count($this->ChangeLog0)
  156.         {
  157.             $ReportBody .= "Changes" $eol;
  158.             $ReportBody .= "-------" $eol;
  159.  
  160.             $ReportBody .= $bullet_start;
  161.             for($i 0$i count($this->ChangeLog)$i++)
  162.             {
  163.                 $ReportBody .= $bullet $this->ChangeLog[$i][0" was changed to [" 
  164.                                $this->ChangeLog[$i][1"]" $eol;
  165.             }
  166.             $ReportBody .= $bullet_end $eol $eol;
  167.         }
  168.         
  169.         // If there are notes then list them
  170.         if(count($this->NoteLog0)
  171.         {
  172.             $ReportBody .= "Notes" $eol;
  173.             $ReportBody .= "-----" $eol;
  174.  
  175.             $ReportBody .= $bullet_start;
  176.             for($i 0$i count($this->NoteLog)$i++)
  177.             {
  178.                 $ReportBody .= $bullet $this->NoteLog[$i$eol;
  179.             }
  180.             $ReportBody .= $bullet_end;
  181.         }
  182.         
  183.         // Re-attach the original body
  184.         $this->Mail->Body .= $eol $eol $ReportBody;
  185.     }
  186.     
  187.     /**
  188.      * Check which default settings have been changed for the report.
  189.      * @private
  190.      * @returns void
  191.      */
  192.     function CheckChanges({
  193.         if($this->Mail->Priority != 3)
  194.             $this->AddChange("Priority"$this->Mail->Priority);
  195.         if($this->Mail->Encoding != "8bit")
  196.             $this->AddChange("Encoding"$this->Mail->Encoding);
  197.         if($this->Mail->CharSet != "iso-8859-1")
  198.             $this->AddChange("CharSet"$this->Mail->CharSet);
  199.         if($this->Mail->Sender != "")
  200.             $this->AddChange("Sender"$this->Mail->Sender);
  201.         if($this->Mail->WordWrap != 0)
  202.             $this->AddChange("WordWrap"$this->Mail->WordWrap);
  203.         if($this->Mail->Mailer != "mail")
  204.             $this->AddChange("Mailer"$this->Mail->Mailer);
  205.         if($this->Mail->Port != 25)
  206.             $this->AddChange("Port"$this->Mail->Port);
  207.         if($this->Mail->Helo != "localhost.localdomain")
  208.             $this->AddChange("Helo"$this->Mail->Helo);
  209.         if($this->Mail->SMTPAuth)
  210.             $this->AddChange("SMTPAuth""true");
  211.     }
  212.     
  213.     /**
  214.      * Adds a change entry.
  215.      * @private
  216.      * @returns void
  217.      */
  218.     function AddChange($sName$sNewValue{
  219.         $cur count($this->ChangeLog);
  220.         $this->ChangeLog[$cur][0$sName;
  221.         $this->ChangeLog[$cur][1$sNewValue;
  222.     }
  223.     
  224.     /**
  225.      * Adds a simple note to the message.
  226.      * @public
  227.      * @returns void
  228.      */
  229.     function AddNote($sValue{
  230.         $this->NoteLog[$sValue;
  231.     }
  232.  
  233.     /**
  234.      * Adds all of the addresses
  235.      * @public
  236.      * @returns void
  237.      */
  238.     function SetAddress($sAddress$sName ""$sType "to"{
  239.         switch($sType)
  240.         {
  241.             case "to":
  242.                 $this->Mail->AddAddress($sAddress$sName);
  243.                 break;
  244.             case "cc":
  245.                 $this->Mail->AddCC($sAddress$sName);
  246.                 break;
  247.             case "bcc":
  248.                 $this->Mail->AddBCC($sAddress$sName);
  249.                 break;
  250.         }
  251.     }
  252.  
  253.     /////////////////////////////////////////////////
  254.     // UNIT TESTS
  255.     /////////////////////////////////////////////////
  256.  
  257.     /**
  258.      * Try a plain message.
  259.      */
  260.     function test_WordWrap({
  261.  
  262.         $this->Mail->WordWrap 40;
  263.         $my_body "Here is the main body of this message.  It should " .
  264.                    "be quite a few lines.  It should be wrapped at the " .
  265.                    "40 characters.  Make sure that it is.";
  266.         $nBodyLen strlen($my_body);
  267.         $my_body .= "\n\nThis is the above body length: " $nBodyLen;
  268.  
  269.         $this->Mail->Body $my_body;
  270.         $this->Mail->Subject .= ": Wordwrap";
  271.  
  272.         $this->BuildBody();
  273.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  274.     }
  275.  
  276.     /**
  277.      * Try a plain message.
  278.      */
  279.     function test_Low_Priority({
  280.     
  281.         $this->Mail->Priority 5;
  282.         $this->Mail->Body "Here is the main body.  There should be " .
  283.                             "a reply to address in this message.";
  284.         $this->Mail->Subject .= ": Low Priority";
  285.         $this->Mail->AddReplyTo("nobody@nobody.com""Nobody (Unit Test)");
  286.  
  287.         $this->BuildBody();
  288.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  289.     }
  290.  
  291.     /**
  292.      * Simple plain file attachment test.
  293.      */
  294.     function test_Multiple_Plain_FileAttachment({
  295.  
  296.         $this->Mail->Body "Here is the text body";
  297.         $this->Mail->Subject .= ": Plain + Multiple FileAttachments";
  298.  
  299.         if(!$this->Mail->AddAttachment("rocks.png"))
  300.         {
  301.             $this->assert(false$this->Mail->ErrorInfo);
  302.             return;
  303.         }
  304.  
  305.         if(!$this->Mail->AddAttachment("phpmailer_test.php""test.txt"))
  306.         {
  307.             $this->assert(false$this->Mail->ErrorInfo);
  308.             return;
  309.         }
  310.  
  311.         $this->BuildBody();
  312.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  313.     }
  314.  
  315.     /**
  316.      * Simple plain string attachment test.
  317.      */
  318.     function test_Plain_StringAttachment({
  319.  
  320.         $this->Mail->Body "Here is the text body";
  321.         $this->Mail->Subject .= ": Plain + StringAttachment";
  322.         
  323.         $sAttachment "These characters are the content of the " .
  324.                        "string attachment.\nThis might be taken from a ".
  325.                        "database or some other such thing. ";
  326.         
  327.         $this->Mail->AddStringAttachment($sAttachment"string_attach.txt");
  328.  
  329.         $this->BuildBody();
  330.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  331.     }
  332.  
  333.     /**
  334.      * Plain quoted-printable message.
  335.      */
  336.     function test_Quoted_Printable({
  337.  
  338.         $this->Mail->Body "Here is the main body";
  339.         $this->Mail->Subject .= ": Plain + Quoted-printable";
  340.         $this->Mail->Encoding "quoted-printable";
  341.  
  342.         $this->BuildBody();
  343.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  344.     }
  345.  
  346.     /**
  347.      * Try a plain message.
  348.      */
  349.     function test_Html({
  350.     
  351.         $this->Mail->IsHTML(true);
  352.         $this->Mail->Subject .= ": HTML only";
  353.         
  354.         $this->Mail->Body "This is a <b>test message</b> written in HTML. </br>" .
  355.                             "Go to <a href=\"http://phpmailer.sourceforge.net/\">" .
  356.                             "http://phpmailer.sourceforge.net/</a> for new versions of " .
  357.                             "phpmailer.  <p/> Thank you!";
  358.  
  359.         $this->BuildBody();
  360.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  361.     }
  362.  
  363.     /**
  364.      * Simple HTML and attachment test
  365.      */
  366.     function test_HTML_Attachment({
  367.  
  368.         $this->Mail->Body "This is the <b>HTML</b> part of the email.";
  369.         $this->Mail->Subject .= ": HTML + Attachment";
  370.         $this->Mail->IsHTML(true);
  371.         
  372.         if(!$this->Mail->AddAttachment("phpmailer_test.php""test_attach.txt"))
  373.         {
  374.             $this->assert(false$this->Mail->ErrorInfo);
  375.             return;
  376.         }
  377.  
  378.         $this->BuildBody();
  379.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  380.     }
  381.  
  382.     /**
  383.      * An embedded attachment test.
  384.      */
  385.     function test_Embedded_Image({
  386.  
  387.         $this->Mail->Body "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
  388.                      "Here is an image!</a>";
  389.         $this->Mail->Subject .= ": Embedded Image";
  390.         $this->Mail->IsHTML(true);
  391.         
  392.         if(!$this->Mail->AddEmbeddedImage("rocks.png""my-attach""rocks.png",
  393.                                           "base64""image/png"))
  394.         {
  395.             $this->assert(false$this->Mail->ErrorInfo);
  396.             return;
  397.         }
  398.  
  399.         $this->BuildBody();
  400.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  401.     }
  402.  
  403.     /**
  404.      * An embedded attachment test.
  405.      */
  406.     function test_Multi_Embedded_Image({
  407.  
  408.         $this->Mail->Body "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
  409.                      "Here is an image!</a>";
  410.         $this->Mail->Subject .= ": Embedded Image + Attachment";
  411.         $this->Mail->IsHTML(true);
  412.         
  413.         if(!$this->Mail->AddEmbeddedImage("rocks.png""my-attach""rocks.png",
  414.                                           "base64""image/png"))
  415.         {
  416.             $this->assert(false$this->Mail->ErrorInfo);
  417.             return;
  418.         }
  419.  
  420.         if(!$this->Mail->AddAttachment("phpmailer_test.php""test.txt"))
  421.         {
  422.             $this->assert(false$this->Mail->ErrorInfo);
  423.             return;
  424.         }
  425.         
  426.         $this->BuildBody();
  427.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  428.     }
  429.  
  430.     /**
  431.      * Simple multipart/alternative test.
  432.      */
  433.     function test_AltBody({
  434.  
  435.         $this->Mail->Body "This is the <b>HTML</b> part of the email.";
  436.         $this->Mail->AltBody "Here is the text body of this message.  " .
  437.                    "It should be quite a few lines.  It should be wrapped at the " .
  438.                    "40 characters.  Make sure that it is.";
  439.         $this->Mail->WordWrap 40;
  440.         $this->AddNote("This is a mulipart alternative email");
  441.         $this->Mail->Subject .= ": AltBody + Word Wrap";
  442.  
  443.         $this->BuildBody();
  444.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  445.     }
  446.  
  447.     /**
  448.      * Simple HTML and attachment test
  449.      */
  450.     function test_AltBody_Attachment({
  451.  
  452.         $this->Mail->Body "This is the <b>HTML</b> part of the email.";
  453.         $this->Mail->AltBody "This is the text part of the email.";
  454.         $this->Mail->Subject .= ": AltBody + Attachment";
  455.         $this->Mail->IsHTML(true);
  456.         
  457.         if(!$this->Mail->AddAttachment("phpmailer_test.php""test_attach.txt"))
  458.         {
  459.             $this->assert(false$this->Mail->ErrorInfo);
  460.             return;
  461.         }
  462.  
  463.         $this->BuildBody();
  464.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  465.  
  466.         $fp fopen("message.txt""w");
  467.         fwrite($fp$this->Mail->CreateHeader($this->Mail->CreateBody());
  468.         fclose($fp);
  469.     }    
  470.  
  471.     function test_MultipleSend({
  472.         $this->Mail->Body "Sending two messages without keepalive";
  473.         $this->BuildBody();
  474.         $subject $this->Mail->Subject;
  475.  
  476.         $this->Mail->Subject $subject ": SMTP 1";
  477.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  478.         
  479.         $this->Mail->Subject $subject ": SMTP 2";
  480.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  481.     }
  482.  
  483.     function test_SmtpKeepAlive({
  484.         $this->Mail->Body "This was done using the SMTP keep-alive.";
  485.         $this->BuildBody();
  486.         $subject $this->Mail->Subject;
  487.  
  488.         $this->Mail->SMTPKeepAlive true;
  489.         $this->Mail->Subject $subject ": SMTP keep-alive 1";
  490.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  491.         
  492.         $this->Mail->Subject $subject ": SMTP keep-alive 2";
  493.         $this->assert($this->Mail->Send()$this->Mail->ErrorInfo);
  494.         $this->Mail->SmtpClose();
  495.     }
  496.     
  497.     function test_Error({
  498.         $this->Mail->Subject .= ": This should be sent"
  499.         $this->BuildBody();
  500.         $this->Mail->ClearAllRecipients()// no addresses should cause an error
  501.         $this->assert($this->Mail->IsError(== false"Error found");
  502.         $this->assert($this->Mail->Send(== false"Send succeeded");
  503.         $this->assert($this->Mail->IsError()"No error found");
  504.         $this->assertEquals('You must provide at least one ' .
  505.                             'recipient email address.'$this->Mail->ErrorInfo);
  506.         $this->Mail->AddAddress(get("mail_to"));
  507.         $this->assert($this->Mail->Send()"Send failed");
  508.     }
  509. }  
  510.  
  511. /**
  512.  * Create and run test instance.
  513.  */
  514.  
  515. if(isset($HTTP_GET_VARS))
  516.     $global_vars $HTTP_GET_VARS;
  517. else
  518.     $global_vars $_REQUEST;
  519.  
  520. if(isset($global_vars["submitted"]))
  521. {
  522.     echo "Test results:<br>";
  523.     $suite new TestSuite"phpmailerTest" );
  524.     
  525.     $testRunner new TestRunner;
  526.     $testRunner->run($suite);
  527.     echo "<hr noshade/>";
  528. }
  529.  
  530. function get($sName{
  531.     global $global_vars;
  532.     if(isset($global_vars[$sName]))
  533.         return $global_vars[$sName];
  534.     else
  535.         return "";
  536. }
  537.  
  538. ?>
  539.  
  540. <html>
  541. <body>
  542. <h3>phpmailer Unit Test</h3>
  543. By entering a SMTP hostname it will automatically perform tests with SMTP.
  544.  
  545. <form name="phpmailer_unit" action="phpmailer_test.php" method="get">
  546. <input type="hidden" name="submitted" value="1"/>
  547. To Address: <input type="text" size="50" name="mail_to" value="<?php echo get("mail_to")?>"/>
  548. <br/>
  549. Cc Address: <input type="text" size="50" name="mail_cc" value="<?php echo get("mail_cc")?>"/>
  550. <br/>
  551. SMTP Hostname: <input type="text" size="50" name="mail_host" value="<?php echo get("mail_host")?>"/>
  552. <p/>
  553. <input type="submit" value="Run Test"/>
  554.  
  555. </form>
  556. </body>
  557. </html>

Documentation generated on Thu, 12 Jun 2008 09:25:49 -0500 by phpDocumentor 1.4.1