PHP comes with a default function mail() that allows you to sendmail directly from a PHP script. Here in this tutorial, we will be talking about the prerequisites to sending a mail directly from a PHP script, the syntax, and its parameters. Show Prerequisites
Syntaxmail (to,subject,message,headers,parameters); Parametersto subject message additional_headers additional_parameters Return Valuemail() function returns TRUE if the SMTP server successfully accepted the mail for delivery, else FALSE. Getting TRUE doesn't necessarily mean that the email is delivered to the recipient's server. TRUE is just an indication that your mail has successfully submitted to the SMTP server's queue for sending. Note: mail() function will not work in Local server. A server connected to the internet and SMTP ports opened will be required to send mail. Few Use Cases and Working Examples1. How to send an HTML mail [email protected], [email protected]"; $subject = "This is a test HTML email"; $message = " This is a test HTML email 2. How to sendmail with an additional command line parameter. [email protected]', 'This is a test subject line', 'The complete body of the message', null, '[email protected]'); ?> Common Errors/Exceptions with PHP sendmail function
Configuring PHP for sending mailIn order to configure anything related to PHP you need to change `php.ini` file. So, we will be editing php.ini file in order to configure Sendmail. You can easily locate or search your php.ini file in Linux using below command: locate php.ini The default location is `/etc/php.ini` You can find the same in windows where XAMPP or LAMPP is installed: `C:\xampp\php\php.ini` Clarification:
Changing php.ini file to add mail configuration. 1. Open your php.ini file using below: For Linux/Mac OS: vim /etc/php.in
2. Search [mail function] in the file. It will be as shown below: [mail function] ; For Win32 only. ; http://php.net/smtp SMTP = localhost ; http://php.net/smtp-port smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from ;sendmail_from = [email protected] ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ; http://php.net/sendmail-path sendmail_path = /usr/sbin/sendmail -t -i ; Force the addition of the specified parameters to be passed as extra parameters ; to the sendmail binary. These parameters will always replace the value of ; the 5th parameter to mail(), even in safe mode. ;mail.force_extra_parameters = ; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename mail.add_x_header = On ; The path to a log file that will log all mail() calls. Log entries include ; the full path of the script, line number, To address and headers. ;mail.log = 3. Add your mail server details to the file or incase you have one you can change it (mail server can be your own ie. local mail server or you can use any ESP as a mail server). 4. Save/close the php.ini file 5. The final step, don’t forget to restart your webserver/php-fpm. Pro tip: You can host a simple “info.php” on your webserver to check each and every configuration of your PHP using below 2 liner code: vim php_info.php Save and exit the file. ConclusionHope, this tutorial is able to help you send mail using PHP. In case you are facing some issues which are not listed above in the tutorial, or you have some suggestions, then please feel free to contribute below in comments. |