PHP Send Results with ability to upload an attachment

I want to create a form that enables my website vistors to upload an attachment that will be included in the email I receive, along with various other form feilds. Can someone help me with this?

Hi bmccarthy,

Sending simple mails in php is quite easy using it’s inbuilt mail() function but dealing with attachments becomes some what difficult. For simplifying the job developers have built a phpmailer class that you can use.

Kindly go through the site http://www.askapache.com/php/phpfreaks-eric-rosebrocks-phpmailer-tutorial.html. It has a very good explanation of using phpmailer class and how to send attachments using it.

Let me know if you have any problem.

I read through the article…and urgh. :slight_smile:

This doesn’t make any sense to me:
"Sending file attachments is really easy to do. You simply add them to the attachment just like you would an address, cc, bcc or reply-to, except using the proper function. See the example below:

// Setup mail class, recipients and body
$mailer->AddAttachment(’/home/mywebsite/public_html/file.zip’, ‘file.zip’);
The AddAttachment function has four arguments:

AddAttachment(PATH_TO_FILE, FILENAME, ENCODING, HEADER_TYPE)

The PATH_TO_FILE is naturally the full path of the header you want to send. Application/octet-stream is default.

That was pretty easy! Let’s move along to using SMTP servers and utilizing diferent types of local E-mail SMTP servers."

What is that you didn’t get? Which part of the explanation? Kindly be specific.

Any of it. :slight_smile:

Here is the php I am currently using. I want to add the option of uploading an attachment. I have already completed the html for the site, but am having trouble understanding what needs to change with the php instructions to include the attachment in the email.


<?php // Subject of email sent to you. $subject = 'Website Lead'; // Your email address. This is where the form information will be sent. $emailadd = '[email protected]'; // Where to redirect after form is processed. $url = 'confirmation.htm'; // Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty. $req = '1'; $text = "Results from form:\n\n"; $space = ' '; $line = ' '; foreach ($_POST as $key => $value) { if ($req == '1') { if ($value == '') {echo "$key is empty";die;} } $j = strlen($key); if ($j >= 20) {echo "Name of form element $key cannot be longer than 20 characters";die;} $j = 20 - $j; for ($i = 1; $i <= $j; $i++) {$space .= ' ';} $value = str_replace('\n', "$line", $value); $conc = "{$key}:$space{$value}$line"; $text .= $conc; $space = ' '; } mail($emailadd, $subject, $text, 'From: '.$emailadd.''); $email = $_POST['Email']; // use their addy instead of yours $subject = 'We will be in contact with you shortly'; // change subject $text = 'Dear Business Owner, Blah blah blah'; // change text mail($email, $subject, $text, 'From: '.$emailadd.''); // send another one out echo ''; ?>

bmccarthy, did you download the PHPMailer class? Did you check examples included in the downloaded zip file? There are just few lines of codes that will let you attach file to a email messages using PHPMailer class. Just give it a try.

I downloaded and extracted it, but of the files it provides, I cannot find anything about attachments within the php files. :frowning:

Ok. Once you download file PHPMailer_v2.0.4.zip (under Downloads - PHPMailer for PHP4), and unzip it, there is a directory named ‘examples’. There you can find this simple test script:
test_mail.php
[php]<?php

include_once(’…/class.phpmailer.php’);

$mail = new PHPMailer(); // defaults to using php “mail()”

$body = $mail->getFile(‘contents.html’);
$body = eregi_replace("[]",’’,$body);

$mail->From = "[email protected]";
$mail->FromName = “First Last”;

$mail->Subject = “PHPMailer Test Subject via mail()”;

$mail->AltBody = “To view the message, please use an HTML compatible email viewer!”; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAddress("[email protected]", “John Doe”);

$mail->AddAttachment(“images/phpmailer.gif”); // attachment

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo “Message sent!”;
}

?>[/php]
You can see where they attach file to the email message in the code.

So, for your task, upon uploading file via html form, you just attach it to the message. And use PHPMailer’s Send() method instead of standard PHP function mail().

So, for this line…

$mail->AddAttachment(“images/phpmailer.gif”); // attachment

…what do I put in the paranthesis in place of “images/phpmailer.gif”?

I think in your first post you said you have a form where users can upload files?
So, here is a simple form with file upload:

[code]

Upload file:
[/code]

Here is your send.php file:
[php]<?php
if($_POST[‘submit’]){
if(is_uploaded_file($_FILES[“upload”][“tmp_name”]){
$filename = $_FILES[“upload”][“name”];
move_uploaded_file($_FILES[“upload”][“tmp_name”], ‘files/’.$filename);

  // prepare email message
  // ... see code in PHPMailer example above ...

  // attach uploaded file
  $mail->AddAttachment('files/'.$filename);

  // send message
  // ... see code in PHPMailer example above ...

}

}
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service