To send email from an app, you need an email account. This example uses Gmail. To enable your email account
For security purposes, while signing-in using your new PHP app, you may need to allow access.
composer require phpmailer/phpmailer
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception;
require 'path-to/PHPMailer/src/Exception.php'; require 'path-to/PHPMailer/src/PHPMailer.php'; require 'path-to/PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing 'true' enables exceptions
$mail->isSMTP(); $mail->Mailer = "smtp"; // Set mailer to use SMTP
$mail->SMTPDebug = 1; // Enable verbose debug output $mail->SMTPAuth = TRUE; // Enable SMTP authentication $mail->SMTPSecure = "tls"; // Enable TLS encryption, 'ssl' (a predecessor to TSL) is also accepted $mail->Port = 587; // TCP port to connect to (587 is a standard port for SMTP) $mail->Host = "smtp.gmail.com"; // Specify main and backup SMTP servers $mail->Username = "youremail@gmail.com"; // SMTP username $mail->Password = "yourpassword"; // SMTP password
$mail->setFrom('from-email@gmail.com', 'name-is-optional'); $mail->addAddress('to-email@virginia.edu', 'name-is-optional'); $mail->addAddress('another-to-email@virginia.edu'); $mail->addReplyTo('reply-to-email@virginia.edu', 'name-is-optional'); $mail->addCC('cc-email@example.com'); $mail->addBCC('bcc-email@example.com');
$mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Subject line goes here'; $mail->Body = 'Body text goes here';
$mail->send();
Download complete example code (text version).
To deploy PHP mail service locally (using XAMPP), refer to PHP deployment (XAMPP). To deploy PHP mail service on UVA CS, refer to PHP deployment (CS). You may put PHPMailer library in the directory where you host your PHP mail service. Be sure to verify the path.
To deploy PHP mail service on GCP, refer to PHP deployment (GCP).
<?php switch (@parse_url($_SERVER['REQUEST_URI'])['path']) { case '/': require 'mail-example.php'; break; case '/PHPMailer/src/PHPMailer.php': require 'PHPMailer/src/PHPMailer.php'; break; case '/PHPMailer/src/Exception.php': require 'PHPMailer/src/Exception.php'; break; case '/PHPMailer/src/SMTP.php': require 'PHPMailer/src/SMTP.php'; break; default: http_response_code(404); exit('Not Found'); } ?>
# Use the PHP 7.3 runtime by replacing "php72" below with "php73" runtime: php73 # Defaults to "serve index.php" entrypoint: serve index.php
Sometimes we may want to allow users to send information to the company's or designated email address using a web form (example usage). Let's modify the PHP program to accept a user's input, construct an email containing the input, and automatically send the email to a designated email address.
Let's modify mail-example.php. Add the following form
<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" /> <title>Example: Form and PHPMailer</title> </head> <body class="container"> <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> <div class="form-group"> <label for="name">Your name (optional):</label> <input type="text" class="form-control" name="name" /> </div> <div class="form-group"> <label for="comment">Comment:</label> <textarea class="form-control" name="comment" required></textarea> </div> <div> <input type="submit" class="btn btn-secondary form-control" value="Send email" /> </div> </form> </body> </html>
<?php $name = $comment = null; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $name = $_POST['name']; if (!empty($_POST['comment'])) $comment = $_POST['comment']; } ?>
if ($comment != null) { $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Subject line goes here'; // name and comment are retrieved from the form data $mail->Body = 'Comment = ' . $comment . ', From ' . $name; $mail->send(); // Send email if comment is entered echo 'Thank you. Your comment has been sent.'; }
Download complete example code (text version).
Released under the
CC-BY-NC-SA 4.0 license.