Skip to content

Instantly share code, notes, and snippets.

@daveh
Last active June 29, 2025 23:37
Show Gist options
  • Save daveh/1164348fe21a6e7363d28c7b94c9eb3f to your computer and use it in GitHub Desktop.
Save daveh/1164348fe21a6e7363d28c7b94c9eb3f to your computer and use it in GitHub Desktop.
Send email with PHP (code to accompany https://youtu.be/fIYyemqKR58)
<!DOCTYPE html>
<html>
<head>
<title>Contact</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head>
<body>
<h1>Contact</h1>
<form method="post" action="send-email.php">
<label for="name">Name</label>
<input type="text" name="name" id="name" required>
<label for="email">email</label>
<input type="email" name="email" id="email" required>
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" required>
<label for="message">Message</label>
<textarea name="message" id="message" required></textarea>
<br>
<button>Send</button>
</form>
</body>
</html>
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
require "vendor/autoload.php";
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
$mail = new PHPMailer(true);
// $mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.example.com";
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->Username = "[email protected]";
$mail->Password = "password";
$mail->setFrom($email, $name);
$mail->addAddress("[email protected]", "Dave");
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
header("Location: sent.html");
<!DOCTYPE html>
<html>
<head>
<title>Contact</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head>
<body>
<h1>Contact</h1>
<p>Thank you for your message.</p>
</body>
</html>
@daveh
Copy link
Author

daveh commented Mar 5, 2024

@GuyCos In Windows it's the command prompt application, in Mac it's called Terminal I believe. You need to change to the folder where your code is using the "cd" command.

To install packages with Composer, you need to have it installed, instructions here.

Then you can follow the instructions in the video. You don't need to install it on your live server, rather the usual method is to install the code locally, then copy it live. (it will install code to the vendor folder, just copy that whole folder to your live server)

@GuyCos
Copy link

GuyCos commented Mar 7, 2024 via email

@GuyCos
Copy link

GuyCos commented Mar 7, 2024 via email

@daveh
Copy link
Author

daveh commented Mar 7, 2024

@GuyCos Is the vendor folder inside the public_html folder?

@GuyCos
Copy link

GuyCos commented Mar 7, 2024 via email

@GuyCos
Copy link

GuyCos commented Mar 7, 2024 via email

@daveh
Copy link
Author

daveh commented Mar 8, 2024

@GuyCos I'm afraid I can't see the screenshots. The composer.lock file (along with the composer.json file) is generated automatically when you run the composer install command. It will be in the same place the vendor folder was.

I suggest you run the composer install command again from your public_html folder, so that all the files are in the correct place.

@GuyCos
Copy link

GuyCos commented Mar 8, 2024 via email

@jcdynasteel
Copy link

When I try to send an email with my personal website and I click "Send", it just downloads a send-email.php file which is exactly just the code. Any help with this?

@daveh
Copy link
Author

daveh commented Apr 5, 2024

@jcdynasteel Sounds like you're opening form.html as a file, not from a web server - if so the web address will be something like file:///path/to/form.html instead of http://example.com/form.html.
If it's not that, please share a screenshot of what happens.

@Shafeeqvv
Copy link

hey Daveh can we do this work without hosting the site? i did this work on sublime text

@daveh
Copy link
Author

daveh commented Apr 23, 2024

@Shafeeqvv You need to be able to run PHP - the simplest way to do this is to install something like XAMPP and put the scripts in the root folder, then load them from there.

@troubleShoutersRUSL
Copy link

can u give me your personal mail ?
for asking a doubt regarding your SMTP video ??

@charlesdarwall1
Copy link

Hi Dave, I have a neubie q for you: No closing PHP ?> after line 35 of send-emai.php. HTTP ERROR 500

I have a hosted site on Ggodaddy.

@daveh
Copy link
Author

daveh commented Oct 22, 2024

@charlesdarwall1 If a file just contains PHP, it's not necessary to include the closing PHP tag. In fact it's recommended not to, as you can introduce hard to find whitespace.

If you're getting a 500 error, this means that PHP isn't configured to show you the actual details of the error. Try adding this to the top of your script:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Then the error message will tell you exactly what the error is and what line it's on.

@charlesdarwall1
Copy link

charlesdarwall1 commented Oct 22, 2024 via email

@Niyonkuru-olivier
Copy link

Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not connect to SMTP host. Failed to connect to server in C:\xampp\htdocs\form\vendor\phpmailer\phpmailer\src\PHPMailer.php:2282 Stack trace: #0 C:\xampp\htdocs\form\vendor\phpmailer\phpmailer\src\PHPMailer.php(2058): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array) #1 C:\xampp\htdocs\form\vendor\phpmailer\phpmailer\src\PHPMailer.php(1687): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Thu, 28 N...', 'ggggggggggggg\r\n') #2 C:\xampp\htdocs\form\vendor\phpmailer\phpmailer\src\PHPMailer.php(1521): PHPMailer\PHPMailer\PHPMailer->postSend() #3 C:\xampp\htdocs\form\send-email.php(33): PHPMailer\PHPMailer\PHPMailer->send() #4 {main} thrown in C:\xampp\htdocs\form\vendor\phpmailer\phpmailer\src\PHPMailer.php on line 2282 i meet with this error what i do

@Niyonkuru-olivier
Copy link

what I do about those error

@daveh
Copy link
Author

daveh commented Nov 29, 2024

@Niyonkuru-olivier It looks like your SMTP server address is either incorrect or the server is unreachable - check the address or try a different SMTP server

@Niyonkuru-olivier
Copy link

Niyonkuru-olivier commented Nov 29, 2024 via email

@Niyonkuru-olivier
Copy link

How I check those SMTP server address

@Niyonkuru-olivier
Copy link

SMTPDebug = SMTP::DEBUG_SERVER; $mail->isSMTP(); $mail->SMTPAuth = true; $mail->Host = "smtp.example.com"; $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = 587; $mail->Username = "[email protected]"; $mail->Password = "password"; $mail->setFrom($email, $name); $mail->addAddress("[email protected]", "Olivier"); $mail->Subject = $subject; $mail->Body = $message; $mail->send(); header("Location: sent.html");

@Niyonkuru-olivier
Copy link

this is my code. helps me

@daveh
Copy link
Author

daveh commented Nov 29, 2024

smtp.example.com is not a valid SMTP server, it's just an example. You need to use one you have access to (e.g. smtp.google.com if you have a Gmail account etc.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment