0

Possible Duplicate:
Send email using GMail SMTP server from PHP page

I'm pretty new to mail in PHP, and I am wanting to set up mail().

The problem is, after a few hours of trying to get it working, I simply can not!

I am wanting this to happen:

I want to send emails to the users on my website via my gmail address.

  1. I am not sure WHERE i configure SMTP for gmail. Do i edit the settings in php.ini (ssl:smtp.gmail.com; 465)?

  2. Is there a way to send emails using the PHP mail() function without the need to use something like pear? I am just wanting to use the mail() function.

  3. If that is not possible, is there a way I can send emails to my users via the localhost setup?

I am pretty confused after looking around for answers during the past few hours.

Any help would be greatly appreciated!

Community
  • 1
  • 1
zuc0001
  • 924
  • 4
  • 12
  • 27
  • You really, really don't want to use `mail`. Really. Especially if you're SMTPing. – Charles Dec 13 '12 at 01:11
  • duplicate of this question. [http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page](http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page) Please use the solution posted there, it's nice. – Max Dec 13 '12 at 01:12

1 Answers1

1

The easiest way I've found to get PHP to send mail using SMTP is via the Mail Pear package.

This way you don't have to involve and obese third party libraries like PHPMailer.


Here's an example:

<?php
require_once "Mail.php";

$headers = array(
  'From' => "Sandra Sender <sender@example.com>",
  'To' => $to="Ramona Recipient <recipient@example.com>",
  'Subject' => "Hi!"
);

$smtp = Mail::factory('smtp', array(
  'host' => "ssl://smtp.gmail.com",
  'port' => 465,
  'auth' => true,
  'username' => "smtp_username",
  'password' => "smtp_password"
));

$body = "Hi,\n\nHow are you?";

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo $mail->getMessage();
}
else {
  echo "mail sent successfully";
}
maček
  • 76,434
  • 37
  • 167
  • 198
  • Do note that while PEAR's Mail package will do the job well, it's really, really old code. You're going to see plenty of strict notices. This is OK. – Charles Dec 13 '12 at 01:12
  • Thanks. I mentioned I didn't want to use it, but I guess its my only option. I downloaded the Pear mail files, but Pear is not installed on my IIS server. I'm a bit stuck, if you could help me? – zuc0001 Dec 13 '12 at 01:13
  • @zuc0001, sorry, I'd recommend just using PHPMailer then. – maček Dec 13 '12 at 01:19