PHP HTML email met bijlage
Ik heb een form waarbij een bestand ge-upload kan worden. De gegevens en een link naar het geuploade bestand worden in de database weggeschreven. Werkt allemaal prima.
Nu wil ik graag ook dat er een HTML email verstuurd wordt MET de bijlage.
Welke header/content type moet ik gebruiken?
Zie de tutorials en script eens door.
Voorbeeld met afbeelding:
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
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
<?php
function vmail($to, $toname, $subject, $body, $from = '[email protected]', $fromname = 'Jouw Website') {
require_once('class.phpmailer.php');
$body .= '<img src="cid:matthijs" width="540" height="150">'; // afbeelding gebruiken, naam matthijs, zie hieronder
$Mail = new PHPMailer();
$Mail->AddAddress ($to, $toname);
$Mail->From = $from;
$Mail->FromName = $fromname;
$Mail->Body = $body;
$Mail->Subject = $subject;
$Mail->IsHTML(true);
$Mail->AddEmbeddedImage("matthijs.jpg", "matthijs", "matthijs", "base64", "image/gif"); // afbeelding toevoegen onder naam "mathhijs"
if ($Mail->Send()) {
return true;
}
else {
return false;
}
}
vmail('[email protected]', 'Klant', 'Email met bijlage', '<b>Hoi</b>');
?>
function vmail($to, $toname, $subject, $body, $from = '[email protected]', $fromname = 'Jouw Website') {
require_once('class.phpmailer.php');
$body .= '<img src="cid:matthijs" width="540" height="150">'; // afbeelding gebruiken, naam matthijs, zie hieronder
$Mail = new PHPMailer();
$Mail->AddAddress ($to, $toname);
$Mail->From = $from;
$Mail->FromName = $fromname;
$Mail->Body = $body;
$Mail->Subject = $subject;
$Mail->IsHTML(true);
$Mail->AddEmbeddedImage("matthijs.jpg", "matthijs", "matthijs", "base64", "image/gif"); // afbeelding toevoegen onder naam "mathhijs"
if ($Mail->Send()) {
return true;
}
else {
return false;
}
}
vmail('[email protected]', 'Klant', 'Email met bijlage', '<b>Hoi</b>');
?>
Nu dus effe in documentatie kijken hoe je bijlage doet, zelfde soort manier.
Gewijzigd op 01/01/1970 01:00:00 door PHP erik
Bedankt voor jullie reactie. Ik zal even posten wat ik tot nu toe heb.
De volgende code heb ik gevonden om een bijlage via mail te versturen:
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
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
<?php
//File attachments work just like mixed email, except that a different content type is
//used for the message as a whole (multipart/mixed instead of multipart/alternative),
//and there's a new Content-Disposition header that tells the email client how to
//handle each part of the message.
//Let's write a PHP script that processes a form submission that contains an email
// message to be sent, possibly with a file attachment, and sends it out. I'll talk
// you through it line by line so that by the end you'll not only have a useful snippet
//of PHP code, but also an understanding of how file attachments work. You can download
//the script (and the form for it) if you want to try it out for yourself.
//First, we grab the submitted values and place them in PHP variables. Most people have
//their servers set up to create global variables for submitted values automatically,
//but as of PHP 4.1 this is no longer the default, so we do it by hand just in case.
//Since we want to accept file attachments, it's safe to assume that the form will
//be submitted with a POST request:
// Read POST request params into global vars
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//File uploads in PHP 4.1 are placed in a special $_FILES array, so we fetch the values
//we need out of it:
// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
//For the sake of brevity, we'll assume that the required parameters ($to and $from)
//now have valid values (email addresses) in them. Normally we would check their format
//with regular expressions. Next, we use the $from value to begin building the extra
//headers for the email:
$headers = "From: $from";
//Next we check the $fileatt variable, which may or may not contain the path and filename
//to an uploaded file attachment. We use PHP's is_uploaded_file function to find out:
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
//aving read in the data for the file attachment, we need to set up the message headers
//to send a multipart/mixed message:
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
//Now for the message body itself. This works just as we saw for the text part of a mixed
//message in the previous section:
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
//Now, to allow for binary file types, we need to use Base64 encoding to convert the
//(possibly binary) file attachment data to a text-only format suitable for sending
//by email. All email programs in popular use support Base64 encoding of file attachments,
//so this is the best way to go. Fortunately, PHP provides a function for Base64 encoding:
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
//We now have everything we need to write the portion of the message that contains the
//file attachment. Here's the code:
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
//That completes the modifications necessary to accommodate a file attachment.
//We can now send the message with a quick call to mail:
// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
//File attachments work just like mixed email, except that a different content type is
//used for the message as a whole (multipart/mixed instead of multipart/alternative),
//and there's a new Content-Disposition header that tells the email client how to
//handle each part of the message.
//Let's write a PHP script that processes a form submission that contains an email
// message to be sent, possibly with a file attachment, and sends it out. I'll talk
// you through it line by line so that by the end you'll not only have a useful snippet
//of PHP code, but also an understanding of how file attachments work. You can download
//the script (and the form for it) if you want to try it out for yourself.
//First, we grab the submitted values and place them in PHP variables. Most people have
//their servers set up to create global variables for submitted values automatically,
//but as of PHP 4.1 this is no longer the default, so we do it by hand just in case.
//Since we want to accept file attachments, it's safe to assume that the form will
//be submitted with a POST request:
// Read POST request params into global vars
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//File uploads in PHP 4.1 are placed in a special $_FILES array, so we fetch the values
//we need out of it:
// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
//For the sake of brevity, we'll assume that the required parameters ($to and $from)
//now have valid values (email addresses) in them. Normally we would check their format
//with regular expressions. Next, we use the $from value to begin building the extra
//headers for the email:
$headers = "From: $from";
//Next we check the $fileatt variable, which may or may not contain the path and filename
//to an uploaded file attachment. We use PHP's is_uploaded_file function to find out:
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
//aving read in the data for the file attachment, we need to set up the message headers
//to send a multipart/mixed message:
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
//Now for the message body itself. This works just as we saw for the text part of a mixed
//message in the previous section:
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
//Now, to allow for binary file types, we need to use Base64 encoding to convert the
//(possibly binary) file attachment data to a text-only format suitable for sending
//by email. All email programs in popular use support Base64 encoding of file attachments,
//so this is the best way to go. Fortunately, PHP provides a function for Base64 encoding:
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
//We now have everything we need to write the portion of the message that contains the
//file attachment. Here's the code:
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
//That completes the modifications necessary to accommodate a file attachment.
//We can now send the message with a quick call to mail:
// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
De volgende code is van mijzelf om een simpele html email te versturen:
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
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
<?php
$headers ="Content-Type: text/html;\n";
$headers .="From: \"Test\" <[email protected]>\n";
$bodytext= "<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
</head>
<body>
<table width=\"736\" border=\"0\">
<tr>
<td width=\"140\"><span class=\"normaal_vet_dashed\">ProbleemID :</span></td>
<td width=\"596\" colspan=\"2\" class=\"normaal_solid\">" . $pid . "</td>
</tr>
<tr>
<td><span class=\"normaal_vet_dashed\">Datum / Tijd :</span></td>
<td colspan=\"2\" class=\"normaal_solid\">" . $datumtijd . "</td>
</tr>
</table>
</body>
</html>";
$subject ="Titel : ";
$subject .=$titel;
if($test == "ja") {
mail("[email protected]", $subject,$bodytext,$headers);
}
elseif($test == "nee") {
mail("[email protected]", $subject,$bodytext,$headers);
?>
$headers ="Content-Type: text/html;\n";
$headers .="From: \"Test\" <[email protected]>\n";
$bodytext= "<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
</head>
<body>
<table width=\"736\" border=\"0\">
<tr>
<td width=\"140\"><span class=\"normaal_vet_dashed\">ProbleemID :</span></td>
<td width=\"596\" colspan=\"2\" class=\"normaal_solid\">" . $pid . "</td>
</tr>
<tr>
<td><span class=\"normaal_vet_dashed\">Datum / Tijd :</span></td>
<td colspan=\"2\" class=\"normaal_solid\">" . $datumtijd . "</td>
</tr>
</table>
</body>
</html>";
$subject ="Titel : ";
$subject .=$titel;
if($test == "ja") {
mail("[email protected]", $subject,$bodytext,$headers);
}
elseif($test == "nee") {
mail("[email protected]", $subject,$bodytext,$headers);
?>
Wat ik nu wil deze 2 codes combineren. Maar ik krijg het niet voor elkaar.
Kan iemand mij helpen?
Gewijzigd op 01/01/1970 01:00:00 door matthijs
iemand een idee? Ik krijg het echt niet voor elkaar. zwaar frustrerend.
Met name de veiligheid is hierin goed geregeld.
Nee dat heb ik niet en dat wil ik ook eigenlijk niet.
Beide scripts werken apart van elkaar en ik vraag alleen om hulp om deze te combineren omdat het mij niet lukt.
Dan kan PHPmailer goed werken en veilig zijn, maar zo kan je wel alle code kopiëren en plakken zonder dat je wat van PHP afweet bij wijze van spreken.
Voor ervaren PHP-ers moet dit toch niet zo moeilijk zijn?
Gr,
Matthijs