About sending emails

I have all this code: $mail = new PHPMailer();

    $mail->isSMTP();
    $mail->Host       = Config::SMTP_HOST;
    $mail->Username   = Config::SMTP_USER;
    $mail->Password   = Config::SMTP_PASSWORD;
    $mail->Port       = Config::SMTP_PORT;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth   = true;
    $mail->isHTML(true);

    $mail->setFrom('name@gmail.com', 'Recipient Name');
    $mail->addAddress($email);
    $mail->Subject = 'This is a test email';
    $mail->Body = 'Email body';

    if ($mail->send()) {
      echo "IT WAS SENT";
    } else {
      echo "NOT SENT";
    }

but when I run in google chrome it tells me not sent.

maybe setting:

 $mail->SMTPDebug  = 1;

will help show what the error is?

Enable debugging mode

$mail->SMTPDebug = 2;

and see what it tells you.

Is that port correct, to send via Gmail? What is the value in SMTP_PORT?

If I add this $mail->SMTPDebug = 2; or this one $mail->SMTPDebug = 1; it tells me this:2024-11-30 18:22:42 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
NOT SENT
and the port is: 465.

what’s the value for Config::SMTP_HOST?

These are the values:
$mail->Host = const SMTP_HOST = ‘sandbox.smtp.mailtrap.io’;

$mail->Port = const SMTP_PORT = 2525;

$mail->Username = const SMTP_USER = ‘94c23eb94ca034’;

$mail->Password = const SMTP_PASSWORD = ‘********2966’;
for User, Password, Host and Port.

well thats not gmail’s SMTP relay host…also you told us the port was 465, but there you say 2525.

It does not really matter if I write there 2525 or 465 in both manners it gives the same problem. And which is the gmail’s SMTP relay host could you tell me please.

Well it does matter if the server you’re connecting to doesnt open 2525 as a port.

If you’re trying to use mailtrap as a sending service, you’d need to give the specific server and port that they specify. I dont know where you got the sandbox server name, but from their documentation here:

It looks like the server should be either bulk or live, their recommended port is 587 (according to their screenshot, they dont open 465. Which… means yes, it does matter…), your username should be api, and the password should be the API key you generated.

If you’re using a different guide from mailtrap, i’d need to know where.

If you are trying to do it via gmail’s own SMTP,
Google’s Help page turns up the following.
https://support.google.com/a/answer/176600?hl=en

1 Like

I don’t know if this helps, but I made a comment about the port?

$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
$mail->Port = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

If I use that it will give me this error: 2024-12-01 17:18:14 Connection: opening to ssl://live.smtp.mailtrap.io:465, timeout=300, options=array()
2024-12-01 17:18:22 Connection: opened
2024-12-01 17:18:22 SERVER → CLIENT: 220 send.smtp.mailtrap.io ESMTP
2024-12-01 17:18:22 CLIENT → SERVER: EHLO localhost
2024-12-01 17:18:25 SERVER → CLIENT: 250-send.smtp.mailtrap.io250-PIPELINING250-SIZE 31457280250-AUTH LOGIN PLAIN250-ENHANCEDSTATUSCODES250 8BITMIME
2024-12-01 17:18:25 CLIENT → SERVER: AUTH LOGIN
2024-12-01 17:18:26 SERVER → CLIENT: 334 VXNlcm5hbWU6
2024-12-01 17:18:26 CLIENT → SERVER: [credentials hidden]
2024-12-01 17:18:28 SERVER → CLIENT: 334 UGFzc3dvcmQ6
2024-12-01 17:18:28 CLIENT → SERVER: [credentials hidden]
2024-12-01 17:18:32 SERVER → CLIENT: 535 5.7.8 Authentication failed
2024-12-01 17:18:32 SMTP ERROR: Password command failed: 535 5.7.8 Authentication failed
SMTP Error: Could not authenticate.
2024-12-01 17:18:32 CLIENT → SERVER: QUIT
2024-12-01 17:18:35 SERVER → CLIENT: 221 2.0.0 Bye
2024-12-01 17:18:35 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
NOT SENT

So it tells you what went wrong.

Your credentials are incorrect.

(I’m not chasing you across a dozen threads to solve one issue. Pick a topic and stick with it.)

In Reference To: How to send emails? - PHP - SitePoint Forums | Web Development & Design Community

Which one are you using? Which one are you setting your values on?

I am using the first one the $mail one

So all of those lines are doing nothing… If i cut out all of the unused lines, your code is currently this:

$mail = new PHPMailer();
        $mail->isSMTP();
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //This is equivalent to "ssl". If you're expecting TLS, this should be ENCRYPTION_STARTTLS
        $mail->SMTPAuth   = 'PLAIN'; // Not valid, should be a boolean.
        $mail->isHTML(true); //true, but message doesnt contain HTML.
        $mail->SMTPDebug = 3; //Showing all messages from both sides.
        $mail->setFrom('nasratullahkhadim00@gmail.com', 'Nasratullah Khadim');
        $mail->addAddress($email);
        $mail->Subject = 'This is a test email';
        $mail->Body = 'Email body';

but that is not working so what should I do

You should probably follow the actual guide you were pointed to.

Where are you getting things like

from?

At first I got things from a php course which I am learning by the way and the teacher is Edwin Diaz but those things did not work so I got them from mailtrap and these are also not working so what should I do?

Mailtrap definitely didnt tell you to make SMTPAuth a string.

Do you have an API token from Mailtrap?