Skip to content

Instantly share code, notes, and snippets.

@daveh
Last active June 10, 2024 14:45
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 = "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>
@tobiholic
Copy link

Hi Dm-cr7,

I experienced the same. Best is to include

$mail->SMTPDebug = SMTP::DEBUG_SERVER;

in your .php file. It will print you out detail server information and you can find the issue easier. Dave also explains this in his video: https://www.youtube.com/watch?v=fIYyemqKR58&t=420s

After I uploaded the script to a web server, I could see the the SMTPAuth was not working and I knew where to check! Hope that helps you also.

@GuyCos
Copy link

GuyCos commented Mar 5, 2024

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

GuyCos commented Mar 5, 2024

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
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 ??

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