Email Attachments

Good morning,

I’ve spent days and days trying to get this to work, and have searched the threads trying this and that and I’m still missing something here. I’m trying to get a form built, that when completed and submitted, sends two emails - one to one group of folk, and another email to another group. Main reason for this is because one group is not allowed to see part of the data being submitted. On both emails however, I need to include the picture that the person filling out the form has attached.

I read that it’s better practice to add to the database, a link to the image verse having the image saved in the database…and I’ve tried both ways for the email attachment to be included but failed on them all. I’m deploying to HostGator if that makes any difference and the emails go out fine to the groups they need to go to, but the image is never there… I’ve even included a hard link in the email to an image I know is there (I can open up the link in my browser and see the image) but the attachment is still not successful.

Here is my code edited a bit for privacy so hopefully I didn’t mess that up - all on event “OnAfterInsert”:

===============================
[rptid] = {Rcrdid};
$mail_message = NULL;

// Get details of new submission
$check_sql = “SELECT added, name, image FROM reports WHERE rcrdid = [rptid]”;
sc_lookup(rp, $check_sql);
[rp_added] = {rp[0][0]};
[rp_name] = {rp[0][1]};
[image] = {rp[0][3]};

// Get email settings for sending mail
sc_lookup(em, “SELECT smtp_server, smtp_user, mail_from, smtp_pwd, mail_to_attorney, mail_to_board FROM mail_settings WHERE type = ‘main’”);
[server] = {em[0][0]};
[user] = {em[0][1]};
[from] = {em[0][2]};
[pwd] = {em[0][3]};
[sendto] = {em[0][4]};

// Set image path - directory
[fp] = “…/_lib/file/img/”;

// ** Send email notification
// Email parameters
$mail_smtp_server = “[server]”; // SMTP server name or IP address
$mail_smtp_user = “[user]”; // SMTP user name
$mail_smtp_pass = “[pwd]”; // SMTP password
$mail_from = “[from]”; // From email
$mail_to = “[sendto]”; // To email
$mail_subject = "New Submission: " . [rptid]; // Message subject
$mail_message = $mail_message.‘The following information was just submitted from the site: <br><br>’; // Message body
$mail_message = $mail_message.'Reported date: ’ . date(“l, jS \of F Y”,strtotime([rp_added])) . ‘<br>’;
$mail_message = $mail_message."Individual reporting: " . [rp_name] . “<br>” ;
$mail_message = $mail_message.'Thank you. ’ . ‘<br>’;
$mail_format = ‘H’; // Message format: (T)ext or (H)tml
$mail_attachments = ‘[fp].[image]’;

// Send email";
sc_mail_send($mail_smtp_server,
$mail_smtp_user,
$mail_smtp_pass,
$mail_from,
$mail_to,
$mail_subject,
$mail_message,
$mail_format,
‘’,
‘’,
‘’,
$mail_attachments);

Then I clear it the message out with
$mail_message = NULL;
Before doing a similar action for the second email.

I’ve tried so many different variations that I’ve lost track of them all, but I must be missing something somewhere and hopefully it’s an easy catch for someone else?

Thanks very much for any help you’re able to provide.

Mark.

Hey Mark:

My Recomendation… do an echo of all your variables to screen, tbh with you this I dont like and i think is the main problem you have.


$mail_attachments = '[fp].[image]'; <--- this works only with double quotes ""

//this should
$mail_attachments = [fp] . [image];

That line bugs me… try echoing the variables to see if they are being set propperly.

Regards.

Hey Mark:

My Recomendation… do an echo of all your variables to screen, tbh with you this I dont like and i think is the main problem you have.


$mail_attachments = '[fp].[image]'; <--- this works only with double quotes ""

//this should
$mail_attachments = [fp] . [image];

That line bugs me… try echoing the variables to see if they are being set propperly.

Regards.

Hey kafecadm,

Thanks SO much for your feedback. It at least tells me I’m on the right track. Unfortunately I still can’t get it to work - I created a control for the form to jump to after submitting the picture, and it’s echoing the path I’m trying to use - but no picture attachment. I’ve tried so many different variations but can’t get it to go through…for example:

…/reports/_lib/file/img/
…/_lib/file/img/
/_lib/file/img/

and when I echo it, I’m getting what my path I’m trying is - for example on the middle one, my control echoes:
…/_lib/file/img/1.png

and on all my tests, no image is attached to the email. I’ve confirmed the images are being saved to _lib/file/img/ - I can see them. And as I mentioned in my first post, I can put a hard link into the form to pull the web link I can copy/paste, and the image is still not being included. I’m going to keep banging away and either my fatigue or my sanity will finally give in =)

Thanks though - I know I’m at least close…

I put the whole web address as the [fb] and ran it. Then copy/pasted the echo right into my browser and it pulls up the image - but still no attachment. It’s almost like there’s a setting that’s not allowing the image to be added to the email… aargh.

oh man that explains it… try not to use relative paths…" …/folder/otherfolder/" use absolute paths “myuser/home/scriptcase/someFolder/img/1.png”, this is common working with files. try that and lets c how it goes.

Regards

Hey kafecadm,

Thanks again for your assistance on this. Didn’t want you to think I wasn’t working on it so quick update. I started with the farthest out link:

/public_html/reports/_lib/file/img

and am now copy / pasting the path I use into the form so I have it in the email for tracking’s sake =)

That didn’t work,

So I tried with out the first ‘/’

public_html/reports/_lib/file/img

That didn’t work so I took out public_html… and so on. I’m going to remove one item at a time until theoretically the image will come through…I hope. Problem now is hostgator is not sending the email right away, so I’m having to wait 30 - 60 minutes between attempts.

I’ll definitely let you know how it turns out - what works e.t.c.

Thanks again for your help on this. It’s truly very much appreciated! I’m doing it for free for a non-profit, and have spent around 12 hours on this little piece alone. The rest of the form was super easy! LOL

Cheers.

Well lemme know if I can be of any assistance.

Regards

Hi kafecadm,

I’m at a loss - I’ve now spent weeks on this and must be missing the boat somewhere. Maybe it’s the database field type (blob)? If I create a form to review the records I’ve added, I can see the images - so I’m assuming the upload and field type are working fine…but none of my paths worked and I’ve sent literally hundreds of emails trying different things.

Tested with image directly in path

…/…/_lib/file/img/1.png
_lib/file/img/1.png

or as variable at the end of these paths:

/
img/
/img/
file/img/
/file/img/
_lib/file/img/
/_lib/file/img/
reports/_lib/file/img/
/reports/_lib/file/img/
public_html/reports/_lib/file/img/
/public_html/reports/_lib/file/img/
user/public_html/reports/_lib/file/img/

When editing a file, I noticed my user name wasn’t the listed root path, so I tried:

/user/public_html/reports/_lib/file/img/
/home3/user/public_html/reports/_lib/file/img/

Or not doing direct links:

…/…/…/_lib/file/img/
…/…/_lib/file/img/
…/_lib/file/img/
_lib/file/img/

To the seasoned hand, I’m sure this looks ludicrous, but I was desperately trying to find a path that would successfully pull the image.

I’m querying the record, and assigning [image] to the returned field from my query. Then in the email, using the [image] variable at the end of the path - but I get nothing. I’ve even changed the form to just write the image to the database (I know this is bad design but I just wanted to see it work) and even with it in the database (so no path is needed I assume) I still don’t get an attachment - so it must be something else I’m doing wrong.

I hate to keep this thread alive for nothing… If I can’t get it working, the users will just have to log in and review the records in a form - instead of getting it in their email =)

Thanks in advance for any ideas / tricks I could try to see how to get it to work

Ok man, lets review everything again ok?.. you are trying to include it into an email right?.. if you have the image into a blob… then you need to place it out of the database before you attach it. ar you doing this?. could you please share your entire code with us?.

Regards

hi guys, hi kafe sempai :slight_smile:

sorry to interrupt, kafe is the boss, i just want to share my experience with this specific stupid issue, i spent long days working on it last year

well, blob will work, finally, but i kept it as plan b (later proofed it can work) but for the best practice, and to keep the images away from a heavy database, I would recommend to have it as image (filename)

now your point will all be in the path

this attachment code worked with me, and sends the image as attached file normally, just add your normal code of sc send mail and use the attachment variable this way… it is tested, however, important point you need to look if you are using “sub folder” for the image when storing, then you need to add that too if used,

another issue was facing, that sometimes there is attachment and sometimes no attachment… so chose to do it this way


[imgvar] = {imagefieldname};
if (empty([imgvar]))
	{
$mail_attachments = ' ';
	}
else {
$mail_attachments = '/home/serveruser/public_html/hostuser/project/_lib/file/img'.{recid}.'/'.[imgvar];
}
// then add $mail_attachments in the array, this way

sc_mail_send($mail_smtp_server,
			 $mail_smtp_user,
			 $mail_smtp_pass,
			 $mail_from,
			 $mail_to,
			 $mail_subject,
			 $mail_message,
			 $mail_format,
			 '',
			 '',
			 '',
			 '',
			 $mail_attachments);



I know you would say that could use directly {imagefieldname} in the path… that was the issue, didn’t work unless made it this way, was using SC v7.1 and now on 8 not sure if it will work using the direct image field, u can try both and c

let us know what happens with you

cheers

Always wellcome to join us MIKEDESAMACHANSEMPAI… :P.

Hey, why I wasn’t invited to the party? I would like to have a pseudonym too. Lol

welcome giu, you already have one dear, 3 letters similar to GUI :slight_smile:

that long name there you need to see here it is nice

kafe started it few months ago and he became the sempai :slight_smile: actually he is genius and very helpful… one of the premium life savers here in the forums :smiley:

cheers guys

From now on you shall be known as SC Graphic User Interface… oh dang … not so clever uh?.. lol >.<

