alicia1234
asked on
Need help sending email attachment
The code below works fine on my local pc (localhost) - which is Windows XP SP3 with IIS.
When I move it to my hosted site, which is UNIX/Apache, it doesn't work. The email is received, but there is no attachment, and the text of the email shows all of the boundaries and the code for the file. See my next post for what it looks like.
When I move it to my hosted site, which is UNIX/Apache, it doesn't work. The email is received, but there is no attachment, and the text of the email shows all of the boundaries and the code for the file. See my next post for what it looks like.
<?php
$fileatt = 'attachments/Invoice-20090415.pdf'; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = 'Invoice-20090415.pdf'; // Filename that will be used for the file as the attachment
$to = "alicia@cookingwithalicia.com";
$subject = "Testing email attachment from remote host";
$headers = 'From: info@localhost.com' . "\n";
$headers .= 'Cc: alicia@orangemooncreations.com' . "\n";
$body = "Here is the file" . "\n\n";
$file = fopen($fileatt,"rb");
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$body .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$body . "\n\n";
$data = chunk_split(base64_encode($data));
$body .= "--{$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" .
"--{$mime_boundary}--\n";
$ok = @mail($to, $subject, $body, $headers);
if($ok) {
echo "<font face=verdana size=2>The file was successfully sent!</font>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I know. ;-( But I'm not that experienced with PHP and I looked at PHPMailer and it looked too complicated for me to pick up right now.
ASKER
But maybe you could get me started? For example, I see in the first link a "require_once" for class.phpmailer.php" - where/how do I get that ?
you get the class by clicking the download link on the left of the example page; or simpler, go here:
http://sourceforge.net/projects/phpmailer/files/
then choose the version according to which PHP you are running
http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5%2F6/PHPMailer_v5.0.2.zip/download
is the latest version for PHP 5 / 6
you might also want to look at this:
http://phpmailer.worxware.com/index.php?pg=install
and
http://phpmailer.worxware.com/index.php?pg=tutorial
for a full tutorial.
HTH
http://sourceforge.net/projects/phpmailer/files/
then choose the version according to which PHP you are running
http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5%2F6/PHPMailer_v5.0.2.zip/download
is the latest version for PHP 5 / 6
you might also want to look at this:
http://phpmailer.worxware.com/index.php?pg=install
and
http://phpmailer.worxware.com/index.php?pg=tutorial
for a full tutorial.
HTH
ASKER
I downloaded PHPMailer for PHP5/6 and extracted the files. I see that there are three .php files. Do I simply move those into my website and use them per the example?
yep pretty much. It's a relatively straight forward setup. Just make sure you are including with the correct path to include the phpmailer class file.
have a go with the tutorial... it really is quite straight forward once you've done that...
have a go with the tutorial... it really is quite straight forward once you've done that...
ASKER
Thanks. I can't work on this until later tonite or tomorrow. Will post back then. Hoping that, in the end, it would have been a lot quicker for me to use PHPMailer in the first place.
I haven't used classes before. Would you just explain to me what the - > syntax is? Thanks!
I haven't used classes before. Would you just explain to me what the - > syntax is? Thanks!
the -> syntax shows that you are calling a function (method) or accessing a variable (property) in the class.
there's plenty of info on these basics in the PHP manual pages, such as here:
http://us.php.net/manual/en/language.oop5.basic.php
there's plenty of info on these basics in the PHP manual pages, such as here:
http://us.php.net/manual/en/language.oop5.basic.php
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Please note that I did ask to have points awared to basic612 anyways for pointing me to PHPMailer and supporting help and documentation. I'll take a look at it when I have more time.
ASKER
I tried changing some of the "\n" to "\r\n" but I honestly do not understand what I'm doing so I'm just grasping at straws.
Open in new window