PHP 4 Send email through SMTP - php

I have a PHP 4 code that needs to be modified to send emails through SMTP and I have already installed the Pear mail package on the server. It is Sevrer 2008 R2 64 bit. The problem is that it does not send any emails! what am I missing here?The Mail.php is located in a different folder than the actual website files so I had to specify the path in the "Include". The name of the file is Vistiro.php3: Please advice:
Function PostSlip() {
Global $_parent,$_contact,$_problem,$_summary;
$p_contact = addslashes($_contact);
$p_problem = addslashes($_problem);
$p_summary = addslashes($_summary);
$p_now = time();
$qstring="insert into slips (spid,o_date,status,priority,o_tech,problem,summary,is_public,l_activity,l_tech,ctag) values ($_parent,$p_now,'O',4,'$p_contact','$p_problem','$p_summary','0','$p_now','$p_contact','ANONYMOUS')";
$qhandle = db_query($qstring);
$count = db_affected_rows($qhandle);
$newsid = db_insertid($qhandle);
RecalcSlip($_parent);
echo "<tr><td bgcolor=#808080 width=100% align=center>\n";
if ($count > 0) {
echo "Thank you for your submission.<br>";
echo "Your Slip ID is <b>$newsid</b><br>";
echo "Use this number at a later date to check on the status of this entry.";
include "/inetpub/PHP_New/PEAR/Mail.php";
$from = "username#domain.com";
$to = "username#domain.com";
$subject = "Test email using PHP SMTP with SSL\r\n\r\n";
$body = "This is a test email message";
$host = "smtp.companyname.com";
$port = "25";
$username = "domain\username";
$password = "email password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$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>");
}
} else {
echo "An error occured during the update. Sorry, please try again later.";
}
echo "<br>Return to Browsing";
}

Related

PHP PEAR Godaddy server SMTP connection refused

I'm facing the below exception
Failed to connect to smtpout.asia.secureserver.net:465 [SMTP: Invalid
response code received from server (code: -1, response: )]
I'm using PHP 7.1.12-3+ubuntu14.04.1+deb.sury.org+1 (cli) and PEAR Version: 1.10.5.
Here's my PHP code:
<?php
try {
require_once "Mail.php";
$from = "support#domain.com";
$to = "user#gmail.com";
$subject = "Testing email please ignore";
$message = "Just testing";
$host = "smtpout.asia.secureserver.net";
$port = 465;
$username = "support#domain.com";
$password = "password";
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $message);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
} catch(Exception $e) {
echo $e;
}
?>
Do I need to change any settings in Godaddy?
If you are using their(Godaddy) email address,SMTP_SERVER should be localhost.
If you are using google server to send the emails change SMTP_SERVER accordingly.
hope this helps
$host: "localhost" //use godaddy server as smtp

Pear mail not working without any error message

I am trying to using Pear mail on my hostgator server. Firstly I've installed the mail package using cpanel and net_smpt was already installed, however when I required the Mail.php class it insisted that the Net folder be within the Mail folder - they were both at the same level before. So I've moved Net inside Mail and it stopped complaining - but maybe I've just messed up everything (maybe not: let me know).
This is my test script, mostly copied from tutorials:
<?php
echo 'Test Pear<br>';
require_once("/home3/myaccountname/php/Mail.php");
$from = "info#mydomain.com";
$to = "myrealemail#gmail.com";
$subject = "Test pear";
$body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit...";
$params['host'] = 'gator3015.hostgator.com';
$params['port'] = '465';
$params['username'] = 'info#mydomain.com';
$params['password'] = 'mypassword';
$params['debug'] = 'true';
$headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', $params);
echo 'good so far';
$mail = $smtp->send($to, $headers, $body);
echo 'Still there';
if ( PEAR::isError($mail) ) {
echo("<p>Error sending mail:<br/>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message sent.</p>");
}
?>
The SMTP parameters have been tested using the codeigniter email class, where they succesfully send emails (but as cronjobs and codeigniter refuse to work for me I'm trying to use Pear Mail instead). It requires SSL but apparently it's on by default with Pear Mail.
I've made sure the PHP error messages were displayed for my test. What happens is that I see all my echos except for "Still there" so something goes wrong when I call $smtp->send(). However no error message is displayed, despite setting debug as true. The if(PEAR::isError($mail)) doesn't seem to have any effect either. And no email is sent.
So I'm a bit at a loss to know what's going wrong. Any idea?
UPDATE: the host does not support SMTP for some reason, which explains why I had the weird issue. Using Mandrill works fine.
Try this example,
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$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>");
}
?>
Also this one,
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://mail.example.com";
$port = "465";
$username = "smtp_username";
$password = "smtp_password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$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>");
}
?>

simple way to add smtp to existing mail.php

I have an existing email.PHP that sends emails based on certain functions with in my website, similar to forum notifications.
However its very hit n miss with sending them, worked well for two days then just stopped.
My question is is there a simple way to add smtp instructions to the existing script so that it will use Gmail rather than PHP mail()
I can supply current script if required.
Thanks in advance.
You need to install PEAR Mail. Then you can do it like this.
<?php
require_once "Mail.php";
$from = "<from#gmail.com>";
$to = "<to#yahoo.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "myaccount#gmail.com"; //<> give errors
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$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>");
}
?>

