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>");
}
?>
Related
Using code similar to below: On my server side I get an error that seems to imply it is trying use a certificate rather than the user/pass I'm providing (also below). Anyone else seen that, and have you gotten it to work? I've tried googling the error but I seem to just get a bunch of irrelevant results. Linux/PHP7.2.5 (also fails on PHP5.4.20) BUT seems to work just fine under Windows/PHP7
SSL_accept error from mail.XXXXXX.com[xxx.xxx.xxx.xxx]: 0
warning: TLS library problem: 20353:error:14094418:SSL routines:SSL3_READ_BYTES:tlsv1 alert unknown ca:s3_pkt.c:1293:SSL alert number 48:
<?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";
$username = "smtp_username";
$password = "smtp_password";
$port = 465;
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => 'PLAIN',
'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>");
}
?>
The relevant part of the error message is
unknown ca
which means "certificate is signed by an unknown certificate authority".
Check if the root CA list is up to date on the machine.
Hi I´m trying to send mails on localhost to my gmail account. I tried the postfix tutorial on the MAMP Pro website but that did not work for me.
Is there any way to enable sending mails from and to my gmail-account on localhost?
I think you will want to authenticate and send directly with the gmail server. For reasons related to spam prevention, I have seen issues with sending from a local machine to and from my gmail account.
You could check out the PEAR Mail package. Using it is fairly simple (code borrowed from another site):
<?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>");
}
?>
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>");
}
?>
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 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);