-2

Thank you for taking time to read this.

How to embed signature into the emails with php?

Facts:

  1. The database column for signature is varchar(max)
  2. When I draw the signature in a php form signature field, It saves the signature data.

example signature input

Example of the data after the signature captured in the database: data:image/jsignature;base30,3S7flha3Z55hic5Y5b1vi63Z2952Ybmi40Z44Yaef630034hc5301a9810Z20Y748953112343_3OZ3aef97Y18jhd81Zdk62Y6gh95Z4761Y6b8Zbb52Y2b9Z38k9Y1a94Zdb9Y48j9Z87ee64Y46ih83

  1. I am using phpmail sender and tried to add the signature field as below
$mail_message        = '
<b>Doctor’s Signature:</b> '.{DoctorSignature}.' <br>
<b>date:</b> '.{date}.' 
'
  1. using jsignature for the signature (Not png)
Synchro
  • 35,538
  • 15
  • 81
  • 104
  • @KenLee Thank you for your comment, I see this error after I send the email : Undefined variable: arr – Christiano Mar 16 '22 at 23:56
  • @KenLee I think the reason is that I am using jsignature not png ? – Christiano Mar 17 '22 at 00:08
  • See whether it helps [convert_base30_to_png](https://github.com/brinley/jSignature/issues/97) (After conversion, just use the converted png in your email) – Ken Lee Mar 17 '22 at 02:10
  • However you are generating the signature in the browser, it should be submitted to PHP as a PNG image. After that, you can treat it just like any other image attachment, exactly as shown in [the PHPMailer upload example](https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_file_upload.phps). No, you can't run javascript within PHP, nor within email, so this conversion needs to happen on the client. – Synchro Mar 17 '22 at 09:54

1 Answers1

0

This is a signature made with jsignature and needs to be parsed as such. I'm not sure if this work in E-Mail but the following code should do the trick:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/gh/brinley/jSignature@master/libs/jSignature.min.js"></script>
<div id="PrintSignatureController">
</div>

<script>
$(document).ready(function() {
  $("#PrintSignatureController").jSignature({
    'decor-color': 'transparent',
    'lineWidth': 1,
  });
  $("#PrintSignatureController").jSignature("importData", "data:image/jsignature;base30,3S7flha3Z55hic5Y5b1vi63Z2952Ybmi40Z44Yaef630034hc5301a9810Z20Y748953112343_3OZ3aef97Y18jhd81Zdk62Y6gh95Z4761Y6b8Zbb52Y2b9Z38k9Y1a94Zdb9Y48j9Z87ee64Y46ih83");
});
</script>

I assume that an E-Mail does not execute JavaScript. In this case, I don't see a way around translating the signature first into base64, which can then be used in a tag.

This is not as pointed but perhaps interesting: https://stackoverflow.com/a/47467731/11781125

  • Thank you for the answer, I am not sure but it does not unbdrestand `'decor-color': 'transparent', 'lineWidth': 1,` error shows 'Parse error: syntax error, unexpected 'decor' (T_STRING) ' – Christiano Mar 17 '22 at 00:16
  • Did you copy and paste it into PHP? You used single quotes for the inserting the HTML. Perhaps you need to escape my single quotes or replace them with double quotes? – Timothy L Gillespie Mar 17 '22 at 00:29
  • Yeah, I copy and pasted it and tried using " instead of ' and also tried to escape all ' and used \' instead . With both methods, I received the email but with no signature – Christiano Mar 17 '22 at 01:00
  • No, you can't run JS inside email, and I'd expect that any form submission performed from JS would include binary image data, not an encoded format (that you would have to decode before use). – Synchro Mar 17 '22 at 09:56