1

php.ini

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = "ssl://smtp.gmail.com"
; http://php.net/smtp-port
smtp_port = 465  

I would like to send email in php using gmail SMTP server and did change on php.ini(See above code). Also add $host, $port, $username and $pw into the sendEmail.php.

However, email still can not be sent out. Can anyone help me? Thanks!

sendEmail.php

 $host = "ssl://smtp.gmail.com";
 $port = "465";
 $username = "username@googlemail.com";
 $password = "password";

 $email_to = "n.liu@the-imcgroup.com";
 $email_subject = "Test E-Mail";
 $email_body = "Email Body";

 if(mail($email_to, $email_subject, $email_body)){
    echo "The email was successfully sent.";
 } else {
    echo "The email was NOT sent.";
 }

?>

hakre
  • 193,403
  • 52
  • 435
  • 836
Acubi
  • 2,793
  • 11
  • 41
  • 54
  • Have you looked at http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page ? – Pateman Nov 18 '11 at 15:33

1 Answers1

4

Things will very likely be much easier for you if you use a library like PHPMailer for this purpose:

http://phpmailer.worxware.com/

Using this class will aid in setting proper headers, configuring addressing and ensuring better deliverability in general.

Here's an example connecting to the gmail SMTP just like you described:

http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/

DeaconDesperado
  • 9,977
  • 9
  • 47
  • 77
  • +1 Also, if you're working in a framework, it may well have email functions built-in that serve a similar purpose to phpmailer. – eaj Nov 18 '11 at 15:35