I need help, phpmailer is going to spam. Is there any wrong config?
This is the new format I want to use, before I used it as in the example https://github.com/PHPMailer/PHPMailer#a-simple-example.
With this new format, it is much cleaner when sending the email.
require './vendor/autoload.php';
include 'assets/class/Email.php';
$email = new Email();
$email->add(
"Email de teste",
"Corpor da mensagem",
"Tiago",
"para@gmail.com"
)->send();
if (!$email->error()){
var_dump(true);
}else{
echo $email->error()->getMessage();
}
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
class Email
{
/** @var PHPMailer */
private $mail;
/** @var stdClass */
private $data;
/** @var Exception */
private $error;
public function __construct()
{
$this->mail = new PHPMailer(true);
$this->data = new stdClass();
//Server settings
//$this->mail->SMTPDebug = SMTP::DEBUG_SERVER;
$this->mail->isSMTP();
$this->mail->Host = 'srv.aaaa.com';
$this->mail->SMTPAuth = true;
$this->mail->Username = 'sac@aaaa.net';
$this->mail->Password = 'aaaa';
$this->mail->SMTPSecure = 'tls'; //PHPMailer::ENCRYPTION_STARTTLS
$this->mail->SMTPOptions = array('ssl' => array('verify_peer' => false,'verify_peer_name' => false,'allow_self_signed' => true));
$this->mail->CharSet = "utf-8";
$this->mail->Port = 587; //587 ou 465
$this->mail->Encoding = '7bit';
$this->mail->setFrom('sac@aaaa.net', 'Aaaaa');
// Content
$this->mail->isHTML(true);
$this->mail->setLanguage("br");
}
public function add(string $subject, string $body, string $recipient_name, string $recipient_email): Email
{
$this->data->subject = $subject;
$this->data->body = $body;
$this->data->recipient_name = $recipient_name;
$this->data->recipient_email = $recipient_email;
return $this;
}
public function attach(string $filePath, string $fileName): Email
{
$this->data->attach[$filePath] = $fileName;
return $this;
}
// pubf
public function send(string $from_name = "Hostcia", string $from_email = "sac@hostcia.net"): bool
{
try {
$this->mail->Subject = $this->data->subject;
$this->mail->msgHTML($this->data->body);
$this->mail->addAddress($this->data->recipient_email, $this->data->recipient_name);
$this->mail->setFrom($from_email, $from_name);
if (!empty($this->data->attach)) {
foreach ($this->data->attach as $path => $name) {
$this->mail->addAttachment($path, $name);
}
}
$this->mail->send();
return true;
} catch (Exception $exception) {
$this->error = $exception;
return false;
}
}
// : ?Exception -> O retorno pode ser nulo ou um Exception
public function error(): ?Exception
{
return $this->error;
}
}
I am very grateful to anyone who can help me.
There's probably nothing wrong with your PHP code, after all the email was send.
The mailserver you're using: srv.aaaa.com
needs to be validated by Google, since you sending the mail to GMail. Every mailserver you send mail to has slightly different rules, about which mails they will accept and which not, but in general you need to set up these things:
See Google: Enhance email security.