PHP Email Sending Error - php

I am trying to edit PHP code that sends an email using PEAR. The below code has worked on the company server, but doesn't seem to work on my computer when I am using localhost.
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
//Send the Actual Mail
$smtp = Mail::factory('smtp',
array ( 'host' => $host,
'auth' => false,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail))
{
echo($mail->getMessage());
}
else
{
echo("<p>Message successfully sent!<p>");
}
The items in $headers are defined earlier, don't worry.
The error returned by echo($mail->getMessage()); is as follows:
Failed to connect to localhost:25 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]
I'm on a machine running Ubuntu. Not sure if any other information is needed.

Related

Set time or sending limit for PHP mailing script

I have a simple php registration form that captures a name and email address. After registering, I have a confirmation email set to go to the registrant and one to go to the admin.
I've had an issue where the user only registered once, but some issue caused the email to be sent 50+ times to both the registrant and admin. It's only happened a few times, but I'm trying to prevent it from happening in the future.
I'm thinking of a time limit on running the script, but wondering the best way to handle this. Below is a snippet of the mailing portion of the script.
2 notes, in case they are of use:
I'm using PEAR Mailer
My mail is being sent via MailGun
$headers = array ('From' => $from,
'To' => $to,
'Bcc' => $bcc,
'Reply-to' => $email,
'Subject' => $subject,
'MIME-Version' => 1,
'Content-type' => 'text/html;charset=iso-8859-1');
$headers_admin_notification = array ('From' => $from_admin_notification,
'To' => $to_admin_notification,
'Subject' => $subject_admin_notification,
'MIME-Version' => 1,
'Content-type' => 'text/html;charset=iso-8859-1');
// credentials for SMTP Authentication included from config file
$smtp = Mail::factory('smtp', array ('host' => 'ssl://smtp.mailgun.org',
'auth' => true,
'port' => 465,
'username' => 'XXXXX',
'password' => 'XXXXX'));
$mail = $smtp->send($to, $headers, $body);
$mail_admin_notification = $smtp->send($to_admin_notification, $headers_admin_notification, $body_admin_notification);
if (PEAR::isError($mail) || PEAR::isError($mail_admin_notification)) {
echo("<p>" . $mail->getMessage() . "</p>");
echo("<p>" . $mail_admin_notification->getMessage() . "</p>");
}
else {
header("Location: LINK-TO-MY-CONFIRMATION-PAGE?fname=$fname&session=$readable_date_and_time");
}
Thanks!

Error! authentication failure

I am working on a php website,Using google smtp to send mail,The code is working fine and it is sending mail to inbox, Through google smtp.But now have changed the $username and $password,It is showing me this error,Thankyou in advance
Error! authentication failure [SMTP: Invalid response code received
from server (code: 534, response: 5.7.14 Please log in via your web
browser and 5.7.14 then try again. 5.7.14 Learn more at 5.7.14
https://support.google.com/mail/answer/78754 y11-v6sm11496340pfn.92 - gsmtp)]
<?php
require_once 'Mail.php'; //this loads up PEAR Mail
if (!class_exists('Mail')) { die('Error: The PEAR Mail class does not exist.'); }
$host = 'ssl://smtp.gmail.com';
$username = '****#gmail.com'; //replace this with your new Gmail address
$password = '****'; //replace this with your new Gmail account password
$port = '465';
$from_email = $username;
$from = '"california worker company" <'.$from_email.'>'; //put your name in here, but keep the double quotes
$to = '"california worker company" <softphoton002#gmail.com>'; //put in your name and main email address to send a test to
$subject = 'California Worker Lead for GENERAL CONTRACTORS';
$myname = $_REQUEST['name'];
$myemail = $_REQUEST['email'];
$myphone = $_REQUEST['phone'];
$annualpayroll = $_REQUEST['annualpayroll'];
$numberofemployees = $_REQUEST['numberofemployees'];
$yourbusinesstype = $_REQUEST['yourbusinesstype'];
$healthinsurancecarrier = $_REQUEST['healthinsurancecarrier'];
$payrollservicemethod = $_REQUEST['payrollservicemethod'];
$body = 'Hello Admin <br/> You get a new lead for GENERAL CONTRACTORS.<br/>
Name:'.$myname.'<br/>
Email:'.$myemail.'<br/>
Phone:'.$myphone.'<br/>
Annual Payroll:'.$annualpayroll.'<br/>
Member of Employees:'.$numberofemployees.'<br/>
Business Type:'.$yourbusinesstype.'<br/>
Health Insurance Carrier:'.$healthinsurancecarrier.'<br/>
Payroll Service Method:'.$payrollservicemethod.'<br/>';
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject,
'Reply-To' => $from_email,
'MIME-Version' => 1,
'Content-type' => 'text/html;UTF-8'
);
$params = array(
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password
);
$smtp = Mail::factory('smtp', $params);
$mail = $smtp->send($to, $headers, $body);
// To user email
$headerss = array(
'From' => $from,
'To' => $myemail,
'Subject' => $subject,
'Reply-To' => $from_email,
'MIME-Version' => 1,
'Content-type' => 'text/html;UTF-8'
);
$paramss = array(
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password
);
$smtps = Mail::factory('smtp', $paramss);
$bodys = 'Hello '.$myname.'<br/>
We received your message for Quote on GENERAL CONTRACTOR. We will reply you in next 24-48 hours.<br/>
Thank you';
$mails = $smtps->send($myemail, $headerss, $bodys);
if (PEAR::isError($mail)) {
echo '<p>Error! '.$mail->getMessage().'</p>';
} else {
header("Location: http://californiaworkercomp.com/general-contractors?success=1");
//echo '<p>Your test message was sent successfully.</p>';
}
?>

PHP pear-mail smtp not working

When I try to run this code, I get a strange error I cannot find an answer to:
// Pear Mail Library
require_once "Mail.php";
$from = 'someone#gmail.com';
$to = 'someoneelse#gmail.com';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'someone#gmail.com',
'password' => 'password'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
The error is:
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) (code: -1, response: )]
I looked for an answer but could not find it anywhere. Please help.
i have the same problem and mine stopped working suddenly. My suspicion is the SSL certificates associated with my name server, or in your case your name server are expired. My hosting site has incompetent help as they have been at it for two weeks and still can't figure it out. So check the name server SSL certificates.
The unknown error code of -1 can be confusing but when it shows up on Pear Mail, then most likely you need to install Mail and Mail_Mime
pear install Mail Mail_Mime
SMTP errors, if any, will always be specifically reported. If this also happens, then install Net_SMTP also.

