Jump to content

[SOLVED] PDF Email Attachment working fine but message doesn't send.


webmaster1

Recommended Posts

Hi All,

 

I'm using a basic chunk of code to send an email with an attached PDF file.

 

The problem is email message isn't being sent.

 


<?php 
$fileatt = "cat_scan.pdf"; // Path to the file 
$fileatt_type = "application/octet-stream"; // File Type 
$fileatt_name = "cat_scan.pdf"; // Filename that will be used for the file as the attachment 

$email_from = "test@email.com"; // Who the email is from 
$email_subject = "Test subject"; // The Subject of the email 
$email_txt = "This is a test email."; // Message that the email has in it 

$email_to = "someone@hotmail.com"; // Who the email is too 

$headers = "From: ".$email_from; 

$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}\""; 

$email_message .= "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" . 
$email_message . "\n\n"; 

$data = chunk_split(base64_encode($data)); 

$email_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" . 
"--{$mime_boundary}--\n"; 

$ok = @mail($email_to, $email_subject, $email_message, $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!"); 
} 
?>

 

Can anybody spot whats causing the problem? I can't see where $email_txt is being called on again.

 

 

Link to comment
Share on other sites

Ok change this

 

$ok = @mail($email_to, $email_subject, $email_message, $headers);

 

to this;

 

<?php
echo '1: '.$email_to.'<br />';
echo '2: '.$email_subject.'<br />';
echo '3: '.$email_message.'<br />';
echo '4: '.$headers.'<br />';
exit();
$ok = @mail($email_to, $email_subject, $email_message, $headers);
?>

 

And post what's printed on the screen

Link to comment
Share on other sites

Its longwinded:

 

1: rexusdiablos@hotmail.com
2: Test subject
3: This is a multi-part message in MIME format. --==Multipart_Boundary_xc2c0dcfd268b95472ca3f644ca7b73e9x Content-Type:text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit --==Multipart_Boundary_xc2c0dcfd268b95472ca3f644ca7b73e9x Content-Type: application/octet-stream; name="cat_scan.pdf" Content-Transfer-Encoding: base64 JVBERi0xLjQNJeLjz9MNCjQgMCBvYmogPDwvTGluZWFyaXplZCAxL0wgMTM4MjcyL08gNi9FIDc5 MTE0L04gMS9UIDEzODE1MS9IIFsgNjM2IDE1Nl0+Pg1lbmRvYmoNICAgICAgICAgICAgICAgICAg IA14cmVmDTQgMTcNMDAwMDAwMDAxNiAwMDAwMCBuDQowMDAwMDAwNzkyIDAwMDAwIG4NCjAwMDAw MDA4NTEgMDAwMDAgbg0KMDAwMDAwMTM2MCAwMDAwMCBuDQowMDAwMDAxNjgyIDAwMDAwIG4NCjAw MDAwMDI3NDIgMDAwMDAgbg0KMDAwMDAwMzcxMiAwMDAwMCBuDQowMDAwMDA0NjQ0IDAwMDAwIG4N 

[i'VE EDITED OUT A COUPLE OF HUNDRED LINES HERE BECAUSE IT WAS EXCEEDING THE CHARACHTER LIMIT TO POST]

NScwMCcpL1RpdGxlKGNhdF9zY2FuKT4+DWVuZG9iag14cmVmDTAgNA0wMDAwMDAwMDAwIDY1NTM1 IGYNCjAwMDAwNzkxMTQgMDAwMDAgbg0KMDAwMDA3OTE2NCAwMDAwMCBuDQowMDAwMTM3OTczIDAw MDAwIG4NCnRyYWlsZXINPDwvU2l6ZSA0Pj4Nc3RhcnR4cmVmDTExNg0lJUVPRg0= --==Multipart_Boundary_xc2c0dcfd268b95472ca3f644ca7b73e9x-- 
4: From: someone@email.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_xc2c0dcfd268b95472ca3f644ca7b73e9x"

Link to comment
Share on other sites

try this

 

<?php 
$fileatt = "cat_scan.pdf"; // Path to the file 
$fileatt_type = "application/octet-stream"; // File Type 
$fileatt_name = "cat_scan.pdf"; // Filename that will be used for the file as the attachment 

$email_from = "test@email.com"; // Who the email is from 
$email_subject = "Test subject"; // The Subject of the email 
$email_txt = "This is a test email."; // Message that the email has in it 

$email_to = "someone@hotmail.com"; // Who the email is too 

$headers = "From: ".$email_from; 

$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}\""; 

$email_message .= "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" . 
$email_message . "\n\n"; 

$data = chunk_split(base64_encode($data)); 

$email_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" . 
"--{$mime_boundary}--\n" . $email_txt; 

$ok = @mail($email_to, $email_subject, $email_message, $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!"); 
} 
?>

Link to comment
Share on other sites

That worked perfectly with very little modification required.

 

Thanks G.

 


<?php 
//define the receiver of the email 
$to = 'youraddress@example.com'; 
//define the subject of the email 
$subject = 'Test email with attachment'; 
//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: webmaster@example.com\r\nReply-To: webmaster@example.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('attachment.zip'))); 
//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

Hello World!!! 
This is simple text email message. 

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

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

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

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/zip; name="attachment.zip"  
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"; 
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.