1

I know this question has been asked many times, but I don't find an answer to my problem.

I have the following code:

require 'vendor/autoload.php';
    
use PHPMailer\PHPMailer\PHPMailer;
    
$mail = new PHPMailer();

$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myEmailAddress@gmail.com';
$mail->Password = 'myPass';
$mail->Port = 587; 

$mail->setFrom('myEmailAddress@gmail.com', 'Name Surname');
$mail->addAddress('myFriend@gmail.com', 'My Friend Mark');

$mail->Subject = 'Test';
$mail->isHTML(true);

$body = "<h1> TEST EMAIL </h1> <p> MY EMAIL IS AWESOME </p>";
$mail->Body = $body;

if($mail->send()){
    echo "SENT!";
}else{
    echo "Error ".$mail->ErrorInfo;
}

This works perfectly on my local server in Windows with XAMPP. When I try to run it on my OVH server I get

SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Can someone help me?

Mehrdad Noroozi
  • 620
  • 2
  • 13
Giuseppe Faraci
  • 290
  • 2
  • 11
  • 1
    You probably can't access an SMTP server from the OVH server. Don't use the SMTP mode. – Olivier Feb 19 '22 at 12:11
  • What can I use if I don't use SMTP? – Giuseppe Faraci Feb 19 '22 at 12:17
  • PHPMailer will just use the `mail()` PHP function. – Olivier Feb 19 '22 at 12:27
  • to use mail() function I should manually edit my php.ini file, shouldn't I? But I have not access to that file. I thought phpmailer is a good solution to avoid directly user mail function – Giuseppe Faraci Feb 19 '22 at 12:38
  • 1
    Why would you need to edit php.ini? Did you try removing the call to `isSMTP()`? – Olivier Feb 19 '22 at 12:41
  • Wow! It worked! Just by removing the call to isSMTP()! I still don't understand why it works, but thank you – Giuseppe Faraci Feb 19 '22 at 12:50
  • If you remove the call to isSMTP, it means you're using OVH's mail server. You could probably achieve the same, but safer and faster, by setting `$mail->Host = 'localhost';`. However, you cannot send from a gmail.com address via either of those routes as it will be classed as forgery by receivers. – Synchro Feb 22 '22 at 13:06

1 Answers1

0

You cannot use an external SMTP (such as smtp.gmail.com) with OVH Web hosting.

But you can use the OVH smtp server when using PHPMailer.

$mail->isSMTP();
$mail->Host = "ssl0.ovh.net";
$mail->Port = 465;
$mail->Username   = "yourmailovh";     
$mail->Password   = "yourpasswordovh";
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';

Hope it helps

2WFR
  • 121
  • 1
  • 8