Direct email sending from a script is possible thanks to the mail() function. We’ll talk about Mail Function In PHP today. Prior to these, we also covered PHP Exception And Error Handling Tutorial , Forms in PHP, PHP Session, PHP Operators, and PHP Cookies, PHP-File Uploading | Example. If you don’t know about these, then act quickly!
What is PHP Mail?
The built-in PHP function known as PHP mail is used by PHP scripts to send emails.
These are the parameters that the mail function accepts:
- Email address
- CC or BC email addresses
- It’s an economical method of informing users of significant events.
- By including a contact form on your website that emails the provided content, you can allow visitors to get in touch with you via email.
- It allows programmers to email themselves notifications of system errors.
- Your newsletter subscribers can receive emails from you using it.
- It can be used to send links to users who have forgotten their passwords.
- It can be used to send activation/confirmation links via email. This is helpful when registering users and checking their email addresses.
Why/When to use the mail function in PHP?
Sending mail using PHP
Simple and basic syntax:
<?php mail($to_email_address,$subject,$message,[$headers],[$parameters]); ?>
|To_email_address||Required. specifies the email’s recipient or recipients.|
|subject||Required. specifies the email’s subject. Note: Newline characters are not permitted in this parameter.|
|message||Required. specifies the message that will be sent. A LF (n) should be used to separate each line. The maximum number of characters per line is 70. Windows note: If a full stop is discovered at the start of a message line, it might be removed. Replace the full stop with a double dot to fix the issue: <?php|
$txt_file = str_replace(“\n.”, “\n..”, $txt);
|headers||Optional. defines extra headers, such as From, Cc, and Bcc. A CRLF (rn) character should be used to separate the additional headers. Note: A From header is required when sending emails. The php.ini configuration file or this parameter can be used to set this.|
|parameters||Optional. specifies an extra parameter for the Sendmail program (the one that is specified in the configuration setting for the Sendmail path). (For example, when using Sendmail with the -f Sendmail option, this can be used to set the envelope sender address.)|
Simple Mail Transmission Protocol (SMTP)
Simple Mail Transmission Protocol (SMTP) is used by PHP mailers to send emails.
The settings for SMTP would already be in place on a hosted server.
The information about how your system sends emails must be included in the php.ini file to properly configure PHP. Locate the [mail function] section of the php.ini file in the /etc/ directory.
Users of Windows should make sure that two directives are provided. The first is known as SMTP and it specifies the address of your email server. Your personal email address is defined by the second option, send mail from.
The Windows configuration should appear something like this:
[mail function] ; For Win32 only. SMTP = smtp.secureserver.net ; For win32 only sendmail_from = [email protected]
Send HTML Mail
<?php $to = "[email protected], [email protected]"; $subject = "HTML email"; $message = " <html> <head> <title>HTML email</title> </head> <body> <p>This email contains HTML Tags!</p> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>GeektoCode</td> <td>GeektoFIle</td> </tr> </table> </body> </html> "; // Always set content-type when sending HTML email $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; // More headers $headers .= 'From: <[email protected]>' . "\r\n"; $headers .= 'Cc: [email protected]' . "\r\n"; mail($to,$subject,$message,$headers); ?>
Unintended recipients have the ability to intercept emails while they are being sent. This might reveal the email’s contents to recipients who weren’t intended. This issue is resolved by secure mail, which uses HTTPS to transmit emails (HTTPS). Before sending a message, HTTPS encrypts it. To send mail from PHP scripts, use the built-in PHP function mail().
Sending secure mail requires performing validation and sanitization checks on the data. Data sanitization and validation can be done quickly and easily using the built-in PHP function filter var().
Does PHP mail need SMTP?
- Does digital ocean (or whatever hosting you may be on) automatically use SMTP since mail() doesn’t require it?
- Is mail() the best option if my website is going to be expecting to send out more than 1000 emails per day (in separate instances, not in a loop).
The PHP mail() function cannot be used if an external email server that needs authentication must be used.