1
votes

I'm trying to make code to send a simple mail via SMTP, I read that I must use the class phpmailer and also install pear engine on the server root, I downloaded some different files to authenticate the SMTP but always requires more files that I don't have or cant be charged. Actually, the PHP errors display this:

Warning: require_once(): open_basedir restriction in effect. File(/opt/plesk/php/7.1/share/pear/PEAR.php) is not within the allowed path(s): (/var/www/vhosts/necotec.es/:/tmp/) in /var/www/vhosts/necotec.es/httpdocs/prueba_smtp/Mail.php on line 48

Warning: require_once(/opt/plesk/php/7.1/share/pear/PEAR.php): failed to open stream: Operation not permitted in /var/www/vhosts/necotec.es/httpdocs/prueba_smtp/Mail.php on line 48

Fatal error: require_once(): Failed opening required 'PEAR.php' (include_path='.:/opt/plesk/php/7.1/share/pear') in /var/www/vhosts/necotec.es/httpdocs/prueba_smtp/Mail.php on line 48

I dont know if cant be found or is a permision problem the files by default takes 644 permisions in this server. Any clue would be helpfull thanks.

1

1 Answers

0
votes

open_basedir restriction in effect

error means that some files or scripts are located out of allowed directory. In your case this file is /opt/plesk/php/7.1/share/pear/PEAR.php.

In Plesk you can disable open_basedir(not secure): Domains > example.com > PHP Settings and set open_basedir to none.

Another way(more secure) is to set open_basedir as {WEBSPACEROOT}{/}{:}{TMP}{/}:/opt/plesk/php/7.1

I was able to configure PHPMailer on my test server(Plesk 17.5-17.8) using steps:

  1. Logged in to the server using SSH
  2. Went to domain Document root directory: # cd /var/www/vhosts/example.com/httpdocs/
  3. Run the command taken from github: # composer require phpmailer/phpmailer
  4. As a result "vendor" folder appeared in "httpdocs" folder.
  5. Created a testmail.php file based on 0-send-email-plesk.php:

    <?php
    
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    //Load Composer's autoloader
    require 'vendor/autoload.php';
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 2; # 0 off, 1 client, 2 client y server
    $mail->CharSet  = 'UTF-8';
    $mail->Host = 'localhost';
    $mail->Port = 25;
    $mail->SMTPSecure = 'tls'; # SSL is deprecated
    $mail->SMTPOptions = array (
        'ssl' => array(
            'verify_peer'  => true,
            'verify_depth' => 3,
            'allow_self_signed' => true,
            'peer_name' => 'Plesk',
        )
    );
    $mail->SMTPAuth = true;
    $mail->Username = 'sender@example.com';
    $mail->Password = 'password';
    $mail->setFrom('sender@example.com', 'Name Surname');
    $mail->addAddress('recipient@domain.tld', 'Name Surname');
    $mail->Subject = 'Email subject';
    $mail->msgHTML('Email content with <strong>html</strong>');
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
    ?>
    

    It assumes that local mail server is used and Default Plesk self signed certificate is used by mail server: Plesk > Tools & Settings > SSL/TLS Certificates > Certificate for securing mail