PDA

Volledige versie bekijken : html code binnen een e-mail


skippy
%Europe/Berlin %483 %2005, 12:35
Weet iemand hoe ik html code binnen een e-mail kan krijgen.

Als ik momenteel html code binnen de e-mail verwerk wordt deze gewoon als platte tekst geinterpreteerd en niet verwerkt als HTML code waardoor je eigenlijk gewoon de source code van de html file binnen de e-mail te zien krijg [V] .

Huidige code ziet er als volgt uit:

<html>
<head>
<title>E-mailformulier</title>
</head>
<body>
<?


$joumail = "mijnemailadres@hotmail.com";

mail("$mijnmail", "$onderwerp", "

<table width=\"185\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">
<tr>
<td><div align=\"center\"><font face=\"Arial, Helvetica, sans-serif\"><strong>Dit
is ff een test om te kijken of dit werkt</strong></font></div></td>
</tr>
<tr><td><br></td></tr>
<tr>
<td><img src=\"test.jpg\" width=\"780\" height=\"300\"></td>
</tr>
</table>




$email", "From: $voornaam $tussenvoegsels $achternaam<$onderwerp $provider>\nReturn-path: $email");

?>

colake
%Europe/Berlin %505 %2005, 13:07
Zet in de mail ook eens <html><body> voor je de echte inhoud geeft en erna niet vergeten af te sluiten.

skippy
%Europe/Berlin %575 %2005, 14:48
Bedankt voor je reactie en je had gelijk ik moest de html en body tags in het mailtje includen :P , maar hij doet het nog niet binnen outlook.

Weet je daar ook iets voor :D

De Kale
%Europe/Berlin %378 %2005, 10:05
je moet de juiste headers meesturen om html email te kunnen verzenden.
dat doe je via de 4de parameter van de mail() functie.
check de php manual om te zien hoe het moet ;) (online staat een voorbeeld)

jbulckens
%Europe/Berlin %591 %2005, 15:11
Hey, ik heb hier nog een voorbeeldje liggen van html in php dat lukt. Ik zal het hieronder zetten en dan moet je het maar bestuderen.

Of het helemaal correct is weet ik niet maar het macheert wel.

Als je je html bestand in de php zet moet je voor elke - " - een schuine streep zetten.
Dus \"

veel succes!

<?php
### Message.
$message = "<html>
<head>

<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
<style type=\"text/css\">
<!--
body {
background-color: #FFFFFF;
}
.style1 {font-family: Tahoma}
a:link {
color: #006600;
}
a:visited {
color: #006633;
}
a:hover {
color: #33FF99;
}
a:active {
color: #336666;
}
.style9 {font-size: 14px; font-family: Geneva, Arial, Helvetica, sans-serif; font-weight: bold; }
.style11 {font-family: Geneva, Arial, Helvetica, sans-serif; font-weight: bold; }
.style19 {
font-size: 10px;
font-family: verdana;
}
.style24 {font-size: 14px}
.style25 {
font-size: 9px;
font-family: verdana;
color: #999999;
}
.style26 {font-size: 10px; font-family: verdana; font-weight: bold; }
-->
</style></head>
<body>
<div align=\"center\">
<table width=\"621\" border=\"4\" align=\"center\" bordercolor=\"#000000\">
<tr>
<td width=\"605\" bgcolor=\"#F2FFDF\"><div align=\"center\">
<table width=\"542\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#666666\" bgcolor=\"#666666\">
<tr bordercolor=\"#666666\" bgcolor=\"#00CCFF\">
<td width=\"249\"><p align=\"right\" class=\"style9\">Naam: <br>
</p></td>
<td width=\"8\">&nbsp;</td>
<td width=\"277\" class=\"style11\">$varnamee</td>
</tr>
<tr bordercolor=\"#666666\" bgcolor=\"#00CCFF\">
<td><div align=\"right\" class=\"style9\">E-mailadres: <br>
</div></td>
<td>&nbsp;</td>
<td class=\"style11\">$varemaill</td>
</tr>
<tr bordercolor=\"#666666\" bgcolor=\"#00CCFF\">
<td><div align=\"right\" class=\"style9\">Aantal porties mosselen (&euro;15): <br>
</div></td>
<td><div align=\"center\"> </div></td>
<td class=\"style11\">$varaantal</td>
</tr>
<tr bordercolor=\"#666666\" bgcolor=\"#00CCFF\">
<td><div align=\"right\" class=\"style9\">Aantal porties vol-au-vent (&euro;15): <br>
</div></td>
<td>&nbsp;</td>
<td class=\"style11\">$varaantal2</td>
</tr>
<tr bordercolor=\"#666666\" bgcolor=\"#00CCFF\">
<td><div align=\"right\" class=\"style9\">Totaal bedrag te betalen: <br>
</div></td>
<td>&nbsp;</td>
<td class=\"style11\">$uitkomst euro</td>
</tr>
</table>
</div> <p align=\"center\" class=\"style19\"><strong>Dankuwel voor je inschrijving!<br>
<br>
Op zaterdag 16 oktober verwachten we je in de eetzaal van Sint-Lutgardis.<br>
</strong> <br>
<span class=\"style24\">Gelieve &euro;$uitkomst te storten op rekeningnummer: <strong>063-9010140-39</strong></span><br>
(voor vrijdag 15 oktober!) <br>
<br>
Indien u nog vragen hebt kan u ons bereiken op mossel@7de.be<br>
of je kan bellen naar Marco: 0476 209 925 </p>
<p align=\"center\" class=\"style26\">Het mossel-comit&eacute;</p>
<p align=\"center\" class=\"style25\">(de winst gaat naar nieuw materiaal voor de zevende. Op scoutskamp dit jaar is er veel kapot gegaan door zware storm) </p></td>
</tr>
</table>
</div>
</body>
</html>
"
;

