Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

email

smtp

pear

I have this script:

require_once "Mail.php";

 $from = "Stephen <[email protected]>";//Google apps domain
 $to = "[email protected]";
 $subject = "Hi!";
 $body = "Hi,\n\nHow are you?";

 $host = "mail.nvrforget.com";
 $username = "[email protected]";
 $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 am coming 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.

like image 779
chromedude Avatar asked Aug 18 '11 21:08

chromedude


People also ask

How to Send email via PHP script?

The PHP mail() function allows sending emails directly from a script. This function returns true for the successful delivery of email, otherwise returns false. PHP mail() function uses sendmail_path value from ini file. For Unix systems the default value is used as /usr/sbin/sendmail or /usr/lib/sendmail.

What is PHP mail?

PHP mail is the built in PHP function that is used to send emails from PHP scripts. The mail function accepts the following parameters: Email address. Subject. Message.


1 Answers

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

This is a non-fatal notice coming from PHP because PEAR Mail is prehistoric and hasn't been updated to use the static keyword introduced five years ago in PHP5.

After reviewing the documentation, your call to Mail::factory looks completely correct and normal.

You failed to tell us if if the call to send succeeds or fails. If it's succeeding, but the mail is never being delivered, please check the SMTP server logs. If it's failing, what's the actual error message? The Mail::send documentation includes a comprehensive list of errors.

You might want to consider using a more modern mail sending library, like Swiftmailer.

like image 183
Charles Avatar answered Oct 26 '22 16:10

Charles