0

I have a contact form on my website that I want to be able to send emails to a certain address. I have followed this tutorial, but I can't get it to work. As a comment under that post stated, I'm using XamPP and can skip step 1, 2, 9 and 10. My website is running on localhost port :8080. The only thing I think it can be, is the fact that I can't find one of the files I should edit, according to that post I mentioned. That file is located (for him) here: wamp\bin\apache\apache[version here]\bin\php.ini. As I'm using XamPP, I don't have that location.

Below is my sendmail.ini file, located here: xampp\sendmail\sendmail.ini. I'm not sure if I have to edit more things to this or not, I have tried changing the localhost to localhost:8080, as I'm using port 8080 like I said. Also note that I have deleted the comments that are in the file, so it's easier to read. I've also changed a bit where my email and password should be, for obvious reasons.

[sendmail]

smtp_server=mail.gmail.com

smtp_port=465

smtp_ssl=ssl

default_domain=localhost:8080

error_logfile=error.log

debug_logfile=debug.log


auth_username= emailaddress
auth_password= mypassword


;pop3_server=
;pop3_username=
;pop3_password=

;force_sender=

;force_recipient=

hostname= localhost:8080

Here is my php.ini file, located here: xampp\php\php.ini. This is not the same file that i couldn't find, the one in the apache folder. I have only put the relevant (I think) bit of the file here, as this is the only thing that I have changed.

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=localhost:443
; http://php.net/smtp-port
smtp_port=465

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t -i" 

Finally, here is my php code that executes the contact form. I get the following error message when executing it: Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\Portfolio\contact_backend.php on line 49

Er ging iets mis, probeer het nog eens. (this is my own set error message)

<?php

if($_POST) {
$visitor_name = "";
$visitor_email = "";
$email_title = "";
$concerned_department = "";
$visitor_message = "";
$email_body = "<div>";
  
if(isset($_POST['visitor_name'])) {
    $visitor_name = filter_var($_POST['visitor_name'], FILTER_SANITIZE_STRING);
    $email_body .= "<div>
                       <label><b>Visitor Name:</b></label>&nbsp;<span>".$visitor_name."</span>
                    </div>";
}

    if(isset($_POST['visitor_email'])) {
    $visitor_email = str_replace(array("\r", "\n", "%0a", "%0d"), '', 
    $_POST['visitor_email']);
    $visitor_email = filter_var($visitor_email, FILTER_VALIDATE_EMAIL);
    $email_body .= "<div>
                       <label><b>Visitor Email:</b></label>&nbsp;<span>".$visitor_email." 
   </span>
                        </div>";
    }
  
if(isset($_POST['email_title'])) {
    $email_title = filter_var($_POST['email_title'], FILTER_SANITIZE_STRING);
    $email_body .= "<div>
                       <label><b>Reason For Contacting Us:</b></label>&nbsp; 
   <span>".$email_title."</span>
                        </div>";
    }
  
if(isset($_POST['visitor_message'])) {
    $visitor_message = htmlspecialchars($_POST['visitor_message']);
    $email_body .= "<div>
                       <label><b>Visitor Message:</b></label>
                       <div>".$visitor_message."</div>
                    </div>";
}
  
$recipient = "martinboersma55@gmail.com";
  
$email_body .= "</div>";

$headers  = 'MIME-Version: 1.0' . "\r\n"
.'Content-type: text/html; charset=utf-8' . "\r\n"
.'From: ' . $visitor_email . "\r\n";
  
if(mail($recipient, $email_title, $email_body, $headers)) {
    echo "<p>Bedankt voor het sturen van het bericht, $visitor_name. Er wordt zsm naar 
gekeken.</p>";
} else {
    echo '<p>Er ging iets mis, probeer het nog eens.</p>';
}
  
} else {
echo '<p>Er ging iets mis, probeer het nog eens.</p>';
}
?>

So to sum it all up, I'm trying to make a working contact form on my localhost, that runs on port :8080. I get the error message I mentioned above and I'm not sure what I'm supposed to do. I followed the tutorial from another post I found, the link is on the top of this question.

Martin
  • 47
  • 8
  • 3
    I'd suggest, you'd take a look at [PHPMailer](https://github.com/PHPMailer/PHPMailer). It's an easy and really good library for sending emails via SMTP. For me at least it always worked way better than the built-in stuff. – stui Sep 22 '21 at 08:08
  • 1
    Use one of the tried and tested mail libraries like PHPMailer instead of the low level `mail()`-function. Specially if you're trying to send mail using gmail. With PHPMailer, you can send using [XOAUTH2[(https://github.com/PHPMailer/PHPMailer/wiki/Gmail-XOAUTH2-Using-Google-API-Client) which is what Google recommends. Then you don't need to mess around with server configurations. And Google have made it way harder to use their SMTP server (for example, you need to configure your account to allow less secure apps, a setting they reset now and again for you, breaking your service) – M. Eriksson Sep 22 '21 at 08:08
  • @stui Yeah, I was just reading about that. I'll look into it and see if I can make it work, thanks for the suggestion! – Martin Sep 22 '21 at 08:10
  • Constant [FILTER_SANITIZE_STRING](https://stackoverflow.com/questions/69207368/constant-filter-sanitize-string-is-deprecated) is deprecated. Please stop using it. – Dharman Sep 24 '21 at 10:06

0 Answers0