Error while sending mail using PEAR Mail package

I am trying to send Mail through PHP Pear Mail package, I have 2 scenarios here , In first scenario mail is sending properly without any error but in second scenario I am getting error message .
Scenario 1 :
File Name : testmail.php
<?php
require_once "Mail.php";
$from = "erp#askspidy.com";
$to = "support#askspidy.com";
$subject = "Landing Page Enquiry From -";
$body = "Hello just testing";
$host = "ssl://mail.askspidy.com";
$port = "465";
$username = "support#askspidy.com";
$password = "askspidy#1234";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'MIME-Version' => 1,
'Content-type' => 'text/html;charset=iso-8859-1'
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'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 I directly run file in url then mail is sent without any errors .
Scenario 2 :
File Name : sendmail.php
<?php
function send_mail($subject,$body)
{
require_once "Mail.php";
$from = "erp#askspidy.com";
$to = "support#askspidy.com";
$host = "ssl://mail.askspidy.com";
$port = "465";
$username = "support#askspidy.com";
$password = "askspidy#1234";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'MIME-Version' => 1,
'Content-type' => 'text/html;charset=iso-8859-1'
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
}
?>
Now i am calling this send_mail function from different file like this
File Name : service/process.php
<?php
require_once("../sendmail.php");
$subject="Landing page enquiry";
$email_body="Hello Just Testing! ";
send_mail($subject,$email_body);
?>
When this file is executed into browser , i am getting error message on line
send_mail($subject,$email_body);
Error :
Warning: include_once(Net/SMTP.php): failed to open stream: No such file or directory in /usr/local/lib/php/Mail/smtp.php on line 348
Warning: include_once(): Failed opening 'Net/SMTP.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /usr/local/lib/php/Mail/smtp.php on line 348
Fatal error: Class 'Net_SMTP' not found in /usr/local/lib/php/Mail/smtp.php on line 349
In Scenario 1 everything is working fine then why in scenario 2 I am getting this error, i guess there is problem with path but i am not sure what should be the path and where should i include that.
Folder Structure :
Include code in file Mail/smtp.php
require_once "Net/SMTP.php";
Note: I have manually installed PEAR Package in Cpanel account and done no settings in php.ini file
Added dirname(FILE) to the path name everywhere in code and it worked
require_once dirname(__FILE__)."/Net/SMTP.php";
You can use __DIR__ for the directory path for current file
require_once __DIR__ . "/Net/SMTP.php";

Sending mail via pear mail

Sorry for my English.
I’m trying to sent e-mai via Mail pear packet:
require_once "Mail.php";
$from = '<frommail#gmail.com>';
$to = '<tomail#vacant.lv>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'frommail#gmail.com',
'password' => 'pass'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
//email addresses are changed
But in result I have error:
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: Permission denied (code: -1, response: )]
Openssl is enabled.
Thanks you.
At some stage you might want to send an HTML email, so you would also need to use the Mail/mime package that is provided by PEAR. I've included that as well.
<?php
require_once "Mail.php";
require_once "Mail/mime.php";
$from = "<noreply#example.com>";
$to = "fred#example.com"; // the email address
$host = "smtp.gmail.com";
$port = "587";
// use the first example username if sending from gmail, as full email address
// is required
$username = "fred.flintstone#gmail.com";
$username = "fred";
$password = "secure";
$headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
$mailbody = "<html><body>...</body></html>";
$mime = new Mail_mime();
$mime->setHTMLBody($mailbody);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory(
'smtp',array (
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password,
'port' => $port
)
);
// send email
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo "<b><Center>Succesfully sent email to</b>$to</center>";
}

Categories