### Recepient
$to = "$varemaill";


### Subject.
$subject = "Bevestiging inschrijving mosseletentje";

### Headers.
$headers = "From: mossel@7de.be\n";
$headers .= "X-Sender: \"rot\" \n";
$headers .= "X-Mailer: PHP\n";
$headers .= "Bcc: mosseletentje@7de.be\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";



### Let's mail it.
mail($to, $subject, $message, $headers);
?>

[m]
%Europe/Berlin %830 %2005, 20:55
http://www.alistapart.com/articles/cssemail

mech7
%Europe/Berlin %838 %2005, 21:07
Als je niet zin heb om al die quotes te tikken doe dan heredoc syntax..

$mail = <<<EOD
<p class="title">Dit is een mailtje</p>
bladieda bla..
leuk he
EOD;

yusufdestina
%Europe/Berlin %978 %2005, 00:29
Ik gebruik hiervoor een php class..
Class mmail.php

<?php
/************************************************\
Name : mMail
Version : 1.0
Author : Fadil Kujundzic
Date : 18 May 2005

mMail is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

mMail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with mMail; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\***********************************************/

class mMail
{
var $To;
var $Cc;
var $Bcc;
var $From;
var $ReplayTo;
var $Subject;
var $Header;
var $Body_html;
var $Html_images;
var $Body_txt;
var $Attachments;
var $Boundary;

function mMail()
{
$this->To = null;
$this->Cc = null;
$this->Bcc = null;
$this->From = null;
$this->ReplayTo = null;
$this->Subject = "";
$this->Header = null;
$this->Body_html = null;
$this->Html_images = null;
$this->Body_txt = null;
$this->Attachments = null;
}

function setHtmlBody($body)
{
$this->Body_html = $body;
}

function setTextBody($body)
{
$this->Body_txt = $body;
}

function addTo($email)
{
if(!is_array($this->To))
$this->To = array();
if($this->checkMail($email))
{
$this->To[] = $email;
return true;
}
return false;
}

function addCc($email)
{
if(!is_array($this->Cc))
$this->Cc = array();
if($this->checkMail($email))
{
$this->Cc[] = $email;
return true;
}
return false;
}

function addBcc($email)
{
if(!is_array($this->Bcc))
$this->Bcc = array();
if($this->checkMail($email))
{
$this->Bcc[] = $email;
return true;
}
return false;
}

function setFrom($name)
{
$this->From = $name;
}

function setSubject($subject)
{
$this->Subject = $subject;
}

function setReplayTo($email)
{
if($this->checkMail($email))
{
$this->ReplayTo = $email;
return true;
}
return false;
}

function addAttachment($name, $type, $path, $encoding="base64")
{
if(!is_array($this->Attachments))
$this->Attachments = array();
$this->Attachments[] = array('name'=>$name, 'type'=>$type, 'path'=>$path, 'encoding'=>$encoding);
}

function send()
{
$mail = $this->build();
foreach($this->To as $to)
mail($to, $this->Subject, $mail, $this->Header);
}

function build()
{
$this->getHeader();
if($this->Body_html && !is_array($this->Attachments))
{
$boundary = "=-".md5(microtime());
$this->Header = preg_replace("=\[BOUNDARY\]=", $boundary, $this->Header);
$boundary_alt = "=-".md5(microtime());
$this->parseHtml($boundary);
$msg = "--".$boundary."\n".
"Content-Type: multipart/alternative; boundary=\"".$boundary_alt."\"\n\n".
"--".$boundary_alt."\n".
"Content-Type: text/plain; charset=iso-8859-1\n".
"Content-Transfer-Encoding: 7bit\n\n".
$this->Body_txt."\n\n".
"--".$boundary_alt."\n".
"Content-Type: text/html; charset=utf-8\n".
"Content-Transfer-Encoding: 7bit\n\n".
$this->Body_html."\n\n".
"--".$boundary_alt."--\n\n".
$this->Html_images.
"--".$boundary."--\n";
}
elseif($this->Body_html && is_array($this->Attachments))
{
$boundary = "=-".md5(microtime());
$boundary_mail = "=-".md5(microtime());
$this->Header = preg_replace("=\[BOUNDARY\]=", $boundary, $this->Header);
$boundary_alt = "=-".md5(microtime());
$this->parseHtml($boundary_mail);
$msg = "--".$boundary."\n".
"Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"".$boundary_mail."\"\n\n".
"--".$boundary_mail."\n".
"Content-Type: multipart/alternative; boundary=\"".$boundary_alt."\"\n\n".
"--".$boundary_alt."\n".
"Content-Type: text/plain; charset=iso-8859-1\n".
"Content-Transfer-Encoding: 7bit\n\n".
$this->Body_txt."\n\n".
"--".$boundary_alt."\n".
"Content-Type: text/html; charset=utf-8\n".
"Content-Transfer-Encoding: 7bit\n\n".
$this->Body_html."\n\n".
"--".$boundary_alt."--\n\n".
$this->Html_images.
"--".$boundary_mail."--\n\n".
$this->getAttachments($boundary).
"--".$boundary."--\n";
}
elseif($this->Body_txt && is_array($this->Attachments))
{
$boundary = "=-".md5(microtime());
$this->Header = preg_replace("=\[BOUNDARY\]=", $boundary, $this->Header);
$msg = "--".$boundary."\n".
"Content-Type: text/plain; charset=iso-8859-1\n".
"Content-Transfer-Encoding: 7bit\n\n".
$this->Body_html."\n\n".
$this->getAttachments($boundary).
"--".$boundary."--\n";
}
else
$msg = $this->Body_txt;
return $msg;
}

function getHeader()
{
$this->Header = "From: ".$this->From.($this->ReplayTo ? '<'.$this->ReplayTo.'>' : '')."\n";
$this->Header .= "Subject: ".$this->Subject."\n";
if(is_array($this->Cc))
$this->Header .= "Cc: ".implode(',', $this->Cc)."\n";
if(is_array($this->Bcc))
$this->header .= "Bcc: ".implode(',', $this->Bcc)."\n";
$this->Header .= "Date: ".date("D, d M Y H:i:s")."\n";
$this->Header .= "Mime-Version: 1.0\n";
$this->Header .= "Message-ID: <id_".md5(microtime()).">\n";
if($this->Body_html)
$this->Header .= is_array($this->Attachments) ? "Content-Type: multipart/mixed; boundary=\"[BOUNDARY]\"\n" : "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"[BOUNDARY]\"\n";
else
$this->Header .= is_array($this->Attachments) ? "Content-Type: multipart/mixed; boundary=\"[BOUNDARY]\"\n" : "Content-Type: text/plain; charset=iso-8859-1\nContent-Transfer-Encoding: 7bit\n\n";
$this->Header .= "\n";
}

function parseHtml($boundary)
{
$images = array();
$html_images = "";
preg_match_all("/src=\"[^\"]*\"/", $this->Body_html, $images);
foreach($images[0] as $image)
{
$path = preg_replace(array("/src=/", "/\"/"), "", $image);
$iname = explode('/', $path);
$iname = $iname[count($iname)-1];
$itype = explode('.', $iname);
$itype = $itype[count($itype)-1];
$cid = 'image_'.md5(microtime());
$path = preg_replace("/http\:\/\//", "", $path);
$path = explode("/", $path);
$path[0] = substr($_SERVER['DOCUMENT_ROOT'],0,strlen($_SERVER['DOCUMENT_ROOT']));
$path = implode("/", $path);
$fp = fopen($path,'r');
$data = chunk_split(base64_encode(fread($fp, filesize($path))));
fclose($fp);
$this->Body_html = preg_replace("/(".addcslashes($image, "./").")/", "src=\"cid:".$cid."\"", $this->Body_html);
$html_images .= "--".$boundary."\n".
"Content-Type: image/".$itype."; name=\"".$iname."\"\n".
"Content-Transfer-Encoding: base64\n".
"Content-ID: <".$cid.">\n".
"Content-Disposition: attachment; filename=".$iname."\n\n".
$data."\n\n";
}
$this->Html_images = $html_images;
if(!$this->Body_txt)
$this->Body_txt = strip_tags($this->Body_html);
}

function getAttachments($boundary)
{
$attachments = "";
foreach($this->Attachments as $attachment)
{
$attachments .= "--".$boundary."\n".
"Content-Type: ".$attachment['type']."; name=\"".$attachment['name']."\"\n".
"Content-Transfer-Encoding: ".$attachment['encoding']."\n".
"Content-Disposition: attachment; filename=".$attachment['name']."\n\n";
$fp = fopen($attachment['path'],'r');
$data = fread($fp, filesize($attachment['path']));
fclose($fp);
$attachments .= ($attachment['encoding'] != 'base64' ? $data : chunk_split(base64_encode($data)))."\n\n";
}
return $attachments;
}

function checkMail($email)
{
if(eregi("^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@('.'([-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.)'.'([-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$))", $email))
{
return FALSE;
}
list($Username, $Domain) = split("@",$email);
if(@getmxrr($Domain, $MXHost))
{
return TRUE;
}
else
{
if(@fsockopen($Domain, 25, $errno, $errstr, 30))
{
return TRUE;
}
else
{
return FALSE;
}
}
}
}
?>

Example.php

<?php
include_once("mmail.php");


/****** HTML Email with attachments ****************/
$mail1 = new mMail();

$mail1->setSubject("hallo html");

$mail1->addTo("friend1@domain.com");
$mail1->addTo("friend2@domain.com");

$mail1->addBcc("friend3@domain.com");

$mail1->addCc("friend4@domain.com");
$mail1->addCc("friend5@domain.com");

$mail1->setFrom("me@mydomain.com");
$mail1->setReplayTo("me@mydomain.com");

$html = implode('', file("myhtmlfile.html"));
# OR
$html = '<table border="2">
<tr>
<td><img src="http://myDomain/image.jpg" border="0"></td>
<td>asdf asdf asdf asdf</td>
<tr>
</table>';

$mail1->setHtmlBody($html);

#$mail1->setTextBody("some text");
#If not call "setTextBody()" function mmail class will create alternate text e-mail from html content.

$mail1->addAttachment("some_image.gif", "image/gif", "some_image.gif");
$mail1->addAttachment("some_doc_file.doc", "application/msword", "some_doc_file.doc");

$mail1->send();


/****** Text Email with attachments ****************/
$mail2 = new mMail();

$mail2->setSubject("hallo text");
$mail2->addTo("friend1@domain.com");
$mail2->addTo("friend2@domain.com");

$mail2->addCc("friend3@domain.com");
$mail2->addCc("friend4@domain.com");

$mail2->setFrom("me@mydomain.com");
$mail2->setReplayTo("me@mydomain.com");

$mail2->setTextBody("some text");

$mail2->addAttachment("some_image.gif", "image/gif", "some_image.gif");
$mail2->addAttachment("some_doc_file.doc", "application/msword", "some_doc_file.doc");

$mail2->send();


/****** Only attachments ****************/
$mail3 = new mMail();

$mail3->setSubject("hallo attachment");
$mail3->addTo("friend1@domain.com");
$mail3->addTo("friend2@domain.com");

$mail3->addBcc("friend3@domain.com");

$mail3->setFrom("me@mydomain.com");
$mail3->setReplayTo("me@mydomain.com");

$mail3->addAttachment("some_image.gif", "image/gif", "some_image.gif");
$mail3->addAttachment("some_doc_file.doc", "application/msword", "some_doc_file.doc");

$mail3->send();


?>

succes d'rmee

mech7
%Europe/Berlin %256 %2005, 07:08
PHP Mailer is ook zeer uigebreide class ^_^

http://phpmailer.sourceforge.net/