How To: Mailformular mit Smarty-Template

Formulare aus denen eine Mail generiert wird sind weit verbreitet. So zum Beispiel bei Kontaktformularen.

Dieses kleine Tutorial soll nun zeigen, wie man dies mittels Smarty-Template realisiert. Als Beispiel nehmen wir oben besagtes Kontaktformular.

Datei: kontakt.tmpl
(Diese enthält das Formular)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<form id="kontakt" action="{$webseite}/kontakt" method="post">
    <h2>Kontakt</h2>

    {if $kontaktErfolg}
        <h3>Kontaktanfrage wurde versendet. Vielen Dank.</h3>
    {/if}

    {if $kontaktAbbruch}
        <h3>Vorgang abgebrochen.</h3>
    {/if}
   
    {if $kontaktFehler}
        <h3 class="kontaktFehler">Bitte füllen Sie die markierten Felder richtig aus.</h3>
    {/if}

    <!-- Name -->
    <div id="name-wrap" class="slider">
        <label for="name">Name <sup>(*)</sup></label>
        <input type="text" id="name" {if $kontaktFehlerName}class="formularErrorField"{/if} name="name" value="{$kontaktName}" tabindex="1" />
        {if $kontaktFehlerName}
            <span class="kontaktFehler">{$kontaktFehlerName}</span>
        {/if}
    </div>

    <!-- E-Mail -->
    <div id="email-wrap"  class="slider">
        <label for="email">E&ndash;Mail <sup>(*)</sup></label>
        <input type="text" id="email" {if $kontaktFehlerEmail}class="formularErrorField"{/if} name="email" value="{$kontaktMail}" tabindex="2" />
        {if $kontaktFehlerEmail}
            <span class="kontaktFehler">{$kontaktFehlerEmail}</span>
        {/if}
    </div>

    <!-- Homepage -->
    <div id="homepage-wrap"  class="slider">
        <label for="homepage">Homepage (http://)</label>
        <input type="text" id="homepage" {if $kontaktFehlerHomepage}class="formularErrorField"{/if} name="homepage" size="25" value="{$kontaktHomepage}" tabindex="3" />
        {if $kontaktFehlerHomepage}
            <span class="kontaktFehler">{$kontaktFehlerHomepage}</span>
        {/if}
    </div>

    <!-- Nachricht -->
    <div id="message-wrap"  class="slider">
        {if $kontaktFehlerNachricht}
            <div class="kontaktFehler">{$kontaktFehlerNachricht}</div>
        {/if}
        <textarea id="nachricht" name="nachricht" {if $kontaktFehlerNachricht}class="formularErrorField"{/if} rows="16" cols="80" tabindex="6" class="expand">{$kontaktNachricht}</textarea>
    </div>

    <p><input type="hidden" id="checkit" name="checkit" value="" /></p>

    <!-- Buttons -->
    <div id="button-wrap">
        <input type="submit" name="kontaktSenden" value="Absenden" />
        <input type="submit" name="kontaktAbbrechen" value="Abbrechen" />
    </div>

    <div id="hinweis-wrap">
        <em><sup>(*)</sup> Pflichtfelder</em>
    </div>
</form>

Datei kontakt-mail.tmpl
(Diese Datei ist die eigentliche Mail)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>{$kontaktTitel}</title>
        <style type="text/css">
            /**
             * Stylesheet kann hier hin
             */
        </style>
    </head>

    <body>
        <p><strong>Hallo</strong></p>
        <p>So eben wurde eine neue Kontaktanfrage über Deine Website vorgenommen.</p>
        <p>Mit folgendem Inhalt:</p>
        <fieldset>
            <legend>
                <span>
                    {$kontaktName}
                </span>
            </legend>
            <div>
                {$kontaktNachricht}
            </div>
        </fieldset>
    </body>
</html>

Datei: kontakt.php
(Die Funktion des Formulars)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
 * Felder überprüfen
 */

if (isset($_POST['kontaktSenden'])) {
    if (strlen($_POST['name'] == '')) {
        $var_bFehler = true;
        $array_templateVariablen = array_merge($array_templateVariablen, array(
            'kontaktFehlerName' => 'Bitte Ihren Namen eintragen.',
        ));
        $array_templateVariablen = array_merge($array_templateVariablen, array(
            'kontaktFehler' => 1,
        ));
    }

    if (strlen($_POST['email'] == '')) {
        $var_bFehler = true;
        $array_templateVariablen = array_merge($array_templateVariablen, array(
            'kontaktFehlerEmail' => 'Bitte eine <strong>gültige</strong> E-Mailadresse.',
        ));
        $array_templateVariablen = array_merge($array_templateVariablen, array(
            'kontaktFehler' => 1,
        ));
    } else {
        if (!strstr($_POST['email'], '@')) {
            $var_bFehler = true;
            $array_templateVariablen = array_merge($array_templateVariablen, array(
                'kontaktFehlerEmail' => 'Bitte geben Sie eine <strong>gültige</strong> E-Mailadresse ein.',
            ));
            $array_templateVariablen = array_merge($array_templateVariablen, array(
                'kontaktFehler' => 1,
            ));
        }
    }

    if (strlen($_POST['homepage'] != '')) {
        if (!strstr($_POST['homepage'], 'http://')) {
            $var_bFehler = true;
            $array_templateVariablen = array_merge($array_templateVariablen, array(
                'kontaktFehlerHomepage' => 'Bitte lassen Sie die URL ihrer Homepage mit <strong>http://</strong> beginnen.',
            ));
            $array_templateVariablen = array_merge($array_templateVariablen, array(
                'kontaktFehler' => 1,
            ));
        }
    }

    if (strlen($_POST['nachricht'] == '')) {
        $var_bFehler = true;
        $array_templateVariablen = array_merge($array_templateVariablen, array(
            'kontaktFehlerNachricht' => 'Bitte eine Nachricht eingeben.',
        ));
        $array_templateVariablen = array_merge($array_templateVariablen, array(
            'kontaktFehler' => 1,
        ));
    } else {
        $var_sPattern = '/[a-z]|[0-9]/i';
        if (!preg_match($var_sPattern, $_POST['nachricht'])) {
            $var_bFehler = true;
            $array_templateVariablen = array_merge($array_templateVariablen, array(
                'kontaktFehlerNachricht' => 'Der Text sollte nicht nur aus Leerzeichen bestehen.',
            ));
            $array_templateVariablen = array_merge($array_templateVariablen, array(
                'kontaktFehler' => 1,
            ));
        }
    }

    if (strlen($_POST['checkit'] != '')) {
        $var_bFehler = true;
        $array_templateVariablen = array_merge($array_templateVariablen, array(
            'kontaktFehlerCheckit' => 'Sry lieber Spammer, aber DU darfst nicht.',
        ));
        $array_templateVariablen = array_merge($array_templateVariablen, array(
            'kontaktFehler' => 1,
        ));
    }

    if (!isset($var_bFehler)) {
        /**
         * Mail an mich :-)
         */

        $var_sReceiver = "mail@empfaenger.de";
        $var_sSender = $_POST['email'];
        $var_sSubject = "Betreff der Mail";

        /* Smarty laden */
        $obj_smarty_kontaktmail = new Smarty();
        /**
         * Auf Safe-Mode prüfen und Smarty dem entsprechend einstellen
         */

        if(ini_get('safe_mode') == "1") {
            $obj_smarty_kontaktmail->use_sub_dirs   = false;
        } else {
            $obj_smarty_kontaktmail->use_sub_dirs   = true;
        }
        /**
         * Template-Engine konfigurieren
         */

        $obj_smarty_kontaktmail->template_dir = SERVER_DIR . 'templates';           # Templateverzeichnis definieren
        $obj_smarty_kontaktmail->compile_dir = SERVER_DIR . 'templates/compile';    # Verzeichnis für kompilierte Daten (chmod 0777)
        $obj_smarty_kontaktmail->cache_dir = SERVER_DIR . 'templates/cache';        # Verzeichnis für Cache (chmod 0777)
        $obj_smarty_kontaktmail->config_dir = SERVER_DIR . 'templates/configs';     # Verzeichnisd für Konfigurationsdaten

        $obj_smarty_kontaktmail->assign("kontaktName", htmlspecialchars($_POST['name'], ENT_QUOTES, 'utf-8'));
        $obj_smarty_kontaktmail->assign("kontaktNachricht", nl2br(htmlspecialchars($_POST['nachricht'], ENT_QUOTES, 'utf-8')));

        $var_TMPLMail = $obj_smarty_kontaktmail->fetch('kontakt-mail.tmpl');

        /* Mailklasse laden */
        include_once ('Rmail/Rmail.php');
        $obj_mail = new Rmail(); // Create the email object

        $obj_mail->setFrom($var_sSender); // Set the From: address
        $obj_mail->setSubject($var_sSubject); // Set the subject
        $obj_mail->setTextCharset('UTF-8');
        $obj_mail->setHTMLCharset('UTF-8');
        $obj_mail->setHeadCharset('UTF-8');
        $obj_mail->setHTML($var_TMPLMail); // Set the HTML (optional)

        /* Send the email */
        $var_mailResult = $obj_mail->send(array(
            $var_sReceiver,
        ));

        /* Erfolg überprüfen */
        if ($var_mailResult) {
            $array_templateVariablen = array_merge($array_templateVariablen, array(
                'kontaktErfolg' => 1,
            ));
        } else {
            echo '' . "\n";
        }
    }
}

/**
 * Kontakt abbrechen
 */

if (isset($_POST['kontaktAbbrechen'])) {
    $array_templateVariablen = array_merge($array_templateVariablen, array(
        'kontaktAbbruch' => 1,
    ));
    unset($_POST);
}
?>
Werbung

WordPress SEO Plugin

Über H.-Peter Pfeufer

Ich bin Webentwickler/ -programmierer aus Leidenschaft, WordPressbegeistert und ab und an auch mal ein netter Mensch.
Vor allen schreibe ich über alles was mit den Themen Webentwicklung/ -programmierung und WordPress zu tun hat. Nebenher auch mal einige Gedanken zu allgemeineren Themen.

Kategorie(n): Smarty-Template
Tags: , , , , ,
Setze ein Lesezeichen auf den Permalink.

Eine Antwort auf How To: Mailformular mit Smarty-Template

  1. [...] damit sagen will, auf jeden Fall ist er bei mir gelandet. Ob jedoch die Erklärung wie man mit Smarty-Template Email-Templates erstellt ausreichend war, kann ich nicht sagen. Vielleicht sollte ich hier irgendwo ein Verzeichnis [...]

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

*

 

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>