Reputation: 2850
Doing my first steps with php
and I am a bit confused. I have written a small script, which should take a POST request with some data, put the data in a formatted email and send it. For this I simply used PhpMailer and adapted the demo code. Apart from an outer try-catch resulting in a 500 response, this is my code for the return messages so far:
if($mail->send()){
http_response_code(200);
exit(json_encode(["message"=>"Mails sent successfully!"]));
}else{
http_response_code(500);
exit(json_encode(["message"=>"Mails not sent!", "error"=>$mail->ErrorInfo]));
}
The script works fine, I receive the email, but testing with cuRL I would expect a reponse body with {"message":"Mails sent successfully!"}
, however I get a full server log, pretty much showing stuff I do not want to show (like the email address the script sends the email to - I have changed these here to generic ones):
HTTP/1.1 200 OK
Date: Mon, 13 Jan 2025 11:35:17 GMT
Server: Apache
X-Content-Type-Options: nosniff
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
2025-01-13 11:35:17 Connection: opening to smtp.myserver.com:587, timeout=5, options=array()<br>
2025-01-13 11:35:17 Connection: opened<br>
2025-01-13 11:35:17 SERVER -> CLIENT: 220 mx01lb.myserver.com ESMTP Exim 4.97.1 Mon, 13 Jan 2025 12:35:17 +0100<br>
2025-01-13 11:35:17 CLIENT -> SERVER: EHLO www.some-domain.comt<br>
2025-01-13 11:35:17 SERVER -> CLIENT: 250-mx01lb.myserver.com Hello www.some-domain.com [81.19.145.43]250-SIZE 157286400250-8BITMIME250-PIPELINING250-PIPECONNECT250-AUTH LOGIN PLAIN250-STARTTLS250 HELP<br>
2025-01-13 11:35:17 CLIENT -> SERVER: STARTTLS<br>
2025-01-13 11:35:17 SERVER -> CLIENT: 220 TLS go ahead<br>
2025-01-13 11:35:17 CLIENT -> SERVER: EHLO www.some-domain.comt<br>
2025-01-13 11:35:17 SERVER -> CLIENT: 250-mx01lb.myserver.com Hello www.some-domain.comt [81.19.145.43]250-SIZE 157286400250-8BITMIME250-PIPELINING250-PIPECONNECT250-AUTH LOGIN PLAIN250 HELP<br>
2025-01-13 11:35:17 CLIENT -> SERVER: AUTH LOGIN<br>
2025-01-13 11:35:17 SERVER -> CLIENT: 334 VXNlcm5hbWU6<br>
2025-01-13 11:35:17 CLIENT -> SERVER: [credentials hidden]<br>
2025-01-13 11:35:17 SERVER -> CLIENT: 334 UGFzc3dvcmQ6<br>
2025-01-13 11:35:17 CLIENT -> SERVER: [credentials hidden]<br>
2025-01-13 11:35:17 SERVER -> CLIENT: 235 Authentication succeeded<br>
2025-01-13 11:35:17 CLIENT -> SERVER: MAIL FROM:<[email protected]><br>
2025-01-13 11:35:17 SERVER -> CLIENT: 250 OK<br>
2025-01-13 11:35:17 CLIENT -> SERVER: RCPT TO:<[email protected]><br>
2025-01-13 11:35:17 SERVER -> CLIENT: 250 Accepted<br>
2025-01-13 11:35:17 CLIENT -> SERVER: DATA<br>
2025-01-13 11:35:17 SERVER -> CLIENT: 354 Enter message, ending with "." on a line by itself<br>
2025-01-13 11:35:17 CLIENT -> SERVER: Date: Mon, 13 Jan 2025 12:35:17 +0100<br>
2025-01-13 11:35:17 CLIENT -> SERVER: To: [email protected]<br>
2025-01-13 11:35:17 CLIENT -> SERVER: From: [email protected]<br>
2025-01-13 11:35:17 CLIENT -> SERVER: Subject: Neue Anfrage<br>
2025-01-13 11:35:17 CLIENT -> SERVER: Message-ID: <[email protected]><br>
2025-01-13 11:35:17 CLIENT -> SERVER: X-Mailer: PHPMailer 6.9.3 (https://github.com/PHPMailer/PHPMailer)<br>
2025-01-13 11:35:17 CLIENT -> SERVER: MIME-Version: 1.0<br>
2025-01-13 11:35:17 CLIENT -> SERVER: Content-Type: text/plain; charset=iso-8859-1<br>
2025-01-13 11:35:17 CLIENT -> SERVER: <br>
2025-01-13 11:35:17 CLIENT -> SERVER: Guten Tag, <br>
2025-01-13 11:35:17 CLIENT -> SERVER: <br>
2025-01-13 11:35:17 CLIENT -> SERVER: sie haben eine neue Anfrage auf der Jugendseite bekommen. Folgende Daten wurden angegeben: <br>
2025-01-13 11:35:17 CLIENT -> SERVER: <br>
2025-01-13 11:35:17 CLIENT -> SERVER: test: asdf <br>
2025-01-13 11:35:17 CLIENT -> SERVER: test2: qwer <br>
2025-01-13 11:35:17 CLIENT -> SERVER: <br>
2025-01-13 11:35:17 CLIENT -> SERVER: <br>
2025-01-13 11:35:17 CLIENT -> SERVER: Mit der Bitte um Bearbeitung. <br>
2025-01-13 11:35:17 CLIENT -> SERVER: Dein freundlicher Bot<br>
2025-01-13 11:35:17 CLIENT -> SERVER: <br>
2025-01-13 11:35:17 CLIENT -> SERVER: .<br>
2025-01-13 11:35:17 SERVER -> CLIENT: 250 OK id=1tXIiz-000000004RA-1doi<br>
2025-01-13 11:35:17 CLIENT -> SERVER: QUIT<br>
2025-01-13 11:35:17 SERVER -> CLIENT: 221 mx01lb.myserver.com closing connection<br>
2025-01-13 11:35:17 Connection: closed<br>
{"message":"Mails sent successfully!"}
After reading up a bit, I tried to add ob_clean();
before sending the return, but no luck. This doesn't seem to come from the output buffer, but is attached to the response somewhere. As mentioned, I have no prior PHP experience, but I would like to be in control what is returned and what not. Any suggestions are much appreciated.
Upvotes: 0
Views: 50
Reputation: 27307
This is because you have enabled the debugging output of PHPMailer, so when you execute the send
method, these contents will be output. Just turn it off before sending:
$mail->SMTPDebug = SMTP::DEBUG_OFF;
Or output the debugging log to somewhere else:
$mail->Debugoutput = 'error_log';
Upvotes: 4