attachFile() public method

Attach a file to the mail object.
public attachFile ( string $file ) : Mail
$file string
return Mail
Example #1
0
 /**
  * Method to process the form
  *
  * @return self
  */
 public function process()
 {
     $fields = $this->getFields();
     $submission = new Table\FormSubmissions(['form_id' => $this->id, 'timestamp' => date('Y-m-d H:i:s'), 'ip_address' => $_SERVER['REMOTE_ADDR']]);
     $submission->save();
     unset($fields['csrf']);
     unset($fields['captcha']);
     unset($fields['id']);
     unset($fields['submit']);
     $files = [];
     if ($_FILES) {
         foreach ($_FILES as $key => $value) {
             if (isset($value['tmp_name']) && !empty($value['tmp_name']) && class_exists('Phire\\Fields\\Model\\Field')) {
                 $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files');
                 $filename = $upload->checkFilename($value['name'], $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files');
                 $fields[$key] = $filename;
                 $files[] = $filename;
                 $upload->upload($value);
                 unset($_FILES[$key]);
             }
         }
     }
     $fv = new \Phire\Fields\Model\FieldValue();
     $values = $fv->save($fields, $submission->id, 'Phire\\Forms\\Model\\FormSubmission');
     $form = Table\Forms::findById($this->id);
     // If the form action is set
     if (!empty($form->action)) {
         $scheme = $form->force_ssl ? 'https://' : 'http://';
         $action = substr($form->action, 0, 4) == 'http' ? $form->action : $scheme . $_SERVER['HTTP_HOST'] . BASE_PATH . $form->action;
         if ($form->method == 'post') {
             $options = [CURLOPT_POST => true, CURLOPT_POSTFIELDS => $values, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true];
         } else {
             $action .= '?' . http_build_query($values);
             $options = [CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true];
         }
         $curl = new \Pop\Http\Client\Curl($action, $options);
         $curl->send();
         unset($curl);
     }
     // Send the submission if the form "to" field is set
     if (!empty($form->to)) {
         $domain = str_replace('www.', '', $_SERVER['HTTP_HOST']);
         $subject = $form->name . ' : ' . $domain;
         // Set the recipient
         $rcpt = ['email' => $form->to];
         $message = '';
         foreach ($values as $key => $value) {
             $message .= ucwords(str_replace('_', ' ', $key)) . ': ' . (is_array($value) ? implode(', ', $value) : $value) . PHP_EOL;
         }
         // Send form submission
         $mail = new Mail($subject, $rcpt);
         if (!empty($form->from)) {
             if (!empty($form->reply_to)) {
                 $mail->from($form->from, null, false)->replyTo($form->reply_to, null, false);
             } else {
                 $mail->from($form->from);
             }
         } else {
             if (!empty($form->reply_to)) {
                 $mail->replyTo($form->from);
             } else {
                 $mail->from('noreply@' . $domain);
             }
         }
         $mail->setText($message);
         if (count($files) > 0) {
             foreach ($files as $file) {
                 if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files/' . $file)) {
                     $mail->attachFile($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files/' . $file);
                 }
             }
         }
         $mail->send();
     }
     $this->clear();
     if (!empty($form->redirect)) {
         if (substr($form->redirect, 0, 4) == 'http' || substr($form->redirect, 0, 1) == '/') {
             $this->redirect = true;
             $redirect = substr($form->redirect, 0, 4) == 'http' ? $form->redirect : BASE_PATH . $form->redirect;
             header('Location: ' . $redirect);
             exit;
         } else {
             $this->message = $form->redirect;
         }
     }
     return $this;
 }
Example #2
0
 public function testInitHtmlTextAndFile()
 {
     $m = new Mail('Subject', array('name' => 'Bob Smith', 'email' => '*****@*****.**'));
     $m->attachFile(__DIR__ . '/../tmp/test.jpg');
     $m->setHtml('Hello');
     $m->setText('Hello');
     $m->getMessage()->init();
 }
Example #3
0
    $html = <<<HTMLMSG
<html>
<head>
    <title>
        Test HTML Email
    </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <h1>Hello [{name}]</h1>
    <p>
        I'm just trying out this new Pop Mail Library component.
    </p>
    <p>
        Thanks,<br />
        Bob
    </p>
</body>
</html>

HTMLMSG;
    $mail = new Mail('Hello World!', $rcpts);
    $mail->from('*****@*****.**', 'Bob')->setHeaders(array('X-Mailer' => 'PHP/' . phpversion(), 'X-Priority' => '3'));
    $mail->setText("Hello [{name}],\n\nI'm just trying out this new Pop Mail component.\n\nThanks,\nBob\n\n");
    $mail->setHtml($html);
    $mail->attachFile('../assets/files/test.pdf');
    $mail->send();
    echo 'Mail Sent!';
} catch (\Exception $e) {
    echo $e->getMessage();
}