0

I wrote some code to sends emails, but those are kept be viewed as spam. I tested it using Gmail. Can any body help me to figure out what's wrong?

I am 100% sure that the following parameters are correct:

$mail->Host
$mail->Username
$mail->Password
$mail->Subject    (contains $subject)
$mail->Body       (contains $message)
$mail->setFrom
$mail->addReplyTo (equals $mail->setFrom)
$to               (contains mail recipient)

I also used $mail->addCustomHeader("BCC: bccmail@gmail.com").

Below the code. I think it's a header issue.

function php_mailer_send_mail ($to,$subject,$message) {
    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions

    try {
        //Server settings
        $mail->SMTPDebug = 2;                                 // Enable verbose debug output
        // $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'https://xxx.xxx.xxx.xxx:XXXX';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'myUsername';                 // SMTP username
        $mail->Password = 'mypassword';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                    // TCP port to connect to

        //Recipients
        $mail->setFrom('emailfrom@mydomain.com', 'Keyword Translation Factory');
        $mail->addAddress($to, 'Welcome User');     // Add a recipient
        $mail->addReplyTo('emailfrom@mydomain.com', 'Keyword Translation Factory');
        // $mail->addCC('ccmail@gmail.com');
        $mail->AddBCC('bccmail@gmail.com');

        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $subject;
        $mail->Body    = $message;
        $mail->AltBody = $message;

        $mail->send();
        // echo 'Message has been sent';
    } catch (Exception $e) {
        echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
    }
}
Amessihel
  • 5,891
  • 3
  • 16
  • 40
  • 2
    Possible duplicate of [How do you make sure email you send programmatically is not automatically marked as spam?](https://stackoverflow.com/questions/371/how-do-you-make-sure-email-you-send-programmatically-is-not-automatically-marked) – Johan Oct 30 '18 at 11:47
  • 1
    `$mail->Host` needs to be a **host**, not a URL. Commenting out `isSMTP()` means you're not sending via SMTP, so none of your SMTP settings make any difference. – Synchro Oct 30 '18 at 13:29

0 Answers0