Custom images on notifications email, and the way to pass the src URL for IMG attachments

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
User avatar
axe70
Registered User
Posts: 134
Joined: Sun Nov 17, 2002 10:55 am
Location: Italy
Contact:

Custom images on notifications email, and the way to pass the src URL for IMG attachments

Post by axe70 » Thu Feb 14, 2019 3:28 pm

Hello cool guys.
With some short and custom code mods, i modified the phpBB to send out notification emails in html format to users, so also img attachments are processed as part of the email html body.

All ok, especially for only text mode, where all bbcode removed, not parsed (so not passed to the generate_text_for_display() function), but just cleaned to be pure text.
All ok also in html, except that unfortunately in the html mode, when i add an img, i note this problem (this is gmail email output for example):

Code: Select all

<a href="https://mysite.com/forum/download/file.php?id=8720&amp;t=18705" target="_blank" data-saferedirecturl="https://www.google.com/url?q=https://mysite.com/forum/download/file.php?id%3D8720%26t%3D18705&amp;source=gmail&amp;ust=1550227055781000&amp;usg=AFQjCNHv__tgsnawRUhyKN3kOMJwbEo2Kg"><img src="https://ci4.googleusercontent.com/proxy/nrSZIcDx-onXERL8TJQUL04J9nNzwpiG5_MNku-pCr48heWLqWTXfLMwi58NrhIiExBTM6leBNe0fbfywPUu7WBoHUFodReRSKAScQ=s0-d-e1-ft#https://mysite.com/forum/download/file.php?id=8720&amp;t=18705" width="100" height="100" alt="testerq" class="CToWUd"></a>
The problem not exists, if the url/src that point to img or a href are directly pointing to some image, the image in this case is correctly parsed and display (ex: http://mysite.com/myimage.jpg)
While the proxy error on google come out if i pass the way phpBB require to download and display the file, so via:
myboard.com/download/file.php?id .... etc
any hint?
Should i retrieve the image url and edit the htaccess (assuming that to retrieve absolute url to attachment image is equally possible like for avatars) ...
wrong in something? ... thanks!

User avatar
axe70
Registered User
Posts: 134
Joined: Sun Nov 17, 2002 10:55 am
Location: Italy
Contact:

Re: Custom images on notifications email, and the way to pass the src URL for IMG attachments

Post by axe70 » Thu Feb 14, 2019 5:02 pm

p.s looking to the joke ... ah!
i was thinking images archived in some strange mode, instead, these are just native images files, with a filename and no extension.
But it is sufficient to add the right extension, you can retrieve the file directly via image url and not via phpBB download/file.php?...
Like so: http://mysite.com/files/4837378rt48rbgc8vrvgcv.jpg
If i'm not wrong, in this way this should work, not still in time to test but should ... Isn't it?
Cheers to all nice cool people!

User avatar
canonknipser
Registered User
Posts: 2096
Joined: Thu Sep 08, 2011 4:16 am
Location: Germany
Name: Frank Jakobs
Contact:

Re: Custom images on notifications email, and the way to pass the src URL for IMG attachments

Post by canonknipser » Thu Feb 14, 2019 5:58 pm

But if you at a file extension to the files in the files-folder , the normal mechanism for attachments won't work any longer. So you can't simply add extensions to those files in the files-folder.

Your base problem is problem with the img-tag and it's interpretation in the browsers. In a old-fashioned CP/M-era-based mechanism they don't use any meta information (eg. mime-type) or file content (eg. mime-magic-methods), but just the file's extension.
Best way to resolve this issue is to copy the image into a separate folder (a subfolder in the store folder would be a good place) and add the proper extension based on the mimetype or use a similar mechanism like in my script: Knowledge Base - Backing up attachments with their original filenames to give them a unique filename you can use for your mails.
Greetings, Frank
phpbb.de support team member
English is not my native language - no support via PM or mail
New arrival - Extensions and scripts for phpBB

User avatar
axe70
Registered User
Posts: 134
Joined: Sun Nov 17, 2002 10:55 am
Location: Italy
Contact:

Re: Custom images on notifications email, and the way to pass the src URL for IMG attachments

Post by axe70 » Thu Feb 14, 2019 6:00 pm

But if you at a file extension to the files in the files-folder , the normal mechanism for attachments won't work any longer. So you can't simply add extensions to those files in the files-folder.
but in this case there is no need to add/modify nothing:
i just need the db row, that i have result of into attachments array, with encoded filename, and file extension. So you can build the url to it on fly.
Isn't it?

P.s it is custom code that not use any external mod
[EDITED]

User avatar
canonknipser
Registered User
Posts: 2096
Joined: Thu Sep 08, 2011 4:16 am
Location: Germany
Name: Frank Jakobs
Contact:

Re: Custom images on notifications email, and the way to pass the src URL for IMG attachments

Post by canonknipser » Thu Feb 14, 2019 6:24 pm

got it - you will handle it via a rewrite-rule in .htaccess? So use the file id (8720) and build a url like http://example.com/mail-img-8720.jpg
In .htaccess you rewrite the url to http://example.com/download/file.php?id=8720 and phpBB will handle the attachment in the usual way.
Greetings, Frank
phpbb.de support team member
English is not my native language - no support via PM or mail
New arrival - Extensions and scripts for phpBB

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51876
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Custom images on notifications email, and the way to pass the src URL for IMG attachments

Post by Brf » Thu Feb 14, 2019 6:37 pm

By default the htaccess blocks access to that files folder, that way nobody can access any of the files without going through phpBB's permissions system.

User avatar
axe70
Registered User
Posts: 134
Joined: Sun Nov 17, 2002 10:55 am
Location: Italy
Contact:

Re: Custom images on notifications email, and the way to pass the src URL for IMG attachments

Post by axe70 » Thu Feb 14, 2019 6:39 pm

... to be honest i was in the way to reply to say that i've got what you mean.
But yes, it's right the one you say.
Yes this can be a good solution with htaccess, or a more raw solution could also be to duplicate the attachment and assigning to it the file ext, so the file will be found when after requested because there is a copy of it with right extension (may into a custom folder that can be purged after some while, since email messages are normally deleted, while posts and attachments will be in the forum's /files folder). And also this can be done within a line of php code. The duplication of the file, will happen when notification email fire.
The htaccess seem to be the right one, but the second require few lines of code, and not break nothing.

[EDITED]

User avatar
axe70
Registered User
Posts: 134
Joined: Sun Nov 17, 2002 10:55 am
Location: Italy
Contact:

Re: Custom images on notifications email, and the way to pass the src URL for IMG attachments

Post by axe70 » Fri Feb 15, 2019 8:29 am

work fine: just to leave an idea to who may never interested to this ... so somewhere, on
/phpbb/notification/type/post.php
there is something like this (in place of custom http://, may imagine to use generate_board_url()
consider $postattachary is an array containing all attachments, that has been previously filled with needed attachments that belong to this topic:

Code: Select all

$posttext = generate_text_for_display($posttext, '', '', '', true);
if($postattachment > 0){
		$imgtypes = array("gif", "jpg", "jpeg", "png", "svg", "bmp");
foreach($postattachary as $paa){
	$filew = './files/'.$paa['physical_filename'];
  $newfilew = './temp-mail-attachments/'.$paa['physical_filename'].'.'.$paa['extension'];
  $newfileu = 'temp-mail-attachments/'.$paa['physical_filename'].'.'.$paa['extension'];
  
	// check if is an image
if (in_array($paa['extension'], $imgtypes)) {
if (!copy($filew, $newfilew)) {
	// may let go
}
	$posttext = str_replace($paa['real_filename'], '<a href="https://mysite/forum/'.$newfileu.'"><img src="https://mysite/forum/'.$newfileu.'" style="width:200px;height:200px;" width="200" height="200" alt="testerwq" /></a><br><a href="https://mysite/forum/download/file.php?id=8633&t=18510&mode=view">Open image</a>', $posttext);
}	else { 
	if (!copy($filew, $newfilew)) { // copy the file to be available for direct calls
	// may let go
  }
	$posttext = str_replace($paa['real_filename'], '<br><a href="https://mysite.com/forum/download/file.php?id='.$paa['attach_id'].'">Open file</a>', $posttext);
}
//parse_attachments($postforumid, $posttext, $postattachary, $up=false);
}
// using parse_attachments? just a link to open image and no preview? then ...
//$posttext  = str_replace('href="./', 'href="https://mysite.com/forum/', $posttext);

$w3FSI = new FilesystemIterator('./temp-mail-attachments/');
$w3now = time();
foreach ($w3FSI as $w3file) {
    if ($w3now - $w3file->getCTime() >= 60 * 60 * 24 * 7) // 7 days older will be removed: or whatever you like
        unlink('./w3ImagesTempFolder/'.$w3file->getFilename());
}

}
// switch to html email header
define("w3SendAsHtml",true); ...
Easy (and may not useful) explain:
These duplicated images files will be removed from the server following these rules:
after 7 days, as soon an user submit an attachment
so: any time an attachment posted within a post on forum, then, this check is executed, and all files older then 7 days removed/clened up from this temporary folder.
If the email will be read after 7 days by the notification date, the image will fail to load into email, because may removed on server: by the way a link Open image will ever display, to open the real post’s attachment image.

[pseudo code EDITED]

Post Reply

Return to “phpBB Custom Coding”