/** * Miscellaneous calls to improve test coverage and some small tests. */ public function testMiscellaneous() { $this->assertEquals('application/pdf', PHPMailer::_mime_types('pdf'), 'MIME TYPE lookup failed'); $this->Mail->addCustomHeader('SomeHeader: Some Value'); $this->Mail->clearCustomHeaders(); $this->Mail->clearAttachments(); $this->Mail->isHTML(false); $this->Mail->isSMTP(); $this->Mail->isMail(); $this->Mail->isSendmail(); $this->Mail->isQmail(); $this->Mail->setLanguage('fr'); $this->Mail->Sender = ''; $this->Mail->createHeader(); $this->assertFalse($this->Mail->set('x', 'y'), 'Invalid property set succeeded'); $this->assertTrue($this->Mail->set('Timeout', 11), 'Valid property set failed'); $this->assertTrue($this->Mail->set('AllowEmpty', null), 'Null property set failed'); $this->assertTrue($this->Mail->set('AllowEmpty', false), 'Valid property set of null property failed'); //Test pathinfo $a = '/mnt/files/飛兒樂 團光茫.mp3'; $q = PHPMailer::mb_pathinfo($a); $this->assertEquals($q['dirname'], '/mnt/files', 'UNIX dirname not matched'); $this->assertEquals($q['basename'], '飛兒樂 團光茫.mp3', 'UNIX basename not matched'); $this->assertEquals($q['extension'], 'mp3', 'UNIX extension not matched'); $this->assertEquals($q['filename'], '飛兒樂 團光茫', 'UNIX filename not matched'); $this->assertEquals(PHPMailer::mb_pathinfo($a, PATHINFO_DIRNAME), '/mnt/files', 'Dirname path element not matched'); $this->assertEquals(PHPMailer::mb_pathinfo($a, PATHINFO_BASENAME), '飛兒樂 團光茫.mp3', 'Basename path element not matched'); $this->assertEquals(PHPMailer::mb_pathinfo($a, 'filename'), '飛兒樂 團光茫', 'Filename path element not matched'); $a = 'c:\\mnt\\files\\飛兒樂 團光茫.mp3'; $q = PHPMailer::mb_pathinfo($a); $this->assertEquals($q['dirname'], 'c:\\mnt\\files', 'Windows dirname not matched'); $this->assertEquals($q['basename'], '飛兒樂 團光茫.mp3', 'Windows basename not matched'); $this->assertEquals($q['extension'], 'mp3', 'Windows extension not matched'); $this->assertEquals($q['filename'], '飛兒樂 團光茫', 'Windows filename not matched'); $this->assertEquals(PHPMailer::filenameToType('abc.jpg?xyz=1'), 'image/jpeg', 'Query string not ignored in filename'); $this->assertEquals(PHPMailer::filenameToType('abc.xyzpdq'), 'application/octet-stream', 'Default MIME type not applied to unknown extension'); //Line break normalization $eol = $this->Mail->LE; $b1 = "1\r2\r3\r"; $b2 = "1\n2\n3\n"; $b3 = "1\r\n2\r3\n"; $this->Mail->LE = "\n"; $t1 = "1{$this->Mail->LE}2{$this->Mail->LE}3{$this->Mail->LE}"; $this->assertEquals($this->Mail->fixEOL($b1), $t1, 'Failed to normalize line breaks (1)'); $this->assertEquals($this->Mail->fixEOL($b2), $t1, 'Failed to normalize line breaks (2)'); $this->assertEquals($this->Mail->fixEOL($b3), $t1, 'Failed to normalize line breaks (3)'); $this->Mail->LE = "\r\n"; $t1 = "1{$this->Mail->LE}2{$this->Mail->LE}3{$this->Mail->LE}"; $this->assertEquals($this->Mail->fixEOL($b1), $t1, 'Failed to normalize line breaks (4)'); $this->assertEquals($this->Mail->fixEOL($b2), $t1, 'Failed to normalize line breaks (5)'); $this->assertEquals($this->Mail->fixEOL($b3), $t1, 'Failed to normalize line breaks (6)'); $this->Mail->LE = $eol; }
/** * Send an e-mail with optional file attachment * * @param string $subject e-mail subject * @param array $recipients List of recipients email addresses, of the form * array([ [to|cc|bcc' =>] array( ... )]... ) * @param string $body Mail body template (one of the MAIL_BODY_xxx constants) * if no matching file is found in DATA directory, body will be empty * @param array $files List of files to attach (optional). Must be the full path. * @param string $from Sender's email address (optional). A display name may * be added after the email address, separated by a comma, * e.g. 'user@domain.tld,Firstname Lastname' * * @global Getopt $opt Command-line options */ function send_email($subject, array $recipients, $body, array $files = null, $from = null) { global $mailer, $smtp_cfg; global $opt, $mail_debug_addresses; $mail = new PHPMailer(); $mail->CharSet = 'UTF-8'; // Setup mailer type switch ($mailer) { case 'smtp': $mail->isSMTP(); $mail->Host = $smtp_cfg['host']; $mail->Port = $smtp_cfg['port']; $mail->SMTPAuth = true; $mail->Username = $smtp_cfg['username']; $mail->Password = $smtp_cfg['password']; break; case 'mail': default: $mail->isMail(); break; } $from = explode(',', $from . ','); $mail->setFrom($from[0], trim($from[1])); $mail->Subject = $subject; $body_template = DIR_DATA . "mail_body_{$body}.txt"; if (is_file($body_template)) { $mail->Body = file_get_contents($body_template); } else { $mail->Body = ' '; } // Recipients if ($opt['debug']) { foreach ($mail_debug_addresses as $addr) { $mail->addAddress($addr); } $eol = PHP_EOL . PHP_EOL; $text = str_repeat('-', 80) . $eol . 'DEBUG MODE - DESTINATAIRES ' . $eol; foreach ($recipients as $type => $list) { $text .= sprintf('%-4s %s' . $eol, ucfirst(is_numeric($type) ? 'To' : $type) . ':', empty($list) ? '-' : implode(', ', $list)); } $text .= str_repeat('-', 80) . $eol; $mail->Body = $text . $mail->Body; } else { foreach ($recipients as $type => $list) { switch ((string) $type) { case 'cc': case 'bcc': $addRecipient = 'add' . strtoupper($type); break; case 'to': default: $type = 'to'; $addRecipient = 'addAddress'; } if (!is_array($list) && !empty($list)) { $list = array($type => $list); } foreach ($list as $addr) { $mail->{$addRecipient}($addr); } } } $mail->Body = $mail->fixEOL($mail->Body); // Attach the files foreach ($files as $filename) { $mail->addAttachment($filename); } // Send email if (!$mail->send()) { exit_error("échec de l'envoi du message: " . $mail->ErrorInfo); } else { echo "Message envoyé\n"; } }