Problem with PHP PEAR Mail
Asked Answered
A

7

17

I am trying to use PEAR Mail to send using an external smtp server. It seems to hang for a while, then the script ends. It outputs all of my "echo" statements up till the one after the send. Nothing is output past the echo that says "before send". Can anyone tell me what might be wrong here? (dummy values substituted for smtp values). Mail is not being sent. Thanks for helping!

echo "start";
$n = $_POST['txtName'];
$e = $_POST['txtEmail'];
$t = 'Kenny <[email protected]>';
$f = 'Kenny <[email protected]>';
$s = 'CPA TEST';
$b = "name: $n email: $e"; 

include("mail.php");
echo "after include";
/* mail setup recipients, subject etc */
$recipients = $t;
$headers["From"] = $f;
$headers["To"] = $t;
$headers["Subject"] = $s;
$mailmsg = $b;
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "my_smtp_host";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "my_email";
$smtpinfo["password"] = "my_password";
echo "before object";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
echo "before send";
/* Ok send mail */
$send = $mail_object->send($recipients, $headers, $mailmsg);
echo "after send";
if (PEAR::isError($send)) { print($send->getMessage());}else{print "end";} 
echo "done";
Anelace answered 17/2, 2010 at 21:15 Comment(0)
M
6

Try this to ensure your mail is working:

<?php
require_once "Mail.php";

$from = "Sandra Sender <[email protected]>";
$to = "Ramona Recipient <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_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>");
 }
?>

If this does not work then you will need to check your PHP Configuration.

See http://php.net/manual/en/function.mail.php for more information.

Muscid answered 17/2, 2010 at 21:37 Comment(2)
Hi there. I tested your script and it has the same effect as the "real" code. It hangs, no warnings, no errors. my php config contains error_reporting = E_ALL | E_STRICT and display_errors = On. Any other suggestions?Pryer
Follow Rap and Jayme Dunlap's advice by running it on the command line to see the error.Muscid
H
49

If someone is running on linux and runs into the same problem as Jayme. Here is another simple solution for installing the missing "Net/" classes. These missing classes causes the script to interrupt.

sudo pear install Net_SMTP

Hardwick answered 2/1, 2013 at 22:8 Comment(3)
This is what fixed it for me.Moyers
On windows it was just pear install Net_SMTP but this helped me nonetheless!Pippas
You can also run apt install php-net-smtpKitchenette
W
16

I ran into the same issue where it hung on the send command. My first step was to run from the command line to see the full error message (as Rap suggested above).

php mymailsample.php

It spit out the following

Warning:  include_once(Net/SMTP.php): failed to open stream: No such file or directory in mail/Mail/smtp.php on line 348
PHP Warning:  include_once(): Failed opening 'Net/SMTP.php' for inclusion (include_path='.:/usr/lib/php') in mail/Mail/smtp.php on line 348
PHP Fatal error:  Class 'Net_SMTP' not found inmail/Mail/smtp.php on line 349

I downloaded the following, and put them in /Net

http://pear.php.net/package/Net_SMTP/download
http://pear.php.net/package/Net_Socket/download

I had to adjust the permissions of the SMTP and Socket libraries so they could be read by Apache.

And voila, it worked!

Womankind answered 7/7, 2011 at 16:21 Comment(1)
Or just run sudo pear install Net_SMTP if can run pear from the command line :)Alveta
M
6

Try this to ensure your mail is working:

<?php
require_once "Mail.php";

$from = "Sandra Sender <[email protected]>";
$to = "Ramona Recipient <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_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>");
 }
?>

If this does not work then you will need to check your PHP Configuration.

See http://php.net/manual/en/function.mail.php for more information.

Muscid answered 17/2, 2010 at 21:37 Comment(2)
Hi there. I tested your script and it has the same effect as the "real" code. It hangs, no warnings, no errors. my php config contains error_reporting = E_ALL | E_STRICT and display_errors = On. Any other suggestions?Pryer
Follow Rap and Jayme Dunlap's advice by running it on the command line to see the error.Muscid
O
4

The reinstall of channel://pear.php.net/Mail-1.2.0 does'nt work for me.(Ubuntu 12.04)

Just remove the caracter "&" before new

sudo vi /usr/share/php/Mail/smtp.php
    349         /*$this->_smtp = &new Net_SMTP($this->host, */
    350         $this->_smtp = new Net_SMTP($this->host,
Orlosky answered 25/2, 2014 at 17:47 Comment(0)
B
1

Todd's script is excellent, but will not solve your problem which is that your browser times out before the SMTP does. That is why you only see half your page and are not seeing any error messages that you can use to debug your SMTP settings.

The solution is to run the PHP script directly. No timeout.

If you can't do that because your ISP doesn't give you shell access, create a cron job to run every minute. Cron will email the output to you which will have full debug details.

Brutal answered 4/5, 2011 at 13:44 Comment(0)
R
1

For Php version 7 you need to install php-net-smtp:

sudo apt-get update
sudo apt-get install php-net-smtp
Rumple answered 1/4, 2021 at 11:50 Comment(0)
R
0

I've actually encountered the same problem. A production script broke down, and I had the hardest time troubleshooting it. Mainly because there was so much code that prevented errors from showing up. Ultimately, I used Kenny Ray's code, modified it to work for my environment, and ran a test. It turned out the Net_Socket somehow disappeared. I've uninstalled and re-installed it, and everything started working again. I hope this helps you.

Rostand answered 16/3, 2012 at 5:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.