简体   繁体   中英

How to use mail() in php with gmail

Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\\xampp\\htdocs\\series\\mailoutput\\index.php on line 8 There was an error sending the email.

I am getting this message when I run the php code. I have this in a folder, I am doing a tut on php from thenewboston. Here is where my file is located C:\\xampp\\htdocs\\series\\mailoutput\\index.php

here is the code in which I am running.

<?php
$to = 'myemail@gmail.com';
$subject = 'This is an email';
$body = 'This is a test email\n\nHope you got it.';
$headers = 'From: someone@gmail.com';


if (mail($to, $subject, $body, $headers))
  {
    echo 'Email has been sent. '.$to;
  }
  else
  {
    echo 'There was an error sending the email.';
  }
 ?>

My php.ini file has these settings

SMTP = ssl://stmp.gmail.com
smtp_port = 465
sendmail_from = myemail@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

In my sendmail.ini file these are the settings

smtp_server=ssl://stmp.gmail.com
smtp_port=465
auth_username=myemail@gmail.com
auth_password=(mypassword)
force_sender=myemail@gmail.com

Not sure why this wont work or if there is an easier way to make this work.

First of all, you've misspelled the hostname here:

smtp_server=ssl://stmp.gmail.com
                  ^^^^

The hostname is smtp .gmail.com, not stmp .gmail.com.

That being said, don't use GMail to send mail from your web application. GMail isn't intended for this type of use - you can only send about 500 messages from a GMail account every day, and any spam complaints related to bulk email from such an account will be dealt with very harshly.

If you want to send email from a server that does not have its own mail server, use a dedicated mail delivery service such as Amazon SES, Sendgrid, Mailgun, etc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM