Php Mailer with Attachment - Step by Step

I have tried searching sitepoint and believe many people including myself would benifit from a walk-through on phpmailer. There are many posts that are related to different parts but none that cover from script install through to form design.

STEP 1 - Installing phpmailer and testing

This first step is simple. Download the files here http://phpmailer.sourceforge.net/
Upload the ALL the files to the directory from which the script will be called. You can test all is well by uploading a file called test.php that contains the following (http://phpmailer.sourceforge.net/tutorial.html)

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
?>

Browse to the test.php file and if no errors are returned the install is correct.

Whats next?

Save the following code into a php document. Alter the settings as required for your server eg. “smtp.email.com”, “from@email.com” & “myfriend@site.com

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();	// telling the class to use SMTP
$mail->Host = "smtp.email.com"; // SMTP server
$mail->From = "from@email.com";
$mail->AddAddress("myfriend@site.com");

$mail->Subject = "first mailing";
$mail->Body = "hi ! \
\
 this is First mailing I made myself with PHPMailer !";
$mail->WordWrap = 50;

if(!$mail->Send())
{
   echo "Message was not sent";
   echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
   echo "Message has been sent";
}
?>

Upload to the same diectory as before and browse to the file. The Message should be sent and confirmation message appear.

A simple HTML form can be used to pass details to the script to be sent instead of the fixed message used previously.

Important parts to the form are the filename of the script
And the variable that will contain the data from the form

HTML FORM


<form name="contact" method="post"  action="phpmailerscript.php">

<input name="name" type="text" size="45" value="<?php echo $name ?>">


Then in the phpmailer script the variable needs to be collected and put in the body of the sent email. For example replace the previous

$mail->Body = "hi ! \
\
 this is First mailing I made myself with PHPMailer !";

with

$mail->Body = "Name: $name\
";

now how do we add an attachment??



$mail->AddAttachment("../dir_to_file/File.dot");


Could you expand on his pmm001?

I was actually hoping to send the attachment using the form. Using something like…

    <input class="text" type="file" name="userfile" size="50" />

try this