Send mail with PHPMailer

Hello to the entire forum,

I want to send emails with PHPMailer, these emails contain attachments.

My hosting account is free, what steps should I follow? Do I have to change any settings on the hosting?

Thank you and greetings

Hi! Welcome to the forum! You need SMTP server before using the PHPMailer library. You can use SMTP server from the Google Mail to send emails. You can follow the example from Admin:

8 Likes

Just setup PHPMailer like normal with your SMTP credentials, no special configuration necessary!

7 Likes

Well, it still doesn’t work, I don’t know if it’s bad hosting configuration, or zoho.

And I see that a lot of people have a lot of problems with PHPMailer. :disappointed_face:

Neither does theI also don’t install it with Composer, which is what they recommend, but I don’t know how it works.

Well, I’ll keep looking. Thank you!!!

If you want us to help find out why it’s not working, please describe the problems you’re having. Especially any error messages you get would be very helpful.

Where do you get that idea from? Sure, a lot of people who want to send email (and are having trouble with it) talk about PHPMailer. But that’s not because PHPMailer is bad, it’s just because it’s basically the go-to option to send email with SMTP.

Composer is the de-facto way to manage third-party packages in your PHP project. It’s useful to learn!

But if you don’t feel like learning it, there is the php-download.com website that just delivers the package to you as an easy to use zip file: ▷ Download the PHP library phpmailer/phpmailer +++ One click!

4 Likes

If you can share the code your using (obviously removing credentials) I’m happy to take a look for you. I had a nightmare getting PHPMailer working. Not because its bad, but because its quite a learning curve.

Once its working, its a really wonderful tool

1 Like

Thank you very much admin, I will try it.

Greetings !!!

Hello dan3008.

Here I spend a code, the majority I’ve tried are like that.
I have followed the steps on the following website …

https://php-forum.com/index.php?threads/php-mail-and-phpmailer.30517/

In the hosting, I have the structure of folders like this …

And the code I use (testmail.php) is the following …
I have commented on attachments, to have fewer problems, and neither send the mail.
And neither does the echo of the “error” or “Email Sent Successfully!”

I have not tried it with accounts of Zoho,Only with those of Gmail.

<?php

echo 'PHP_Mailer';

require 'PHPMailer-6.9.3/src/PHPMailer.php';
require 'PHPMailer-6.9.3/src/SMTP.php';
require 'PHPMailer-6.9.3/src/Exception.php';


//--------------------------------------------------

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

$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = '********';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom([email protected]', 'Your Name');
$mail->addAddress([email protected]', 'Recipient Name');
$mail->isHTML(true);
$mail->Subject = 'Subject of the Email';
$mail->Body = 'Body of the Email';

//--------------------------------------------------

//$file_path = '/path/to/your/attachment.pdf'; // Path to the attachment
//$mail->addAttachment($file_path, 'filename.pdf'); // Adjust filename as needed

//$file_path = '/'; // Path to the attachment
//$mail->addAttachment($file_path, 'XDS2000.pdf'); // Adjust filename as needed

//--------------------------------------------------

if (!$mail->send()) {
echo 'Error: ' . $mail->ErrorInfo;
} else {
echo 'Email sent successfully!';
}

Thanks for everything !!!
Greetings !!!

are you using your gmail password or an app password?

I made that mistake. You need to generate and use an app password, not your normal login password

4 Likes

Hello @dan3008

I could already solve it, there were these failures.

1 - It had to activate the verification in 2 steps, in the sending account.

2 - Create a password app on the sender’s account, and use the 16 digit number in the password, Because with the normal password, to enter the account it does not work. (Give an SMTP connection error, which is the mistake that always gave me.)

3 - In the github code that passed the link, I had this …

require 'PHPMailer-6.9.3/src/PHPMailer.php';
require 'PHPMailer-6.9.3/src/SMTP.php';
require 'PHPMailer-6.9.3/src/Exception.php';

And I saw that when opening the folder 'phpmailer-6.9.3 , Inside this folder there was another with the same name, And inside there were the php codes, so I put …

require ‘PHPMailer-6.9.3/PHPMailer-6.9.3/src/PHPMailer.php’;
require ‘PHPMailer-6.9.3/PHPMailer-6.9.3/src/SMTP.php’;
require ‘PHPMailer-6.9.3/PHPMailer-6.9.3/src/Exception.php’;

Maybe all the codes that tested worked, but they were not going well, for not making the first 2 previous steps.

Well, finally solve it !!!He had more than 2 weeks, and there was no way to send an email.

Now I just have to try, attach files.

But I think before, I will take a coffee or a beer !!!

Thank you all. Greetings!!!

2 Likes

hahahahaha

Right now I was writing it to you !!!
Greetings !!!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.