Eens zien of de laatste phpmailer versie wat beter is dan wat ik nu heb.
Wat de analogie met die fiets betreft:
Soms kan het wel nuttig zijn om vanaf 0 te herbeginnen.
Indien je deftig leert programmeren zou dat niet echt een probleem moeten zijn.
Je zou een eigen functie verzendMail($message, $from, $to, ...) moeten hebben.
Een aanpassing van de mail functionaliteit zou je dan ook enkel in de functie verzendMail moeten doen.
EDIT
Ik heb dit getest. Het werkt prima.
Voorbeeld van gebruik:
Download phpmailer v5
http://phpmailer.codeworxtech.com/index.php?pg=phpmailer en zet de files in de zelfde map als index.php (ofwel zet je ze elder, maar moet je het aanpassen in het script op lijn 84)
index.php
<?php
// TO DO gebruiker: pas deze gegevens eerst aan.
define ('Username', 'info');
define ('password', '******');
define ('host', '***.com');
define ('from', '***@***.com');
define ('from_name', 'Mijn Naam');
// laatste TO DO gebruiker:
define ('SEND_MAIL', false); // zet op true indien je de mail wil sturen. Indien false krijg je een print_r van het phpmailer object. Pas eerst alle gevevens aan. Test best eerst met false
switch( empty($_GET['p']) ? '' : $_GET['p'] )
{
default: echo '<html>
<head>
<style>
#add_attachment{cursor: pointer;}
input{width: 300px}
</style>
<script>
var index=0;
function addAttachment()
{
document.getElementById("attachments_div").innerHTML += "<div><input type=\"file\" name=\"attachment_"+ index++ +"\" /></div>";
}
</script>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="index.php?p=verzend">
<div><label><input type="text" name="from" value="'. from .'"/> from </label></div>
<div><label><input type="text" name="from_name" value="'. from_name .'"/> Display name </label></div>
<div><label><input type="text" name="subject" /> subject </label></div>
<div><label><textarea name="to" rows="5" cols="60">'. TO_ARRAY .'</textarea> to (1 per lijn)</label></div>
<div><label><textarea name="body" rows="10" cols="60"></textarea> body (geen html header toevoegen) </label></div>
<div id="add_attachment" onClick="addAttachment();">add attachment</div>
<div id="attachments_div"></div>
<div><label><input type="submit" value="Verzend mail"/></label></div>
</form>
</body>
</html>';
break;
case 'verzend':
if ( !empty($_POST ) )
{
$toArray = array();
$_POST['to'] = explode ("\n", $_POST['to']);
foreach($_POST['to'] as $to)
{
if (! empty($to) )
$toArray[] = trim($to);
}
$res = verzendMail( $_POST['body'],
$_POST['subject'],
$_POST['from'],
$toArray,
'contents.html',
host,
$_POST['from_name'],
SEND_MAIL ,
$_FILES
);
if ($res === true)
$message = 'mail verstuurd. <a href="index.php">terug</a>';
else
$message = '<pre>'. print_r($res,1) .'</pre>'; // in principe zou je hier iets anders mee doen, maar is is wel nuttig om dit eens te zien.
echo $message;
}
break;
}
function verzendMail($body, $subject, $from, $to=array(), $htmlHeaderFile, $host="", $fromName = "Emmanuel Delay", $doMail=false, $attachments )
{
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/
// Aangepast door Emmanuel Delay
try {
require 'class.phpmailer.php'; // eventueel het pad aanpassen. Zorg dat de file bestaat. Download phpmailer v5
http://phpmailer.codeworxtech.com/index.php?pg=phpmailer
if ($htmlHeaderFile)
{
$a = file_get_contents($htmlHeaderFile);
$a = str_replace('{title}', $subject, $a);
$a = str_replace('{body}', $body, $a);
$body = $a;
}
$mail = new PHPMailer(true); //New instance, with exceptions enabled
//$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = host; // "mail.yourdomain.com"; // SMTP server
$mail->Username = Username; //"
[email protected]"; // SMTP server username
$mail->Password = password; // SMTP server password
$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo($from,$fromName);
$mail->From = $from;
$mail->FromName = $fromName;
foreach ($to as $a)
$mail->AddAddress($a);
$mail->Subject = $subject ;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
$mail->attachmentsErrors = array();
foreach ($attachments as $key => $atachment)
{
if(! $mail->AddAttachment($atachment['tmp_name'], $atachment['name']) )
{
$mail->attachmentsErrors[] = $key;
}
}
if ($doMail)
{
return $mail->Send() ;
}
else
{
return $mail;
}
}
catch (phpmailerException $e)
{
$mail->errors = $e;
return $mail;
}
}
?>
contents.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>{title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
{body}
</body>
</html>
Beschouw dit niet als een script dat af is. Beveilig zelf, breid dit zelf uit.
Je doet er mee wat je wil.