MAMP PRO PHP Mail - Gmail - php

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>");
}
?>

Related

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

problem in sending mail by gmail smtp in server but works fine in localhost

I have opened a site in a free web hosting company which is not offering mail() facility so I have decided to implement this facility of sending emails using G-mail's SMTP server.
I have used PEAR' Mail package to send mails. It works fine locally but unfortunately its not all working in my website's server.
When the page is opened, it produces a blank white screen even no errors are being mentioned. You can check here to see the script run.
This is my code:
<?php
require_once "Mail.php";
$from = "username#gmail.com";
$to = "username#yahoo.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "username#gmail.com";
$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>");
}
?>
Can anyone help me to understand what's going wrong?
You maybe forgot to upload the Mail.php file or one of the dependencies. A white page often indicates a fatal error.
Gmail blocks some web hosting providers. Contact you your web hosting provider.

sending Emails using PEAR's Mail in PHP is working fine on Windows Dev System but not on Linux Server?

I am using following code to send email using mail.google
<?php
require_once "Mail.php";
$from = "someone#somedomain.com";
$to = "otherone#somedomain.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "mygmailaccount#gmail.com";
$password = "mygmailpassword";
$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>");
}
?>
This code works fine on Windows Development Machine but not on my Amazon Linux Server ?
Please tell me if any additional configuration is required.
Mail.php isn't the only file needed for sending mails via SMTP. You need the following files:
Mail.php
PEAR.php
PEAR5.php
Mail/smtp.php
Net/SMTP.php
Net/Socket.php
Mail/RFC822.php
At least these files were needed when I tried out your example on my own linux server.
I don't know how this Amazon web stuff is working but you probably need to look at some log-file or enable error messages in HTML output somehow or increase the warning level to find out what's really going wrong.
If you want to install the files manually you can download them from here:
http://pear.php.net/package/PEAR/download/
http://pear.php.net/package/Mail/download/
http://pear.php.net/package/Net_Socket/download/
http://pear.php.net/package/Net_SMTP/download/

Categories