Skip to content

Instantly share code, notes, and snippets.

@daveh
Last active April 28, 2024 11:56
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • 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>
@Yechiel17
Copy link

image

It’s message error !

@Yechiel17
Copy link

It’s it a message error !

@daveh
Copy link
Author

daveh commented Jun 1, 2023

@Yechiel17 You either need to include Composer's autoloader as done in the source code above, or load the classes manually as described here.

@Yechiel17
Copy link

the error is resolved but now in the part of the script how can the message sent from my site land directly on my email address knowing that it is a professional address?

@daveh
Copy link
Author

daveh commented Jun 7, 2023

@Yechiel17 This might help

@kisdel
Copy link

kisdel commented Jul 3, 2023

Fatal error: Uncaught Error: Class "phpmailer\phpmailer\src\PHPMailer" not found in C:\xampp\htdocs\send-email.php:13 Stack trace: #0 {main} thrown in C:\xampp\htdocs\send-email.php on line 13

@Kpvadhiya
Copy link

Kpvadhiya commented Aug 24, 2023

Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Keyurvadhiya\contact.php on line 8

Fatal error: require(): Failed opening required 'vendor/autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\Keyurvadhiya\contact.php on line 8

So how to add vendor file

Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting in C:\xampp\htdocs\Keyurvadhiya\PHPMailer\src\PHPMailer.php:1845 Stack trace: #0 C:\xampp\htdocs\Keyurvadhiya\PHPMailer\src\PHPMailer.php(1587): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Thu, 24 A...', 'class="control-...') #1 C:\xampp\htdocs\Keyurvadhiya\PHPMailer\src\PHPMailer.php(1423): PHPMailer\PHPMailer\PHPMailer->postSend() #2 C:\xampp\htdocs\Keyurvadhiya\contact.php(35): PHPMailer\PHPMailer\PHPMailer->send() #3 {main} thrown in C:\xampp\htdocs\Keyurvadhiya\PHPMailer\src\PHPMailer.php on line 1845

@daveh
Copy link
Author

daveh commented Aug 25, 2023

@Kpvadhiya You need to use Composer to install PHPMailer

@konstantinosiakovou
Copy link

konstantinosiakovou commented Dec 18, 2023

Thank you a lot for your work. I learned a lot. The only think I did not is that I sent an email via form with my 2nd account but at inbox there is no appear 2nd email but the email I set.

@daveh
Copy link
Author

daveh commented Dec 19, 2023

@konstantinosiakovou Check your spam folder, or try a different SMTP server / account

@konstantinosiakovou
Copy link

@konstantinosiakovou Check your spam folder, or try a different SMTP server / account

Thank you so much for your reply. I will think about it.

@tobiholic
Copy link

tobiholic commented Feb 17, 2024

Hi Dave!
thanks for the awesome youTube video, helped my a lot. It's working for me, the form is submitting and I am receiving an mail. Only issue is, that the $mail->setFrom($email, $name); is not working.
I am using smtp gmail with my account, and the mail always comes from Tobixxx@gmail.com. Any ideas how I could set the From status to the variable and input from the form? Or do I have to use another server?

Thanks for your help already.

Cheers, Tobi

@daveh
Copy link
Author

daveh commented Feb 18, 2024

@tobiholic Hi Toby - you can only send email from the account you're authenticating with. So if you're using Gmail, the email sent from PHP will always come from your Gmail account. (Otherwise you could send email that looked like it came from someone else) However, in this case what we tend to do is set the reply-to address, so that when you reply to the email, it replies to another address and not the sender. So like this:

$mail->addReplyTo($email, $name);

Then you'll get the email from your Gmail account, but when you click reply, the email client will reply to the other email address.

@tobiholic
Copy link

awesome, that also helps! Thanks for your quick reply.

@Dm-cr7
Copy link

Dm-cr7 commented Feb 20, 2024

hello dave please help me i have this problem"This page isn’t workingIf the problem continues, contact the site owner.
HTTP ERROR 405
"

@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.

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