Sending email with attachement sometimes nt working

the mail() function

Moderators: egami, macek, gesf

Post Reply
fisma
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Jun 19, 2013 5:36 am

Wed Jun 19, 2013 6:04 am

I have code taken from http://webcheatsheet.com/php/send_email ... chment.php for sending attachements by mail. Sometimes I get a message that "Can not open files because filetype is not supported or file has been damaged.".

Here is my entire code:

<?php

//define the receiver of the email
//$to = 'xxx@xxxxx.com';
$to = 'xxx@xxxxx.com'; // note the comma

//define the subject of the email
$subject = 'You have new email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: xxx@xxxxx.com\r\nReply-To: xxx@xxxxx.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('../www/bestilling.pdf')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

V&#230r s&#229; snill, finn vedlagt fil.

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="Windows-1252"
Content-Transfer-Encoding: 7bit


<p>V&#230r s&#229; snill, finn vedlagt fil.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="bestilling.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
//echo $mail_sent ? "Mail sent" : "Mail failed";

?>

johnj
php-forum Super User
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm

Thu Jun 20, 2013 10:07 pm

what type of file are you trying to send, what is the extension?.
Is the file getting attached properly?

fisma
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Jun 19, 2013 5:36 am

Sun Jun 23, 2013 9:13 am

Full name of the file is bestilling.pdf. When I receive an email this file presents as an attachement to an email.

fisma
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Jun 19, 2013 5:36 am

Sun Jun 23, 2013 9:16 am

what is interesting for me is sometimes attachement file comes normally and I can open it. But sometimes I can not open an attachement. I don't know what may be the reason

fisma
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Jun 19, 2013 5:36 am

Mon Jun 24, 2013 1:28 pm

Can anyone give me any php script sending attachements?

johnj
php-forum Super User
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm

Tue Jun 25, 2013 9:42 pm

change this line @mail( $to, $subject, $message, $headers ); to mail( $to, $subject, $message, $headers );

The '@' suppresses any errors thrown, once you remove it any error thrown will be visible.

johnj
php-forum Super User
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm

Tue Jun 25, 2013 9:45 pm

Also - The standard MIME type for pdf files is is application/pdf. So, please change application/zip to application/pdf

fisma
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Jun 19, 2013 5:36 am

Thu Jun 27, 2013 12:42 am

Still the same error. I have added a little code to above given. This code sends a copy of the same file to my gmail email account. In gmail everything is much more worth. Email that comes from web site has no attachement. But in my corporate email Outlook shows that email has an attachement but can not open it.

fisma
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Jun 19, 2013 5:36 am

Thu Jun 27, 2013 12:44 am

I know that there is some problem with encoding file. Can anyone give me any other code that will send pdf attachements? I do not need even encoding.

johnj
php-forum Super User
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm

Fri Jun 28, 2013 10:22 pm


fisma
New php-forum User
New php-forum User
Posts: 7
Joined: Wed Jun 19, 2013 5:36 am

Sat Jun 29, 2013 2:35 am

I receive the following error message on the script that you gave me:
Parse error: syntax error, unexpected ';' in /home/28/w116444/bazaar/email.php on line 21

On line 21 I have the following code:

$name = basename($file);

What is the problem I do not understand.

johnj
php-forum Super User
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm

Thu Jul 04, 2013 12:13 am

try to print $name and see if it has a value.

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest