0

I am trying to write a php script that sends an attachment using php mail_runner with an attachment taken from a file within a subdirectory.

This is part of a multi step approach, where I have used imagettftext to take a template jpg training certificate, fill it in with the learners data and then save the file to the server, which I have done, for example:

    $filenamedate = date('Ymdhi');
    $file=$filenamedate." ".$name." (".$CourseDescription .")";
    $file_path="Certificates/".$file.".jpg";

Does successfully save the .jpg to the /Certificate folder, the filename below is an example:

202303070135 Jonathan Green (D1 Policy and Rules for Drivers).jpg

I am able to send a basic email using mail_runner, which is a must for this project, this includes the normal fields such as to, msg, from, etc.

However, I am struggling to get the code working to attach the file that has been saved above. What I am trying is roughly:

$from = "myemail@test.com";
$to = "myclient@test.com";
$msg = "Test Documents Attached.";
$subject="Documents";
 
$attachments = array();
// Attachments description. The 'path'(a path to the attachment) is required. Others parameters are optional:
//'name' overrides the attachment name, 'encoding' sets a file encoding, 'type' sets a MIME type
$attachments = array('path' => getabspath('$file_path'),
                  'name' => 'testcertificate.jpg',
                  'encoding' => 'base64',
                  'type' => 'application/octet-stream');
 
$ret = runner_mail(array('from' => $from, 'to' => $to, 'subject' => $subject, 'body' => $msg, 'attachments' => $attachments));

Unfortunaley this does not work and gives me the following error:

Fatal error : Uncaught TypeError: Cannot access offset of type string on string

The only other modification ive made results in the email being sent with at attachment, but not been able to open it.

So, what do I need to do to grab this file from the server and send as an attachment?

However, I am struggling to get the code working to attach the file that has been saved above. What I am trying is roughly:

`$from = "myemail@test.com";
$to = "myclient@test.com";
$msg = "Test Documents Attached.";
$subject="Documents";
 
$attachments = array();
// Attachments description. The 'path'(a path to the attachment) is required. Others parameters are optional:
//'name' overrides the attachment name, 'encoding' sets a file encoding, 'type' sets a MIME type
$attachments = array('path' => getabspath('$file_path'),
                  'name' => 'testcertificate.jpg',
                  'encoding' => 'base64',
                  'type' => 'application/octet-stream');
 
$ret = runner_mail(array('from' => $from, 'to' => $to, 'subject' => $subject, 'body' => $msg, 'attachments' => $attachments));`

Unfortunaley this does not work and gives me the following error:

Fatal error : Uncaught TypeError: Cannot access offset of type string on string

The only other modification ive made results in the email being sent with at attachment, but not been able to open it.

So, what do I need to do to grab this file from the server and send as an attachment?

JKG79
  • 3
  • 2
  • 500 Internal Server Error is a generic error message informing you that the server crashed while processing the request. Beyond that, it's (intentionally) meaningless, and is of very little use for debugging. You need to check the error logs on the server to try and find the underlying exception message. Once you've got that, you stand a chance of identifying the problem, and then moving on to trying to fix it. – ADyson Mar 07 '23 at 16:16
  • thanks, ive updated it, it was missing a ; but now with that I am getting Fatal error : Uncaught TypeError: Cannot access offset of type string on string in C – JKG79 Mar 07 '23 at 16:22
  • Which line throws that? Have you googled the error? Have you done any debugging? It sounds like you are trying to access a string (i.e. a text variable) as if it was an array. – ADyson Mar 07 '23 at 17:07
  • 1
    You have single quotes around the variable so `getabspath()` will be trying to get the path of a file named `$file_path`. Also `$attachments` should be an array of arrays per the docs: https://xlinesoft.com/phprunner/docs/runner_mail_function.htm – cOle2 Mar 07 '23 at 17:53

0 Answers0