Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Sending email with attachement sometimes nt working

the mail() function

Moderators: macek, egami, gesf

Sending email with attachement sometimes nt working

Postby fisma » Wed Jun 19, 2013 6:04 am

I have code taken from http://webcheatsheet.com/php/send_email_text_html_attachment.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";

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

Re: Sending email with attachement sometimes nt working

Postby johnj » 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?
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Sending email with attachement sometimes nt working

Postby fisma » 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

Re: Sending email with attachement sometimes nt working

Postby fisma » 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

Re: Sending email with attachement sometimes nt working

Postby fisma » Mon Jun 24, 2013 1:28 pm

Can anyone give me any php script sending attachements?
fisma
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Jun 19, 2013 5:36 am

Re: Sending email with attachement sometimes nt working

Postby johnj » 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: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Sending email with attachement sometimes nt working

Postby johnj » 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
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Sending email with attachement sometimes nt working

Postby fisma » 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

Re: Sending email with attachement sometimes nt working

Postby fisma » 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.
fisma
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Jun 19, 2013 5:36 am

Re: Sending email with attachement sometimes nt working

Postby johnj » Fri Jun 28, 2013 10:22 pm

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

Re: Sending email with attachement sometimes nt working

Postby fisma » 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.
fisma
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Jun 19, 2013 5:36 am

Re: Sending email with attachement sometimes nt working

Postby xiaomingzc » Mon Jul 01, 2013 11:31 pm

If you ask someone to tell you information about buy diablo 3 Gold someone they just met, they usually begin with explaining their sight. One of the most famous functions on a experience, for females and men as well, your sight set the overall tone of your complete overall look. Because of this, females get trapped over Cheap WOW Gold keeping the most ideal look with several cosmetics. While lashes and eye make-up may be the celebrities presenting in that area, your eye-brows play a key part in personality, overall look and overall look. Items like temple gel, help to Runescape Gold maintain that smooth, groomed look while guaranteeing those persistent rebellious locks are managed.
xiaomingzc
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Jul 01, 2013 11:22 pm

Re: Sending email with attachement sometimes nt working

Postby johnj » Thu Jul 04, 2013 12:13 am

try to print $name and see if it has a value.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to PHP coding => Mail

Who is online

Users browsing this forum: Exabot [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.