Search code examples
phphtmlxamppsmtpphpmailer

How to send mail from my mac using local xampp with php contacts form


i have xampp 7.1 installed on my 2016 macbook pro. I have a php contacts form in my source folder which is rendered successfully by chrome along with my html. However, I need to configure the smtp settings to test some functionality locally from my mac.

I found a php.ini in the xampp etc folder that contains some mail function. php.ini mail function screenshot

I found other articles which reference phpmailer but since they were a few years old i figured newer versions of xampp for mac may have all the functionality builtin.

Can I modify this php.ini file to use a gmail mailbox as a recipient? How so?

thanks in advance

Here is a copy paste of the mail function :

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=smtp.gmail.com
; 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 =

; 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

; Log all mail() calls including the full path of the script, line #, 
to address and headers
;mail.log =


Solution

  • If what you are trying to do is sending mails from your app server to your own gmail inbox, you don't have to modify these settings; use PHPMailer is an easier way. So let's start step by step.

      1. Allowing Less secure apps -or- configure for XOAUTH2; credits to @Synchro.
      1. Download PHPMailer and extract PHPMailer.php, SMTP.php and Exception.php to your xampp/htdocs folder

        enter image description here

      1. Write a sample send mail page send.php: (modify to your real account)

        <?php
        require_once('SMTP.php');
        require_once('PHPMailer.php');
        require_once('Exception.php');
        
        use \PHPMailer\PHPMailer\PHPMailer;
        use \PHPMailer\PHPMailer\Exception;
        
        $mail=new PHPMailer(true); // Passing `true` enables exceptions
        
        try {
            //settings
            $mail->SMTPDebug=2; // Enable verbose debug output
            $mail->isSMTP(); // Set mailer to use SMTP
            $mail->Host='smtp.gmail.com';
            $mail->SMTPAuth=true; // Enable SMTP authentication
            $mail->Username='[email protected]'; // SMTP username
            $mail->Password='YourPassword'; // SMTP password
            $mail->SMTPSecure='ssl';
            $mail->Port=465;
        
            $mail->setFrom('[email protected]', 'optional sender name');
        
            //recipient
            $mail->addAddress('[email protected]', 'optional recipient name');     // Add a recipient
        
            //content
            $mail->isHTML(true); // Set email format to HTML
            $mail->Subject='Here is the subject';
            $mail->Body='This is the HTML message body <b>in bold!</b>';
            $mail->AltBody='This is the body in plain text for non-HTML mail clients';
        
            $mail->send();
        
            echo 'Message has been sent';
        } 
        catch(Exception $e) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: '.$mail->ErrorInfo;
        }
        
        ?>
        
      1. When all above are ready, issue the url of send.php on your app server to send the example email
      1. Base on this example, you can write a contact form for real use. You might also want to change the location of PHPMailer, then you should modify the path in the example code to point to the new location where they are.