PHP Beitrag

E-Mails mit Anhängen versenden

<?php

define('SERVER_NAME', preg_replace('/www./i', '', $_SERVER['SERVER_NAME']));

define('DATATYPE', 1); // 0 = text/plain | 1 = text/html

$type = 'normal';

switch ($type) {

    case 'high':
        $priority = 1;
        $importance = 'High';
        break;

    case 'normal':
        $priority = 3;
        $importance = 'Normal';
        break;

    default:
        $priority = 3;
        $importance = 'Normal';
        break;
}

$attachment = array();

$attachment[] = array(
    'type' => 'text/plain',
    'name' => 'index.htm',
    'data' => file_get_contents(getcwd().'/index.htm')
);

$from = SERVER_NAME.' <demo@'.SERVER_NAME.'>';

$subject = 'Demo - '.SERVER_NAME;

$cc = null;

$bcc = null;

$header = "From: ".$from.PHP_EOL;

if (!is_null($cc)) { $header .= "Cc: ".$cc.PHP_EOL; }

if (!is_null($bcc)) { $header .= "Bcc: ".$bcc.PHP_EOL; }

$header .= "Message-Id: <".sha1(microtime())."@".SERVER_NAME.">".PHP_EOL;
$header .= "X-Mailer: PHP ".phpversion().PHP_EOL;
$header .= "MIME-Version: 1.0".PHP_EOL;
$header .= "X-Priority: ".$priority.PHP_EOL;
$header .= "Importance: ".$importance.PHP_EOL;

$boundary = '-----='.hash('md5', uniqid(mt_rand(), 1));

$content = (string) '';

$header .= 'Content-Type: multipart/mixed;'.PHP_EOL;
$header .= ' boundary="'.$boundary.'"'.PHP_EOL;

$content .= 'This is a multi-part message in MIME format'.PHP_EOL.PHP_EOL;
$content .= '--'.$boundary.PHP_EOL;

$type = (DATATYPE === 0) ? 'text/html' : 'text/plain';

if (count($attachment) > 0) {

    $content .= 'Content-Type: '.$type.'; charset=utf-8'.PHP_EOL;
    $content .= 'Content-Transfer-Encoding: 8bit'.PHP_EOL.PHP_EOL;
}
else {

    $header .= 'Content-Type: '.$type.'; charset=utf-8'.PHP_EOL;
    $header .= 'Content-Transfer-Encoding: 8bit';
}

$content .= 'Inhalt der E-Mail'.PHP_EOL;

if (count($attachment) > 0) {

    foreach ($attachment as $data) {

        $content .= PHP_EOL.'--'.$boundary.PHP_EOL;
        $content .= 'Content-Type: '.$data['type'].'; name='.$data['name'].PHP_EOL;
        $content .= 'Content-Transfer-Encoding: base64'.PHP_EOL;
        $content .= 'Content-Disposition: attachment; filename='.$data['name'].PHP_EOL.PHP_EOL;
        $content .= chunk_split(base64_encode($data['data']));
    }

    $content .= "\n".'--'.$boundary.'--';
}

var_dump(mail('empfaenger@domain.de', $subject, $content, $header)); // liefert TRUE oder FALSE
E-Mails mit Anhängen versenden
© 2017 Gino Dola
PHP & Web Developer

Schreibe einen Kommentar