Jump to content

Add Attachments to my Email


Joob
 Share

Recommended Posts

Hello guys,
 
I'm having trouble receiving attachments in email ..
When I send an attachment, it says "attachment: file1.jpg" or "attachment: file.rar" all ok .. but when I click on it, the link is not correct, and gives me a link with a huge extension.
 
Next function
$has_attachment = preg_match_all("/multipart\/mixed/", $email_body, $attachment);
if ($op_getKey) {
	if ($has_attachment) {
		$mailParts = explode("--" . $getKey[1][1], $email_body);
		$attachParts = explode("--" . $getKey[1][0], $email_body);
	
		$get_attach_name=preg_match_all("/filename=\"(.*?)\"/", $attachParts[2], $attach_name);
		$get_attach_type=preg_match_all("/Content-Type: (.*?);/", $attachParts[2], $attach_type);
		$pure_code = explode("\n\n",$attachParts[2]);
		$pure_code = str_replace("\r","",$pure_code[1]);
		$pure_code = str_replace("\n","",$pure_code);
	}else{
		$mailParts = explode("--" . $getKey[1][0], $email_body);
	}

And I'm making the call that way

<a target="_blank" href="data:<?=$attach_type[1][0]?>;base64,<?=$pure_code?>"> <?=$attach_name[1][0]?> </a>

I have this code in my another file (attachment.php)

<?php
if (isset($_GET['attachment'])) {
	

}

?>

Regards

 

 

Link to comment
Share on other sites

You're saying that the email does no have the attachment when it arrives?  Are you using the PHP mail() function to send it?  Why not show us the mail portion instead of whatever that php code is trying to do?

 

PS - sending an att. with the mail() function is pretty hard.  Try switching to PHPMailer.  Much easier to use and fairly simple to install.

Link to comment
Share on other sites

Thank for ur reply.

 

I am using email pipe, with SQL based.

 

I'm using it like this

<?php
$id = $_POST['id'];
$pdo = new PDO("sqlite:mydb.db");
$email = $pdo->query("SELECT * FROM `emails` WHERE `id`='$id'")->fetch(PDO::FETCH_ASSOC);

$email_body = base64_decode($email['message']);

$letter_html = "";
//$op_getKey = preg_match_all("/Content-Type: multipart\/alternative; boundary=\"(.*?)\"/", $email_body, $getKey);
//$op_getKey = preg_match_all("/alternative; boundary=\"(.*)\"/", $email_body, $getKey);
$op_getKey = preg_match_all("/boundary=\"(.*)\"/", $email_body, $getKey);
//$has_attachment = preg_match_all("/multipart\/mixed/; boundary=\"(.*?)\"/", $email_body, $attachment);
$has_attachment = preg_match_all("/multipart\/mixed/", $email_body, $attachment);
if ($op_getKey) {
	if ($has_attachment) {
		$mailParts = explode("--" . $getKey[1][1], $email_body);
		$attachParts = explode("--" . $getKey[1][0], $email_body);
	
		$get_attach_name=preg_match_all("/filename=\"(.*?)\"/", $attachParts[2], $attach_name);
		$get_attach_type=preg_match_all("/Content-Type: (.*?);/", $attachParts[2], $attach_type);
		$pure_code = explode("\n\n",$attachParts[2]);
		$pure_code = str_replace("\r","",$pure_code[1]);
		$pure_code = str_replace("\n","",$pure_code);
	}else{
		$mailParts = explode("--" . $getKey[1][0], $email_body);
	}
	 	//$letter_text = str_replace('Content-Type: text/plain; charset="UTF-8"', '', $mailParts[1]);
		//echo $letter_text;

	$the_mail = $mailParts[2];


	$to_replace = array(
		'Content-Type: text/html; charset="UTF-8"',
		'Content-Type: text/html; charset=UTF-8',
		'Content-Type: text/html; charset="utf-8"',
		'Content-Type: text/html; charset=utf-8',
		'Content-Type: text/html; charset="iso-8859-1"',
		'Content-Type: text/html; charset=iso-8859-1',
		'Content-Type: text/html; charset="ISO-8859-1"',
		'Content-Type: text/html; charset=ISO-8859-1',
		'Content-Type: text/plain; charset="iso-8859-1"',
	);

	foreach ($to_replace as $k => $v) {
		$the_mail = str_replace($to_replace[$k], '', $the_mail);
	}
	
	  //$the_mail = str_replace('Content-Type: text/html; charset="UTF-8"', '', $the_mail);
	  //$the_mail = str_replace('Content-Type: text/html; charset=UTF-8', '', $the_mail);
	  //$the_mail = str_replace('Content-Type: text/html; charset="utf-8"', '', $the_mail);
	  //$the_mail = str_replace('Content-Type: text/html; charset=utf-8', '', $the_mail);
	  //$the_mail = str_replace('Content-Type: text/html; charset="iso-8859-1"', '', $the_mail);
	  //$the_mail = str_replace('Content-Type: text/html; charset=iso-8859-1', '', $the_mail);
	  //$the_mail = str_replace('Content-Type: text/html; charset="ISO-8859-1"', '', $the_mail);
	  //$the_mail = str_replace('Content-Type: text/html; charset=ISO-8859-1', '', $the_mail);


	if (preg_match_all("/Content-Transfer-Encoding: quoted-printable/", $the_mail, $mailToDecode)) {
		$message_to_decode = str_replace('Content-Transfer-Encoding: quoted-printable', '', $the_mail);
		$letter_html = quoted_printable_decode($message_to_decode);
	}elseif(preg_match_all("/Content-Transfer-Encoding: base64/", $the_mail, $mailToDecode)){
		$message_to_decode = str_replace('\r', '', $the_mail);
		$message_to_decode = str_replace('\n', '', $message_to_decode);
		$message_to_decode = str_replace('Content-Transfer-Encoding: base64', '', $message_to_decode);
		$letter_html = base64_decode($message_to_decode);
		// $letter_html = $message_to_decode;
	} else {
		$letter_html = $the_mail;
	}
	// echo $letter_html;
} else {


	$mailParts = explode("\n\n", $email_body);



	foreach ($mailParts as $k => $v) {
		if ($k > 0) {
			$letter_html .= $v . "\n\n";
		}
	}

	if(preg_match_all("/Content-Transfer-Encoding: base64/", $email_body, $mailToDecode)){
		$message_to_decode = str_replace('\r', '', $letter_html);
		$message_to_decode = str_replace('\n', '', $message_to_decode);
		$letter_html = base64_decode($message_to_decode);
		
		// $letter_html = $message_to_decode;
	}
	
}
?>

And I call it like this

<strong><?=$lang[$xlang][5]?></strong>: <a target="_blank" href="https://site.com/<?=$attach_type[1][0]?>;base64,<?=$pure_code?>"><?=$attach_name[1][0]?></a>

When I send an email to my website to receive the email, the anex doesn't work (the link) on my website.. can not download..

Edited by Joob
Link to comment
Share on other sites

Ok - you have a script that is receiving piped emails.  I get that.  So - what does your script do with this email?  Attempt to read thru it line by line?  Are you using your very own code or are you using some kind of class or external processor to gather certain data from it?  And what is 'anex'?  Do you know how to handle an email that has attachments?  Very complex for a beginner.  You should probably read up on the structure of an email if that is the case.

 

One thing you could do would be to read the email and compose a simple text file of what you are getting and save it.  Then you could look at the text file to see how you did.  Plus you could add stuff to that text output to help you debug what your script is doing for you.

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.