Change email header in php pear mail

I'm trying to figure out how send email from php using google's SMTP servers, but change the FROM header to match my domain. The email sends fine, but the recipient sees it sent from myemail#gmail.com, rather than me#mydomain.com. I'm using php pear mail to send.
require_once("Mail.php");
$from = "Me <me#mydomain.com>";
$to = "Zach <myemail#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "smtp.gmail.com";
$username = "****";
$password = "****";
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
You must set a ssl encryption. Google can help you http://www.google.com/search?q=pear+mail+ssl

How to use pear mail mime

How can you use pear mail mime with google. I found this which lets you use pear mail with google, but not mail mime: http://globalconstant.scnay.com/2009/11/06/sending-email-through-gmail-using-php/
require_once "Mail.php";
require_once "Mail/mime.php";
$from = "Sender <*******#googlemail.com>";
$to = "Receiver <*******#googlemail.com>";
$subject = "Welcome to SITENAME!";
$crlf = "\n";
$html = "<h1> This is HTML </h1>";
$headers = array('From' => $from,
'To' => $to,
'Subject' => $subject);
$host = "smtp.gmail.com";
$port = 465;
$username = "********#googlemail.com";
$password = "********";
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);
$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 $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";
I keep getting
Failed to add recipient: #localhost
[SMTP: Invalid response code received
from server (code: 555, response:
5.5.2 Syntax error. f52sm5542930wes.35)]
Edit:
The email is now received, however it turns out like this:
This is a message I sent from PHP using=
the PEAR Mail package and SMTP through Gmail. Enjoy!
It looks like you have an issue with the email header. I updated your code based on the pear mail doc (http://pear.php.net/manual/en/package.mail.mail-mime.example.php):
require_once "Mail.php";
require_once "Mail/mime.php";
$from = "Sender <*******#googlemail.com>";
$to = "Receiver <*******#googlemail.com>";
$subject = "Welcome to SITENAME!";
$crlf = "\n";
$html = "<h1> This is HTML </h1>";
$headers = array('From' => $from,
'To' => $to,
'Subject' => $subject);
//$host = "smtp.gmail.com";
$host = "ssl://smtp.gmail.com"; // try this one to use ssl
$port = 465;
$username = "********#googlemail.com";
$password = "********";
//$mime = new Mail_mime($crlf);
$mime = new Mail_mime(array('eol' => $crlf)); //based on pear doc
$mime->setHTMLBody($html);
//$body = $mime->get();
$body = $mime->getMessageBody(); //based on pear doc above
$headers = $mime->headers($headers);
$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 $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";
It works for me so I hope it will work for you!
Cheers,
Erez
#john: Using the code from the link you posted, modify it like so --
<?php
require_once('Mail.php');
require_once('Mail/mime.php');
$from = 'Sender <sender#gmail.com>';
$to = 'Receiver <receiver#something.com>';
$subject = 'Sent from PHP on my machine';
$text = 'This is a message I sent from PHP '
. 'using the PEAR Mail package and SMTP through Gmail. Enjoy!';
$message = new Mail_mime();
$message->setTXTBody(strip_tags($text)); // for plain-text
$message->setHTMLBody($text);
$body = $message->get();
$host = 'smtp.gmail.com';
$port = 587; //According to Google you need to use 465 or 587
$username = 'sender';
$password = 'your_password';
$headers = array('From' => $from,
'To' => $to,
'Subject' => $subject);
$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 $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";
?>
Update:
Edit:
The email is now received, however it turns out like this:
This is a message I sent from PHP using=
the PEAR Mail package and SMTP through Gmail. Enjoy!
#john: Update
$body = $mime->get();
to
$body = $mime->get(array('text_charset' => 'utf-8'));
and try again.
$body = $mime->get(array('text_charset' => 'utf-8'));
In addition to the above you need html_charset for html emails.
$crlf = "\n";
$body = $mime->get(array('html_charset' => 'utf-8', 'text_charset' => 'utf-8', 'eol' => $crlf));
This will fix the abberations like  in the e-mails.
Couldn't comment on StealthyNinja's answer so I posted my own, sorry about that.
The question is also a bit old but I maybe this could be useful to others.
To get rid of all that HTML tags and weird characters you have to prepare your header so the e-mail client can read the e-mail right. Try this AFTER setting your $headers array:
$headers = $message->headers($headers);
It should work ok after that.
I have used this code to remove 3D after = sign.
$hdrs = array( 'From' => $from,
'To' => $to,
'Subject' => $subject );
$mime =& new Mail_mime();
$mime->setTXTBody($message);
if($htmlMessage==""){
$htmlMessage=$message;
}
$mime->setHTMLBody($htmlMessage);
if($attachmentIsFile){
if($attachment!=null)
$mime->addAttachment($attachment,'application/octet-stream',$attachmentName.extractExtension($attachment));
}else{
if($attachment!="")
$mime->addAttachment($attachment,'application/octet-stream',$attachmentName,false);
}
$body = $mime->get(array('text_encoding' => '8bit','html_encoding' => '8bit'));
$hdrs = $mime->headers($hdrs);

Categories