Reputation: 27
I've followed a yt-tutorial on how to send an email with PHPMailer. After doing this I ended up with the code below, but it doesn't work for me. I don't get any error messages and I don't get any emails either. What could be the reason to this? Have I missed something? I am using XAMPP as a local server to run my code.
require_once ('PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = '465';
$mail->isHTML();
$mail->Username = '[email protected]';
$mail->Password = 'password';
$mail->SetFrom('[email protected]');
$mail->Subject = 'Hello World';
$mail->Body = 'A test email!';
$mail->AddAddress('[email protected]');
$mail->Send();
Upvotes: 0
Views: 6258
Reputation: 114
Download the php mailer from https://github.com/PHPMailer/PHPMailer and use it. To use it: first you need to create a folder called "phpmailer". And then put the php mailer folder you downloaded in it. then use this code:
<?php
header("Access-Control-Allow-Origin: *");
header('Access-control-Allow-Headers: Authorization,Content-Type ,X-Auth-Token , Origin');
$username=$_POST["email"];
$password=$_POST["password"];
$to=$_POST["to"];
$subject=$_POST["subject"];
$body=$_POST["body"];
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
require 'phpmailer/src/Exception.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $username; // SMTP username
$mail->Password = $password; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption; ` PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('[email protected]',$username);
$mail->addAddress($to); // Add a recipient
// Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody =;
$mail->send();
echo "<script>";
echo "window.alert('Email was sent')";
echo "</script>";
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Also make sure you turn "less secure app access" on for this to work. Because such codes use existing email to send emails. The easiest way is to use your gmail account.
Upvotes: 2