0

When I try to send email from my HTML form I get the following error message:

Warning: mail(): Failed to connect to mailserver at "smtp.gmail.com" port 465, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Apache24\htdocs\WEB-SERVER\5.1.1 Epost-sändning utan bifogade filer\default.php on line 29

I have php 7.2 and apache24

And i have downloaded the files:

PHPMailerAutoload.php
class.phpmailer.php
class.smtp.php
class.pop3.php

and put them in the same catalog as my php file

I don't see what is wrong, i have also changed my SMTP och port in my php.ini to the right ones. (465 and 'smtp.gmail.com')

PHP code:

<?php

    ini_set('SMTP','smtp.gmail.com');
    ini_set('smtp_port',465);
    require 'PHPMailerAutoload.php';
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465;
    $mail->SMTPAuth = true;
    $mail->Username = 'filip.fellman@gmail.com';
    $mail->Password = 'hejsan';
    $mail->SMTPSecure = 'ssl';

    if ($_POST["password"] == "hejsan") {

        $from = $_POST["from"];
        $to = $_POST["to"];
        $cc = $_POST["cc"];
        $bcc = $_POST["bcc"];
        $subject = $_POST["subject"];
        $message = $_POST["message"];
        $password    = $_POST["password"] . "\n\n\n\nObservera! Detta meddelande är sänt från ett formulär på Internet och avsändaren kan vara felaktig!";

        $headers =  "From: " . $from . "\r\n" .
                    "Cc: " . $cc . "\r\n" .
                    "Bcc: " . $bcc;

        mail($to, $subject, $message, $headers);


    }else {
        echo "Fel lösenord!";
    }

?>

What's wrong?

FirstOne
  • 6,033
  • 7
  • 26
  • 45
Filip
  • 45
  • 7
  • 2
    Why are you creating a `PHPMailer` object and then not using it? – David Dec 20 '17 at 18:49
  • Look at the second answer of https://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php. The mail function is built-into PHP and will use the default SMTP service on your server, but some webhosts are not configured to allow this. To use the gmail SMTP server you need to continue to use PHP mailer class as explained in the SO question I mentioned. – hostingutilities.com Dec 20 '17 at 19:09
  • 1
    Possible duplicate of [How to send an email using PHP?](https://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php) – hostingutilities.com Dec 20 '17 at 19:10

0 Answers0