What is wrong with this PHP script to send mail using Pear Mail?

I have this script:

require_once "Mail.php"; $from = "Stephen < username@nvrforget.com >";//Google apps domain $to = " username@gmail.com "; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; $host = "mail.nvrforget.com"; $username = " username@nvrforget.com "; $password = "password"; $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo("<p>" . $mail->getMessage() . "</p>"); } else { echo("<p>Message successfully sent!</p>"); } 

I come up with this error:

 Non-static method Mail::factory() should not be called statically 

Any idea how to fix this? Pear Mail is installed on the server.

+6
source share
3 answers

The non-static Mail :: factory () method should not be called static

This is a non-fatal notification coming from PHP because PEAR Mail is prehistoric and not updated to use the static introduced five years ago in PHP5.

After looking at the documentation, your call to Mail::factory looks absolutely correct and normal.

You did not tell us if the send call succeeds or fails. If this happens but the mail never arrives, check the SMTP server logs. If this fails, what is the error message? The Mail::send documentation contains a complete list of errors.

You might want to use a more modern mail library, such as Swiftmailer .

+11
source

perhaps this is due to the missing ampersand?

In the documentation examples, I notice that using factory looks like this:

 // Create the mail object using the Mail::factory method $mail_object =& Mail::factory('sendmail', $params); 

Note the setting with = &

-2
source

added @ to all pear / mail calls. someday you can end up with Mail :: factory () should not be called a static error

-2
source

Source: https://habr.com/ru/post/895366/


All Articles