Adi Mehmedičević
Adi Mehmedičević

Reputation: 1

Vlucas doesn't work

I can't make ENVs to work, the script works fine with plain text. What did I do wrong in this setup?

This is my script:

<?php
if (isset ($_SERVER ["HTTPS"]) && $_SERVER ["HTTPS"] !== "off") {
    header("Strict-Transport-Security: max-age=31536000; includeSubDomains; preload");

    header("Content-Security-Policy: default-src 'self';
    script-src 'self' https://www.google-analytics.com https://ssl.google-analytics.com https://www.googletagmanager.com; 
img-src 'self' https://www.google-analytics.com; 
connect-src 'self' https://www.google-analytics.com;");

}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $industry = filter_input(INPUT_POST, "industry", FILTER_SANITIZE_SPECIAL_CHARS);
    $purpose = filter_input(INPUT_POST, "purpose", FILTER_SANITIZE_SPECIAL_CHARS);
    $info = filter_input(INPUT_POST, "info", FILTER_SANITIZE_SPECIAL_CHARS);
    $select = filter_input(INPUT_POST, "webType", FILTER_SANITIZE_SPECIAL_CHARS);
    $webPurpose = filter_input(INPUT_POST, "webPurpose", FILTER_SANITIZE_SPECIAL_CHARS);
    $name = filter_input(INPUT_POST, "name", FILTER_SANITIZE_SPECIAL_CHARS);
    $clientEmail = filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL);
}

if (!filter_var($clientEmail, FILTER_VALIDATE_EMAIL)) {
    die("Invalid email.");
}

require "../vendor/autoload.php";
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use Dotenv\Dotenv;

$dotEnv = dotEnv::createImmutable("/../");
dotEnv->load();
$mail = new PHPMailer();

$mail->isSMTP();
$mail->isHTML(true);
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->SMTPAuth = true;
$mail->Username = getenv("email");
$mail->Password = getenv("password");

$mail->setFrom("[email protected]");
$mail->addAddress("[email protected]");
$mail->Subject = "New Submission!";
$mail->Body = "Industry - " . $industry . "<br>" . "Purpose - " . $purpose . "<br>" . "Additional information - " . $info . "<br>" . "Web type - " . $webPurpose . "<br>" . "Management - " . $managment . "<br>" . "Name - " . $name . "<br>" . "Email - " . $clientEmail;

if ($mail->send()) {
    header("location: ../mail_submitted.html");
    exit();
} else {
    echo "Sorry, something went wrong. You can try submitting again, or contact me directly at [email protected]";
};

Upvotes: 0

Views: 25

Answers (1)

aland
aland

Reputation: 2004

If you want to use the getenv() functions, you need to call $dotEnv = Dotenv::createUnsafeImmutable("/../"); as opposed to $dotEnv = dotEnv::createImmutable("/../");

This is documented on the readme https://github.com/vlucas/phpdotenv#putenv-and-getenv

Upvotes: 0

Related Questions