Last active
June 29, 2025 23:37
-
-
Save daveh/1164348fe21a6e7363d28c7b94c9eb3f to your computer and use it in GitHub Desktop.
Send email with PHP (code to accompany https://youtu.be/fIYyemqKR58)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
How to check those SMTP server address
…On Fri, Nov 29, 2024 at 9:24 PM Dave Hollingworth ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
@Niyonkuru-olivier <https://github.com/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
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/daveh/1164348fe21a6e7363d28c7b94c9eb3f#gistcomment-5307531>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BBVCZQMIFCADHBKYHOVJP6L2DAQC5BFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTCNZXHEYDKMRVU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you were mentioned.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
How I check those SMTP server address
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");
this is my code. helps me
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
@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