LOL, thanks guys for all your suggestions and I’m sorry I haven’t had a chance to get to it today. As what probably happened to you, Scriptcase crashed on me this morning and I spent what little time I had, working on the fix. Didn’t realize all my backups over the year totaled over 20 gig until me trying to zip up the www folder took over an hour and wasn’t half way finished! So had to clean it all up and get the fix in place - which worked. I will try to look at my attachment problem some more tomorrow and get more details too. I’m about worn out on this one…

Thanks again for the suggestions so far! If I wasn’t doing this for free, I’d be a bit embarrassed at how long this has taken me… =)

Cheers.

Thanks again guys for your help here. I’ve tried multiple things again, using the suggestions and now think I must be missing something else. I have tried so many different things and am getting emails but no attachment - any help / suggestion would be much much appreciated!

I’ve attached 3 images - one of the MySql database (I assume the image field should be a blob even though we’re doing file name), one of Scriptcase Edit Fields (to show you how they’re configured), and one of the actual Field (in case there is anything I didn’t set correctly in it). The “OnAfterInsert” code looks like this:

[rptid] = {Rcrdid};

$mail_message = NULL;

// Get email settings for sending mail
sc_lookup(em, “SELECT smtp_server, smtp_user, mail_from, smtp_pwd, mail_to_alert, mail_to_board FROM tbl_mail WHERE type = ‘main’”);
[server] = {em[0][0]};
[user] = {em[0][1]};
[from] = {em[0][2]};
[pwd] = {em[0][3]};
[toalert] = {em[0][4]};
[board] = {em[0][5]};

// Get details of new submission
$check_sql = “SELECT added, name, phone, email, address, whenobserved, description, discussed, image FROM reports WHERE rcrdid = [rptid]”;
sc_lookup(rp, $check_sql);
[rp_added] = {rp[0][0]};
[rp_name] = {rp[0][1]};
[rp_phone] = {rp[0][2]};
[rp_email] = {rp[0][3]};
[rp_address] = {rp[0][4]};
[rp_observed] = {rp[0][5]};
[rp_descrip] = {rp[0][6]};
[rp_discuss] = {rp[0][7]};
[rp_image] = {rp[0][8]};

// ** Send email notification
// Email parameters
$mail_smtp_server = “[server]”; // SMTP server name or IP address
$mail_smtp_user = “[user]”; // SMTP user name
$mail_smtp_pass = “[pwd]”; // SMTP password
$mail_from = “[from]”; // From email
$mail_to = “[toalert]”; // To email
$mail_subject = "Attention - New Submission ID: " . [rptid]; // Message subject
$mail_message = $mail_message.‘The following information was just submitted: <br><br>’; // Message body
$mail_message = $mail_message.'Reported date: ’ . date(“l, jS \of F Y”,strtotime([rp_added])) . ‘<br>’;
$mail_message = $mail_message."Reporting: " . [rp_name] . “<br>” ;
$mail_message = $mail_message."Number: " . [rp_phone] . “<br>” ;
$mail_message = $mail_message."Address: " . [rp_email] . “<br>” ;
$mail_message = $mail_message."Residence: " . [rp_address] . “<br>” ;
$mail_message = $mail_message."Observed: " . [rp_observed] . “<br>” ;
$mail_message = $mail_message."Description: " . [rp_descrip] . “<br><br>” ;
$mail_message = $mail_message."Discussions (if app): " . [rp_discuss] . “<br><br>” ;
$mail_message = $mail_message.“Thank you,” . “<br><br>”;
$mail_message = $mail_message."Alert Team. ";
$mail_format = ‘H’; // Message format: (T)ext or (H)tml
if (empty([rp_image]))
{
$mail_attachments = ’ ';
}
else
{
$mail_attachments = ‘/home/user/public_html/reports/_lib/file/img/’.[rp_image];
}

// Send email";
sc_mail_send($mail_smtp_server,
$mail_smtp_user,
$mail_smtp_pass,
$mail_from,
$mail_to,
$mail_subject,
$mail_message,
$mail_format,
‘’,
‘’,
‘’,
‘’,
$mail_attachments);

MySql.png

ScriptCase.png

ScriptCase-Field.png

ooook… man… i think this is your problem.

1.- if you are using a blob then you must use the scriptcase “Image(Database)” type of field, if not you are saving the “File name” of the image instead of the actuall image data… remember when you are using a blob… you are inserting “binary data” into your database, in other words you are including the actual 1 & 0’s of your file into the database.

2.- being that the case… you need to take such info out of the database and then place it into some directory so it can be attached to the email. to do so… use something like

file_put_contents('/path/to/new/file_name/new_file_name.jpeg', $my_blob);

3.- once you got the file in place then you use such path to the file to attach it to the email.

Hope this helps…Regards

Wow…again, kafecadm I really appreciate your help! That is all it was after days and days of testing… I should have indicated the field type first =( I changed the image field type to ‘text’ and used the same paths I’d been using, and the image is attached to the email… what a relief after weeks of working on this.

Thanks again SO much! I owe you…

No worries :smiley: that’s what we are here for.

Regards

P.D- dude one more thing… remember that if you use Image File Name in your scriptcase there is no need for you to use the table data as Blob… a varchar(500) <- pick the lenght that suits you better… would be enough. ;D