-2

I am using PHPMailer - https://github.com/PHPMailer/PHPMailer to send an email with attachment. The mails gets sent to gmail/hotmail but they get send to "Junk", how do I avoid this? What could be the trustable domain from which I can easily send to User's inbox?

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require 'vendor/autoload.php';

require_once "./vendor/phpmailer/phpmailer/src/PHPMailer.php";
require_once "./vendor/phpmailer/phpmailer/src/Exception.php";
require_once "./vendor/phpmailer/phpmailer/src/POP3.php";
require_once "./vendor/phpmailer/phpmailer/src/SMTP.php";

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$email = new PHPMailer();
$email->From      = 'Steven123@gmail.com';
$email->FromName  = 'Steven';
$email->Subject   = 'PDF attachment';
$email->Body      = 'Hi';
$email->AddAddress( 'NAME_OF_THE_USER@gmail.com' );
$email->AddAttachment( "./PDF.pdf" , "PDF.pdf", 'base64', 'application/octet-stream' );
$email->Send();

?>
Mitch
  • 41
  • 5
  • 2
    You should use your SMTP credentials to actually send it _via_ your Gmail account. Right now, this is just an email “pretending” to be from `Steven123@gmail.com`, but send by a completely arbitrary machine on the internet. – CBroe Jul 15 '21 at 10:33
  • 2
    Does this answer your question? [php email - how to avoid mail ending up in spam box](https://stackoverflow.com/questions/9260910/php-email-how-to-avoid-mail-ending-up-in-spam-box) – Eric Landheer Jul 15 '21 at 10:33
  • @CBroe how do I use SMTP credentials when PHPMailer has it inbuilt? – Mitch Jul 15 '21 at 10:34
  • https://github.com/PHPMailer/PHPMailer#a-simple-example – CBroe Jul 15 '21 at 10:35
  • And specifically regarding Gmail, you might want to read https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#gmail-oauth2-and-allow-less-secure-apps as well. – CBroe Jul 15 '21 at 10:37
  • 1
    To expand on what @CBroe just mentioned about PHPMailer and Gmail, use the [XOAUTH2](https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2) way over "allow less secure apps", since Google have started to automatically disable that setting again after a period of time. – M. Eriksson Jul 15 '21 at 10:46

1 Answers1

0

Thank you for all the info, I got it working and tested it to send mails to gmail and hotmail. I used SMPT server of our own company and did something like this

$email = new PHPMailer();
$email->Host       = 'SMPT SERVER ADDRESS';                          //Set the SMTP server to send through
$email->SMTPAuth   = true;                                   //Enable SMTP authentication
$email->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
$email->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$email->From      = 'Steven123@gmail.com';
$email->FromName  = 'Steven';
$email->Subject   = 'PDF attachment';
$email->Body      = 'Hi';
$email->AddAddress( 'NAME_OF_THE_USER@gmail.com' );
$email->AddAttachment( "./PDF.pdf" , "PDF.pdf", 'base64', 'application/octet-stream' );
$email->Send();
Mitch
  • 41
  • 5