Need Help to Send Email with Attachments

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Need Help to Send Email with Attachments

Postby creacon » 14. January 2010 16:49

I'm trying to find a way to send email - via encrypted SSL - with attachments. I tried a product called PHPMailer, which seemed like a good solution. Unfortunately it requires at least php 5, and my web host, Yahoo, is using php 4.3.11. I saw some responses from Izzy regarding Pear, so I looked it up on the XAMPP console, and was totally confused about how to even begin to use it.

I found some Pear example code for what I want to do in one of the W3Schools tutorials, but when I copied it to my page and tried to execute it, nothing happened; so obviously I must be missing something but have not clue what.

My website is a questionnaire that when filed out and submitted, it gets converted to .pdf's (which I have working now). I need to send those .pdf's as attachments to another company for processing, and have to use encrypted email because they contain sensitive/private information. I have my Yahoo business email set up for SSL (using smtp port 465), and I can successfully send simple emails (using the php mail() function) via that encrypted transport. What I need now is a way to include attachments.

Could/would someone please please help me with this? :?
Posts: 36
Joined: 21. August 2009 19:14

Re: Need Help to Send Email with Attachments

Postby Izzy » 14. January 2010 20:10

Bob, sending file attachments in PHP is related to headers and mime content types.

You have already set up the TLS sendmail part of your requirement.

This Advanced email in PHP tutorial may help you greatly and page 5 starts the information for File Attachments with a script you can download, that I have not checked out:

The rest of the tut is also very useful and worth keeping a bookmark.

Go to this secure search engine and type in the keyword box how to send attachments in php mail for many useful results including the above tutorial:

Please let me know if you manage to get a handle on sending attachments via a php script.
Thanks and good luck.
Posts: 3344
Joined: 25. April 2006 17:06

Re: Need Help to Send Email with Attachments

Postby creacon » 15. January 2010 12:45

Thanks Izzy for your response, and I be's a workin' on it, but thanks to my extremely limited web experience, nearly everything I read provides more questions than answers, and I can't figure out where to look for more answers. I think his explanations presume much more knowledge & experience than I currently have.

Conceptually, I could follow the tutorial - more or less (mostly less) - and I downloaded the test script, but much of the code detail, I couldn't understand. The test script worked with a form that's included in the download so here are some of my confusions:
Code: Select all
// Read POST request params into global vars
$to      = $_POST['to'];
$from    = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];

This part is fine; I've done that throughout my pages. The next segment, however, poses questions:
Code: Select all
// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
  // Read the file to be attached ('rb' = read binary)
  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));

I'm not sure if this is necessary in my case since the .pdf's are generated from my pages using a product called FPDF. It allows the .pdf's to be sent to; the browser, a local file (on the server), download them or return them as strings. Currently, for testing purposes, I'm sending them to the browser, but for the final product, I'm not sure which w/b the best to use.
Next confusion;
Code: Select all
  // Generate a boundary string
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

What is a boundary string, and would I just use this as is or will it be necessary to modify it for my use? If so, how/what?
Now, more questions
Code: Select all
  // Add the headers for a file attachment
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";
Do I ALWAYS just use this as is? Is the version ALWAYS 1.0?
Next segment:
Code: Select all
  // 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";

I sort of think that the above would be used more or less as is (even though I don't understand the meaning of the values - even the comment is confusing to me), except maybe for the text in the first line. What I'm curious about is the last line. It looks like he's adding lines 2 - 4, then appending the value of the variable itself. I do that when I want to add data at the beginning of a variable that already has a value assigned, but I don't see anywhere above that segment where $message is being assigned a value.
Next is:
Code: Select all
  // Base64 encode the file data
  $data = chunk_split(base64_encode($data));
Would I need this at all, since my attachment(s) is/are .pdf and not binary?
Then finally:
Code: Select all
  // 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" .
Won't this replace the values previously loaded into $message? Why are those two lines commented out, and will they be included in the value of $message? If I don't use the "$dat = ..." segment, would I leave out the next-to-last line above, or replace it with something else, and if so, what?

I realize this is asking a lot, but If you could enlighten me about this, I'd be very grateful. I'm getting dangerously close to my deadlines and this is the only part of my project that isn't working.
Posts: 36
Joined: 21. August 2009 19:14

Re: Need Help to Send Email with Attachments

Postby creacon » 15. January 2010 16:44

Ignore the above, and please accept my apoloyies for overreacting. I managed to make the sample script work for me, so now everything's pretty much OK.
Posts: 36
Joined: 21. August 2009 19:14

Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 44 guests