Skip to content

Instantly share code, notes, and snippets.

@daveh
Last active October 30, 2025 09:47
Show Gist options
  • Select an option

  • Save daveh/1164348fe21a6e7363d28c7b94c9eb3f to your computer and use it in GitHub Desktop.

Select an option

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 = "you@example.com";
$mail->Password = "password";
$mail->setFrom($email, $name);
$mail->addAddress("dave@example.com", "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>
@GuyCos

GuyCos commented Mar 5, 2024

Copy link
Copy Markdown

Greetings Mr. Daveh,
I would like to send you my appreciations for making these instructions available. I followed the steps you provided on this video: https://youtu.be/fIYyemqKR58 . But from its segment which time starts from 3 minutes 53 seconds, you recommended to install the PHP Mailer Package.

Would you please help me understand how to install the PHP Mailer Package on the cPannel provided by the website host?

Also, in the video segment starting from 4 minutes 05 seconds, you said: "On the command line from the same folder as our code, we'll run the command to install the PHP Mailer package." Would you please help me understand how to process this information you made available?

I will greatly appreciate your help,
Guy

@GuyCos

GuyCos commented Mar 5, 2024

Copy link
Copy Markdown

Greetings Mr. Daveh,

From the youtube video you provided, you said:
"on the command line from the same folder as our code we'll run the command to install the PHP Mailer package"

May I please know where to find that specific "command line" you mentioned in your video?

Thank you in advance,
Guy

@daveh

daveh commented Mar 5, 2024

Copy link
Copy Markdown
Author

@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

GuyCos commented Mar 7, 2024 via email

Copy link
Copy Markdown

@GuyCos

GuyCos commented Mar 7, 2024 via email

Copy link
Copy Markdown

@daveh

daveh commented Mar 7, 2024

Copy link
Copy Markdown
Author

@GuyCos Is the vendor folder inside the public_html folder?

@GuyCos

GuyCos commented Mar 7, 2024 via email

Copy link
Copy Markdown

@GuyCos

GuyCos commented Mar 7, 2024 via email

Copy link
Copy Markdown

@daveh

daveh commented Mar 8, 2024

Copy link
Copy Markdown
Author

@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

GuyCos commented Mar 8, 2024 via email

Copy link
Copy Markdown

@jcdynasteel

Copy link
Copy Markdown

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

daveh commented Apr 5, 2024

Copy link
Copy Markdown
Author

@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
Copy Markdown

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

@daveh

daveh commented Apr 23, 2024

Copy link
Copy Markdown
Author

@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
Copy Markdown

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

@charlesdarwall1

Copy link
Copy Markdown

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

daveh commented Oct 22, 2024

Copy link
Copy Markdown
Author

@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

charlesdarwall1 commented Oct 22, 2024 via email

Copy link
Copy Markdown

@Niyonkuru-olivier

Copy link
Copy Markdown

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
Copy Markdown

what I do about those error

@daveh

daveh commented Nov 29, 2024

Copy link
Copy Markdown
Author

@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

Niyonkuru-olivier commented Nov 29, 2024 via email

Copy link
Copy Markdown

@Niyonkuru-olivier

Copy link
Copy Markdown

How I check those SMTP server address

@Niyonkuru-olivier

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

@Niyonkuru-olivier

Copy link
Copy Markdown

this is my code. helps me

@daveh

daveh commented Nov 29, 2024

Copy link
Copy Markdown
Author

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