phpmailer and gmail
Dit is wat ik zie:
Dit is mijn mailcode met form eronder:
Code (php)
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
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
<?php
// functie spamcheck
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// definieren variabelen
// PHP mailer settings
require_once ('../phpmailer/class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->CharSet = 'utf-8'; //sets character set
$mail->IsSMTP(); //To inform that we are using SMTP
$mail->SMTPDebug = 2; //Shows long message set to 1 to avoid it
$mail->Host = "smtp.gmail.com"; // As we are using gmail SMTP
$mail->Port = "465"; // smtp port number
$mail->SMTPSecure = "ssl"; //gmail requires authentication through ssl or tls
$mail->SMTPAuth = true;
// your account details for authentication provide correct user name and password
$mail->Username = "[email protected]";
$mail->Password = "mijnpassword";
$mail->From = $_POST['email'];
$mail->FromName = $_POST['naam'];
$mail->AddAddress("[email protected]", "Jack Maessen");
$mail->Subject = "Contactformulier";
// variabelen body en body email opmaken
$naam = $_POST['naam'];
$achternaam = $_POST['achternaam'];
$email = $_POST['email'];
$bericht = $_POST['bericht'];
$body = "";
$body .= "Naam: ";
$body .= $naam;
$body .= "<br />";
$body .= "Achternaam: ";
$body .= $achternaam;
$body .= "<br />";
$body .= "Email: ";
$body .= $email;
$body .= "<br />";
$body .= "Bericht: ";
$body .= $bericht;
$body .= "<br />";
$mail->WordWrap = 80;
$mail->MsgHTML($body, dirname(__FILE__), true); //Create message bodies and embed images
// check of submitter een robot is en of er geldige input is geleverd
$mailcheck = spamcheck($_POST['email']);
if($_POST['robot'] != "test_spambot") {
die();
}
//check of email geldig is
elseif ($mailcheck==FALSE) {
echo "Ongeldige input";
}
else {
// email verzenden
$formsent = $mail->Send();
// echo's als verzenden goed of fout is gegaan
if ($formsent){
echo 'Dank voor het versturen';
}
else{
echo 'Sorry, maar er is iets misgegaan met het formulier, probeer het later nog eens.';
}
}
} // eind request method
?>
<form action="" method="post" name="" id="">
<input type="hidden" name="robot" value="test_spambot" /><br />
Naam: <br />
<input type="text" name="naam" /><br />
Achternaam: <br />
<input type="text" name="achternaam" /><br />
Email: <br />
<input type="email" name="email" /><br />
Bericht: <br />
<textarea name="bericht" /></textarea>
<br /><br />
<input type="reset" value="Reset" />
<input type="submit" value="Verzenden" />
</form>
// functie spamcheck
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// definieren variabelen
// PHP mailer settings
require_once ('../phpmailer/class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->CharSet = 'utf-8'; //sets character set
$mail->IsSMTP(); //To inform that we are using SMTP
$mail->SMTPDebug = 2; //Shows long message set to 1 to avoid it
$mail->Host = "smtp.gmail.com"; // As we are using gmail SMTP
$mail->Port = "465"; // smtp port number
$mail->SMTPSecure = "ssl"; //gmail requires authentication through ssl or tls
$mail->SMTPAuth = true;
// your account details for authentication provide correct user name and password
$mail->Username = "[email protected]";
$mail->Password = "mijnpassword";
$mail->From = $_POST['email'];
$mail->FromName = $_POST['naam'];
$mail->AddAddress("[email protected]", "Jack Maessen");
$mail->Subject = "Contactformulier";
// variabelen body en body email opmaken
$naam = $_POST['naam'];
$achternaam = $_POST['achternaam'];
$email = $_POST['email'];
$bericht = $_POST['bericht'];
$body = "";
$body .= "Naam: ";
$body .= $naam;
$body .= "<br />";
$body .= "Achternaam: ";
$body .= $achternaam;
$body .= "<br />";
$body .= "Email: ";
$body .= $email;
$body .= "<br />";
$body .= "Bericht: ";
$body .= $bericht;
$body .= "<br />";
$mail->WordWrap = 80;
$mail->MsgHTML($body, dirname(__FILE__), true); //Create message bodies and embed images
// check of submitter een robot is en of er geldige input is geleverd
$mailcheck = spamcheck($_POST['email']);
if($_POST['robot'] != "test_spambot") {
die();
}
//check of email geldig is
elseif ($mailcheck==FALSE) {
echo "Ongeldige input";
}
else {
// email verzenden
$formsent = $mail->Send();
// echo's als verzenden goed of fout is gegaan
if ($formsent){
echo 'Dank voor het versturen';
}
else{
echo 'Sorry, maar er is iets misgegaan met het formulier, probeer het later nog eens.';
}
}
} // eind request method
?>
<form action="" method="post" name="" id="">
<input type="hidden" name="robot" value="test_spambot" /><br />
Naam: <br />
<input type="text" name="naam" /><br />
Achternaam: <br />
<input type="text" name="achternaam" /><br />
Email: <br />
<input type="email" name="email" /><br />
Bericht: <br />
<textarea name="bericht" /></textarea>
<br /><br />
<input type="reset" value="Reset" />
<input type="submit" value="Verzenden" />
</form>
Hoe kan ik die overbodige informatie weg krijgen zodat alleen de echo verschijnt?
Gewijzigd op 12/11/2013 15:36:47 door Jack Maessen
$mail->SMTPDebug = 2; //Shows long message set to 1 to avoid it
Kris Peeters op 12/11/2013 15:48:22:
exit; is onnodig voor dit.
@Kris: dat ziet er idd wat professioneler uit als men de form verzonden heet
Dank voor support