Jump to content

[SOLVED] GMAIL messes up my emails with PDF attachements.


Recommended Posts

Argh!

 

I put an automated email application live in work but now I've found out that messes up when it sends to GMAIL.

 

HOTMAIL and OUTLOOK work fine.

 

Problem: The email message ends up being sent as the attachment and the original attachment is nowhere to be found.

 

Is there a quick solution to this?

 

<?php 
//define the receiver of the email 
$recipientemail = mysql_real_escape_string($_POST['recipientemail']);
$to = ''.$recipientemail.''; 
//define the subject of the email 
$subject = 'Hello'; 
//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: bob@bob.com\r\nReply-To: bob@bob.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('somepdf.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

A message goes here

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

A message goes here

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

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/pdf; name="NTL_Pricing_Information.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 ? "<p><font face='arial' size='2'><center>Thank you <b>$salesagentname</b>. The desired mail and document has been sent to <b>$recipientemail</b> for <b>$recipientname</b>.</font></center></p>" : "<p><font face='arial' size='2'><center>This mail <b>did not</b> send. Please try again!</p></font></center></p>"; 
?> 

After a little research the only problem I've come across online is whether or not the mail provider is configured to recieve html email. This shouldn't be an issue because the script I'm using has a plain text alternative message set up. Also it doesn't account for the lost attachment.

 

 

 

 

 

 

Hi All,

 

Just ressurecting this thread to see if anyone knows why the script doesn't work with certain email providers.

 

I just realised that the two it did work with (outlook and hotmail) are both microsoft.

You can always look into PEAR::Mail or phpMailer, they seem to handle attachments very well.

 

Will do. I'm beginning to think that the providers are identifying my mail as spam.

 

The link below explains how PEAR can resolve the issue.

 

http://www.webkami.com/programming/php/php-pear-mail-intro/sending-pear-mail-using-php-function-error.html

 

I just don't like the idea of using huge copy and paste PHP scripts that I don't understand mostly because I don't kow how to begin editing them when I need to be specific.

You can always look into PEAR::Mail or phpMailer, they seem to handle attachments very well.

 

Will do. I'm beginning to think that the providers are identifying my mail as spam.

 

The link below explains how PEAR can resolve the issue.

 

http://www.webkami.com/programming/php/php-pear-mail-intro/sending-pear-mail-using-php-function-error.html

 

I just don't like the idea of using huge copy and paste PHP scripts that I don't understand mostly because I don't kow how to begin editing them when I need to be specific.

 

Should be plenty of examples using PEAR, but yea. If you want to understand it you can always look at the source code for it. Since it is open source.

Is PEAR a programming language? Is it popular? I have only heard of it once before & have been in college for 9 years. I have found that my college doesn't seem to offer many detailed programming classes.

 

No it is not a language. It is more like a framework. PEAR can be installed as an extension of PHP and you can download different modules from pear.php.net which are suppose to help elaborate on certain parts of PHP and have code made that does certain tasks easier without the guess work, such as the MAIL function.

 

Just allows for more functionality of PHP.

I've just downloaded the PEAR module for MIME mail but I'm not sure what to do with the file.

 

Its a *.tar file:

 

File extension TAR description:

Contains multiple files stored as one archive; created with the Unix tar program; TAR files are not automatically compressed, so they are often compressed with Gnu Zip (which creates a .GZ file). A filename that ends in ".tar.gz" is a TAR archive that has been compressed with Gnu Zip compression; sometimes the shortened .TGZ extension is used as well.

 

I work with CPANEL rather than having my computer set up as its own server.

Apparently I just upload it and activate it with:

 

include_path = ".:/usr/lib/php:/usr/local/lib/php:/home/my_user_name/pear";

 

I'm not too sure what it means by my_user_name though  ???

 

 

 

 

Well, the first script I posted (see original post) won't work with GMAIL and YAHOO MAIL.

 

Premiso's suggestion of using PEAR won't work because I can't figure out what my user name is (see prev. post).

 

I'm now trying this script though the email message won't send:

 

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

$email_from = "info@email.com"; // Who the email is from 
$email_subject = "2nd Script Test"; // The Subject of the email 
$email_txt = "Hopefully this second script works."; // 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 anyone help me with this?

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.