-
Notifications
You must be signed in to change notification settings - Fork 414
[PHP-wasm] Capture outbound email via withSMTPSink #2585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adamziel
wants to merge
26
commits into
trunk
Choose a base branch
from
with-smtp-sink
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
beb1296
WIP: withSMTPSink
adamziel 1de7af9
Add tests for SmtpSink EHLO, AUTH, STARTTLS, and parseEmail
bgrgicak 659d2b6
Fix linter errors
bgrgicak efcde45
Add PHP mail tests
bgrgicak 3b924b0
Extract WebSocketShim from TCPOverFetchWebsocket
bgrgicak 3348dff
Patch PHP mail() to route through wasm_popen/wasm_pclose
bgrgicak 7ca6b56
Add SMTP sink, sendmail handler, and SmtpSinkWebSocket
bgrgicak 78bacf2
Wire withSMTPSink into web and node runtimes
bgrgicak e06454b
Refresh PHP versions (8.4.20, 8.5.5)
bgrgicak 0aaf687
Add TODO for handling concurrent calls in wasm_popen
bgrgicak 62c440b
mak emit helpers public in WebSocketShim
bgrgicak a3c2b74
Recompile PHP 8.4 JSPI node binary to include mail() popen patch
bgrgicak 7f373f7
Simplify fsockopen SMTP test to only verify email delivery
bgrgicak b26a835
Remove redundant queueMicrotask in SmtpSinkWebSocket
bgrgicak 6905989
Remove silent .catch() handlers in SmtpSinkWebSocket
bgrgicak 062ab31
Remove redundant comments in SMTP sink tests
bgrgicak 001ce4a
Remove redundant comments that restate what the code already says
bgrgicak aefb381
Remove reply334 helper, redundant comments, and silent error handler …
bgrgicak 579f242
Remove unnecessary setTimeout workaround in sendmail handler
bgrgicak 94297ef
Remove wrong filename comment and section dividers in SMTP sink
bgrgicak cc3d98a
Enforce maxSize in sendmail handler to match SmtpSink's limit
bgrgicak 730b5b4
Fix TCPOverFetchWebsocket.close() to transition through CLOSING state
bgrgicak 55b1027
Buffer sends during CONNECTING and error on send after close in SmtpS…
bgrgicak b833fde
Move CC/BCC recipient merging into parseMessage and simplify sendmail…
bgrgicak a11bef7
support string data in TCPOverFetchWebsocket
bgrgicak 85e0184
Use default TextDecoder arguments in SmtpSink
bgrgicak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file renamed
BIN
+24.2 MB
...e-builds/8-4/asyncify/8_4_19/php_8_4.wasm → ...e-builds/8-4/asyncify/8_4_20/php_8_4.wasm
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file renamed
BIN
+23.6 MB
.../node-builds/8-4/jspi/8_4_19/php_8_4.wasm → .../node-builds/8-4/jspi/8_4_20/php_8_4.wasm
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import { PHP, setPhpIniEntries } from '@php-wasm/universal'; | ||
| import type { CaughtMessage } from '@php-wasm/util'; | ||
| import { loadNodeRuntime } from '../lib'; | ||
|
|
||
| const phpVersions = ['8.4']; | ||
| // TODO re-enable testing on all versions before merging | ||
| // 'PHP' in process.env | ||
| // ? [process.env['PHP']! as SupportedPHPVersion] | ||
| // : SupportedPHPVersions; | ||
|
Comment on lines
+6
to
+9
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will recompile all PHP versions after code review to prevent the PR from crashing in GitHub
bgrgicak marked this conversation as resolved.
|
||
|
|
||
| describe.each(phpVersions)('PHP %s – SMTP sink', (phpVersion) => { | ||
| let php: PHP; | ||
| let emails: CaughtMessage[]; | ||
|
|
||
| beforeEach(async () => { | ||
| emails = []; | ||
| php = new PHP( | ||
| await loadNodeRuntime(phpVersion as any, { | ||
| withSMTPSink: { | ||
| port: 25, | ||
| onEmail: (m: CaughtMessage) => emails.push(m), | ||
| }, | ||
| }) | ||
| ); | ||
| await setPhpIniEntries(php, { | ||
| disable_functions: '', | ||
| allow_url_fopen: 1, | ||
| }); | ||
| }, 30_000); | ||
|
|
||
| afterEach(() => { | ||
| php?.exit(); | ||
| }); | ||
|
|
||
| it('captures an email piped via proc_open to sendmail', async () => { | ||
| const result = await php.run({ | ||
| code: `<?php | ||
| error_reporting(E_ALL); | ||
| $email = "From: sender@test.com\r\nTo: recipient@test.com\r\nSubject: Hello from PHP\r\n\r\nThis is the body."; | ||
| $proc = proc_open( | ||
| '/usr/sbin/sendmail -t -i', | ||
| [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], | ||
| $pipes | ||
| ); | ||
| if (!is_resource($proc)) { | ||
| echo 'PROC_OPEN_FAILED'; | ||
| exit; | ||
| } | ||
| fwrite($pipes[0], $email); | ||
| fclose($pipes[0]); | ||
| $exit = proc_close($proc); | ||
| echo $exit === 0 ? 'SENT' : 'EXIT_' . $exit; | ||
| `, | ||
| }); | ||
|
|
||
| expect(result.text).toBe('SENT'); | ||
| expect(emails).toHaveLength(1); | ||
| expect(emails[0].from).toContain('sender@test.com'); | ||
| expect(emails[0].to).toContain('recipient@test.com'); | ||
| expect(emails[0].subject).toBe('Hello from PHP'); | ||
| expect(emails[0].text?.trim()).toBe('This is the body.'); | ||
| }); | ||
|
|
||
| it('captures an email sent via fsockopen SMTP', async () => { | ||
| const result = await php.run({ | ||
| code: `<?php | ||
| error_reporting(E_ALL); | ||
|
|
||
| // Helper: read a full SMTP reply (may be multi-line). | ||
| // Multi-line replies have a dash after the code (e.g. "250-..."). | ||
| // The final line has a space (e.g. "250 ..."). | ||
| function smtp_read_reply($fp) { | ||
| $lines = ''; | ||
| while (($line = fgets($fp)) !== false) { | ||
| $lines .= $line; | ||
| // Final line: code followed by space (not dash) | ||
| if (preg_match('/^\\d{3} /', $line)) break; | ||
| } | ||
| return $lines; | ||
| } | ||
|
|
||
| $smtp = fsockopen('localhost', 25, $errno, $errstr, 5); | ||
| if (!$smtp) { | ||
| echo "CONNECT_FAILED: $errstr ($errno)"; | ||
| exit; | ||
| } | ||
|
|
||
| // Read server greeting | ||
| $greeting = smtp_read_reply($smtp); | ||
| if (strpos($greeting, '220') !== 0) { | ||
| echo "BAD_GREETING"; | ||
| fclose($smtp); | ||
| exit; | ||
| } | ||
|
|
||
| fwrite($smtp, "EHLO localhost\\r\\n"); | ||
| smtp_read_reply($smtp); | ||
|
|
||
| fwrite($smtp, "MAIL FROM:<sender@test.com>\\r\\n"); | ||
| smtp_read_reply($smtp); | ||
|
|
||
| fwrite($smtp, "RCPT TO:<recipient@test.com>\\r\\n"); | ||
| smtp_read_reply($smtp); | ||
|
|
||
| fwrite($smtp, "DATA\\r\\n"); | ||
| smtp_read_reply($smtp); | ||
|
|
||
| fwrite($smtp, "From: sender@test.com\\r\\n"); | ||
| fwrite($smtp, "To: recipient@test.com\\r\\n"); | ||
| fwrite($smtp, "Subject: Hello via SMTP\\r\\n"); | ||
| fwrite($smtp, "\\r\\n"); | ||
| fwrite($smtp, "This is the body.\\r\\n"); | ||
| fwrite($smtp, ".\\r\\n"); | ||
| smtp_read_reply($smtp); | ||
|
|
||
| fwrite($smtp, "QUIT\\r\\n"); | ||
| fclose($smtp); | ||
| echo 'SENT'; | ||
| `, | ||
| }); | ||
|
|
||
| expect(result.text).toBe('SENT'); | ||
| expect(emails).toHaveLength(1); | ||
| expect(emails[0].from).toContain('sender@test.com'); | ||
| expect(emails[0].to).toContain('recipient@test.com'); | ||
| expect(emails[0].subject).toBe('Hello via SMTP'); | ||
| expect(emails[0].text?.trim()).toBe('This is the body.'); | ||
| }); | ||
|
|
||
| it('captures an email sent via mail()', async () => { | ||
| const result = await php.run({ | ||
| code: `<?php | ||
| error_reporting(E_ALL); | ||
| $result = mail( | ||
| 'recipient@test.com', | ||
| 'Hello from PHP', | ||
| 'This is the body.', | ||
| 'From: sender@test.com' | ||
| ); | ||
| echo $result ? 'SENT' : 'FAILED'; | ||
| `, | ||
| }); | ||
|
|
||
| expect(result.text).toBe('SENT'); | ||
| expect(emails).toHaveLength(1); | ||
| expect(emails[0].from).toContain('sender@test.com'); | ||
| expect(emails[0].to).toContain('recipient@test.com'); | ||
| expect(emails[0].subject).toBe('Hello from PHP'); | ||
| expect(emails[0].text?.trim()).toBe('This is the body.'); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we do that universally for all popen calls in all the C code we use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker here btw, it would just be useful. There's a CLI flag that wraps C function calls, we use it somewhere in this Dockerfile. Was it
-Wl,--wrap=symbol?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but I would prefer to first enable PHP-WASM to support concurrent writable popen calls before switching all popen and pclose calls to the PHP-WASM implementation.
Let's do it as a follow-up PR.
Context https://github.com/WordPress/wordpress-playground/pull/2585/changes#diff-7c2ff9d253962b8819bd036427a04e7383e8d190b6e9d4f9d54bfe8f9fcf9ca2R579-R588
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add support for concurrent popen calls PR