So senden Sie E-Mails mit der PHP-Funktion mail()
โก Intelligente Zusammenfassung
PHP mail is the built-in function that sends email directly from a PHP script using the serverโs SMTP settings. This walkthrough covers the mail() syntax and parameters, configuring SMTP in php.ini, sanitizing user input with filter_var, sending secure mail, and using PHPMailer for reliable delivery.

Was ist PHP-Mail?
PHP mail is the built-in PHP function that is used to send emails from PHP scripts.
Die Mail-Funktion akzeptiert die folgenden Parameter:
- E-Mail-Adresse
- Betreff
- Nachricht
- CC or BCC email addresses
Why and When to use PHP mail
The mail function is useful in many situations:
- It is a cost effective way of notifying users of important events.
- Ermรถglichen Sie Benutzern die Kontaktaufnahme mit Ihnen per E-Mail, indem Sie auf der Website ein Kontaktformular bereitstellen, das den bereitgestellten Inhalt per E-Mail sendet.
- Developers can use it to receive system errors by email.
- Sie kรถnnen es verwenden, um Ihren Newsletter-Abonnenten E-Mails zu senden.
- You can use it to send password reset links to users who forget their passwords.
- You can use it to email activation and confirmation links. This is useful when registering users and verifying their email addresses.
Senden von E-Mails mit PHP
The PHP mail function has the following basic syntax.
<?php mail($to_email_address,$subject,$message,[$headers],[$parameters]); ?>
HIER,
- โ$to_email_addressโ ist die E-Mail-Adresse des Mailempfรคngers
- โ$subjectโ ist der Betreff der E-Mail
- โ$messageโ ist die zu sendende Nachricht.
- โ[$headers]โ is optional; it can be used to include information such as CC and BCC.
- CC is the acronym for carbon copy. It is used when you want to send a copy to an interested person, i.e. a complaint email sent to a company can also be sent as CC to the complaints board.
- BCC is the acronym for blind carbon copy. It is similar to CC, but the email addresses included in the BCC section are not shown to the other recipients.
Einfacher Mail Transmission Protokoll (SMTP)
PHP mail uses the Simple Mail Transmission Protokoll (SMTP) zum Senden von E-Mails.
On a hosted server, the SMTP settings would already have been set.
The SMTP mail settings can be configured from the โphp.iniโ file in the PHP installation folder.
To configure SMTP settings on your localhost, assuming you are using XAMPP on Windows, locate the โphp.iniโ file in the directory โC:\xampp\phpโ.
- Open it using Notepad or any text editor. We will use Notepad in this example. Click on the Edit menu.
- Click on the Findโฆ menu
- The Find dialog will appear
- Click on the Find Next button
Locate the entries under [mail function]. The default lines usually look like this:
- ; SMTP = lokaler Host
- ; smtp_port = 25
Remove the semicolons before SMTP and smtp_port, and set SMTP to your SMTP-Server and the port to your SMTP port. Your settings should look as follows:
- SMTP = smtp.example.com
- smtp_port = 25
Note: the SMTP settings can be obtained from your web hosting provider. If the server requires authentication, then add the following lines:
- auth_username = example_username@example.com
- auth_password = example_password
Save the new changes and restart the Apache Server.
PHP Mail Beispiel
Let us now look at an example that sends a simple mail.
<?php $to_email = 'name@example.com'; $subject = 'Testing PHP Mail'; $message = 'This mail is sent using the PHP mail function'; $headers = 'From: noreply@example.com'; mail($to_email,$subject,$message,$headers); ?>
Ausgang:
Note: the above example only takes the 4 mandatory parameters. You should replace the above fictitious email address with a real email address.
Bereinigen von E-Mail-Benutzereingaben
Das obige Beispiel verwendet der Einfachheit halber fest codierte Werte im Quellcode fรผr die E-Mail-Adresse und andere Details.
Let us assume you have to create a contact us form where users fill in the details and then submit.
- Users can accidentally or intentionally inject code in the headers, which can result in sending spam mail.
- Um Ihr System vor solchen Angriffen zu schรผtzen, kรถnnen Sie eine benutzerdefinierte Funktion erstellen, die die Werte vor dem Senden der E-Mail bereinigt und validiert.
Let us create a custom function that validates and sanitizes the email address using the filter_var built-in function. The filter_var function is used to sanitize and validate user input data.
Es hat die folgende grundlegende Syntax.
<?php filter_var($field, SANITIZATION_TYPE); ?>
HIER,
- โfilter_var(โฆ)โ ist die Validierungs- und Bereinigungsfunktion
- โ$fieldโ ist der Wert des zu filternden Feldes.
- โSANITIZATION_TYPEโ is the type of sanitization to be performed on the field, such as:
- FILTER_VALIDATE_EMAIL โ returns true for valid email addresses and false for invalid email addresses.
- FILTER_SANITIZE_EMAIL โ removes illegal characters from email addresses, so an address with stray characters is reduced to info@domain.com.
- FILTER_SANITIZE_URL โ removes illegal characters from URLs, leaving a clean address such as https://www.example.com.
- FILTER_SANITIZE_STRING โ removes tags from string values, so <b>am bold</b> becomes am bold. Note this filter is deprecated in PHP 8.1; use htmlspecialchars() instead.
The code below uses a custom function to send secure mail.
<?php function sanitize_my_email($field) { $field = filter_var($field, FILTER_SANITIZE_EMAIL); if (filter_var($field, FILTER_VALIDATE_EMAIL)) { return true; } else { return false; } } $to_email = 'name@example.com'; $subject = 'Testing PHP Mail'; $message = 'This mail is sent using the PHP mail function'; $headers = 'From: noreply@example.com'; // check if the email address is invalid $secure_check = sanitize_my_email($to_email); if ($secure_check == false) { echo "Invalid input"; } else { // send email mail($to_email, $subject, $message, $headers); echo "This email is sent using PHP Mail"; } ?>
Ausgang:
und geschรผtzt Mail
E-Mails kรถnnen wรคhrend der รbertragung von unbeabsichtigten Empfรคngern abgefangen werden.
This can expose the contents of the email to unintended recipients.
Sichere E-Mail lรถst dieses Problem, indem transmitting emails over an encrypted connection using SSL or TLS, for example SMTP over SSL on port 465 or STARTTLS on port 587.
Encryption scrambles the message before sending it, so only the intended recipientโs mail server can read it.
PHPMailer: A Better Alternative to mail()
For real applications, most developers use the PHPMailer library instead of the raw mail() function. PHPMailer authenticates with an external SMTP server, which greatly improves deliverability, and it makes attachments and HTML email simple.
Install it with Composer, then send mail through an authenticated, encrypted SMTP connection as shown below.
<?php use PHPMailer\PHPMailer\PHPMailer; require 'vendor/autoload.php'; $mail = new PHPMailer(true); $mail->isSMTP(); $mail->Host = 'smtp.example.com'; $mail->SMTPAuth = true; $mail->Username = 'user@example.com'; $mail->Password = 'secret'; $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->setFrom('noreply@example.com', 'My App'); $mail->addAddress('name@example.com'); $mail->Subject = 'Testing PHPMailer'; $mail->Body = 'This email is sent using PHPMailer over SMTP'; $mail->send(); ?>
Compared with mail(), PHPMailer gives you SMTP authentication, built-in encryption, attachment support, and clearer error handling, which is why it is the recommended choice for production email.






