Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Procedure to Generate an xml file and email

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Procedure to Generate an xml file and email

Unread post by ricklincs »

I have used php mail() to send information within an email by calling a procedure from a edit screen with nuAddActionButton('nuRunPHPHidden', 'Email', 'nuRunPHPHidden("Discharged_email")') as Javascript. This works great and the email is received.

The procedure (run from Vessel form) is:

$from = "me@test.co.uk";
$to = "you@test.co.uk";
$subject = "Vessel Discharged Notification '#vesselid#' Load: '#LoadingPlanNumber#'";
$message = "Plan No: #LoadingPlanNumber# Vessel: #vesselid# Weekno: #weeknumber# Date: #steveend#";
$headers = "From:" . $from;
mail($to, $subject, $message, $headers);
$LoadingPlanNumber = '#LoadingPlanNumber#';
$vesselid = '#vesselid#';
$weeknumber = '#weeknumber#';
$steveend = '#steveend#';

However I am now told they need this information sent as an email xml attachment. Has anyone managed to produce an xml file with php and then email as an attachment, or any idea of a starting point?

Thank you.
kev1n
nuBuilder Team
Posts: 4299
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Procedure to Generate an xml file and email

Unread post by kev1n »

Generate the xml with the DOMDocument class, save it to a file on the server

Example for DOMDocument
https://stackoverflow.com/questions/355 ... hp/3552794


and send it with nuSendEmail()

Code: Select all

nuSendEmail('to@test.com','from@test.com','From','Content','Subject', ['server_path_to_xml'], true);
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Re: Procedure to Generate an xml file and email

Unread post by ricklincs »

Thanks Ke1vn, that has worked great. I could not get nuSendemail() to work, I think something to do with my email settings/provider but managed to do this with
PHP Mail. Again thanks Ke1vn for your quick reply and help.
kev1n
nuBuilder Team
Posts: 4299
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Procedure to Generate an xml file and email

Unread post by kev1n »

ricklincs wrote: I could not get nuSendemail() to work
It should work like this:

Code: Select all

$fileName = "yourfile.xml";
$filePath = "path_to_server/".$fileName;

nuSendEmail($toAddress, $fromAddress, $fromName, $mailBody, $mailSubject, array($fileName => $filePath) , true);
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Re: Procedure to Generate an xml file and email

Unread post by ricklincs »

I have been using the below procedure (run from the a vessel form) to send an email and a xml attachment for over a year now, it has worked great, well it has sent 93 vessel discharge emails and xml attachments with no errors.
Yesterday it starting to miss the seconds of the Element(LOADARRIVALUTCDATETIME), the database still shows the field steveend as datetime and in the format 2022-06-28 12:38:00 but when the xml attachment is created it now shows 2022-06-28T12:38 instead of 2022-06-28T12:38:00 as it previously did.

I have made no amendments or updates to NuBuilder. The version of PHP is 7.4.30 and the database structure hasn't been changed. Has body else seen this with datetime?


Start PHP:

$LoadingPlanNumber = '#LoadingPlanNumber#';
$vesselid = '#vesselid#';
$site = '#SITE#';
$steveend = '#steveend#';
$myfile = '#LoadingPlanNumber#';
$ext = 'xml';
$document = 'SLPLoadArrivalDateEntity';
//Build xml file
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;

$root = $doc->createElement('Document');
$root = $doc->appendChild($root);

$ele1 = $doc->createElement('SLPLoadArrivalDateEntity');
//$ele1->nodeValue=$document;
$root->appendChild($ele1);

$ele2 = $doc->createElement('LOADARRIVALUTCDATETIME');
$ele2->nodeValue=$steveend;
$ele1->appendChild($ele2);

$ele3 = $doc->createElement('LOADID');
$ele3->nodeValue=$LoadingPlanNumber;
$ele1->appendChild($ele3);

$ele4 = $doc->createElement('SITE');
$ele4->nodeValue=$site;
$ele1->appendChild($ele4);

$doc->save("$myfile.$ext");
// Recipient
$to = '[email address hidden]';

// Sender
$from = '[email address hidden]';
$fromName = '[Name hidden]';

// Email subject
$subject = 'Vessel Discharge';

// Attachment file
$file = "$myfile.$ext";

// Email body content
$htmlContent = '
<h3>Vessel Discharge</h3>
<p>This email is sent from:</p>
<p></p>
<p> [Hidden]</p>
<p>{Hidden]</p>
<p>{Hidden]</p>
<p></p>
<p>Tel {hidden]. Email: [hidden]</p>
';

// Header for sender info
$headers = "From: $fromName"." <".$from.">";

// Boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

// Multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";

// Preparing attachment
if(!empty($file) > 0){
if(is_file($file)){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($file,"rb");
$data = @fread($fp,filesize($file));

@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" .
"Content-Description: ".basename($file)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($file)."\"; size=".filesize($file).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;

// Send email
$mail = @mail($to, $subject, $message, $headers, $returnpath);

// Email sending status

// Archive Vessel in Manifest

End Php
kev1n
nuBuilder Team
Posts: 4299
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Procedure to Generate an xml file and email

Unread post by kev1n »

Does $steveend; contain the datetime string? What do you see if you output it nuDebug?

Code: Select all

nuDebug($steveend);
kev1n
nuBuilder Team
Posts: 4299
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Procedure to Generate an xml file and email

Unread post by kev1n »

Is this solved?
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Re: Procedure to Generate an xml file and email

Unread post by ricklincs »

Sorry for the late reply (have been away due to ill health). No this issue is still not resolved. Yes $steveend contains a datetime string.
This is now happening for any procedures I use to produce xml files which datetime fields. Fields with values 22-08-16T14:54:00 miss off the :00 fields with values 22-08-16T14:54:01 it works fine for.
kev1n
nuBuilder Team
Posts: 4299
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Procedure to Generate an xml file and email

Unread post by kev1n »

kev1n wrote: Wed Jul 06, 2022 8:51 pm Does $steveend; contain the datetime string? What do you see if you output it nuDebug?

Code: Select all

nuDebug($steveend);
Did you try that and what was the output?

If you place the PHP in a separate file (outside of nuBuilder & replace all hash cookies with some values), does the issue occur too?
Post Reply