0

I am quite new to php and i am tying to use php mailer to send emails. y code is giving me a success message but the email is not being delivered. Here is my code.

<?php
use phpmailer\phpmailer\PHPMailer;
use phpmailer\phpmailer\SMTP;
use phpmailer\phpmailer\Exception;

require 'vendor/phpmailer/phpmailer/src/Exception.php';
require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/phpmailer/src/SMTP.php';

//PHPMailer Object
$mail = new PHPMailer;

//From email address and name
$mail->From = "mail@gmail.com";
$mail->FromName = "Elvis ";

//To address and name
$mail->addAddress("mail1@gmail.com", "Elvis");
$mail->addAddress("mail2@gmail.com"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("mail1@gmail.com", "Reply");

//CC and BCC
//$mail->addCC("cc@example.com");
//$mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
 $mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send())
{
       echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
    echo "Message has been sent successfully";
}

i installed phpmailer using composer at first but i was giving me an error PHP Fatal error: Uncaught Error: Class 'PHPMailer' on the logs so i included the files manually.

Elvis Kirui
  • 81
  • 1
  • 3
  • 11
  • mail received from local MTA, and mail delivered to the user in-box are not the same thing. –  May 19 '18 at 05:35
  • There are many reasons why a mail might not be delivered. The main reason for this might be that you're not using an official mail server. Services like GMail, Hotmail and many others use the [The Spamhaus Block List](https://www.spamhaus.org/sbl/) to block emails coming from private servers like servers running at home. This is to prevent a lot of spam and abuse. You could verify your server's IP address here: https://www.spamhaus.org/lookup/ to check if this is what's happening. – icecub May 19 '18 at 05:36

1 Answers1

1

You're sending using the default mail() transport, which means messages are sent through your local mail server, so look in its logs, usually somewhere like /var/log/mail.log, and that will tell you what happens to messages after you've submitted them.

Synchro
  • 35,538
  • 15
  • 81
  • 104