1

I'm trying to make a php form which sends a gmail email to someone through php form.

I expected the gmail to arrive, because I typed in the mail($to,$userSubject,$body);

It didn't work, however. I'm using visual studio code's php live server extension, because I just couldn't set it up with xampp control panel.

Some code:

$userName = $_POST['name'];
$userEmail = $_POST['email'];
$userSubject = $_POST['subject'];
$message = $_POST['message'];

$to = "example@gmail.com";

$body = "";

$body .= "From: ".$userName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Message: ".$message. "\r\n";

When I run the code from the form I get an error message:
Warning: mail(): Failed to connect to mailserver at "smpt.gmail.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Users\Acer\Desktop\kenefagijnica\webform.php on line 31

I tried to use ini_set(), but I don't know what parameters to use to change the port so that I am able to send gmails.

I also tried looking at the php.ini file but I don't know what to do for the Mailer.

Help, anyone?

user123
  • 11
  • 3
  • please have a look on https://stackoverflow.com/questions/17392371/mail-function-wont-send-email-error – Merin Ovelil Nov 25 '20 at 08:09
  • Yes but... I'm not using xampp – user123 Nov 25 '20 at 08:11
  • oke.what you are using as server – Merin Ovelil Nov 25 '20 at 08:14
  • Change port from 25 to any other from documentation - many ISPs block this port so that might be the reason you cannot connect – Seti Nov 25 '20 at 08:16
  • I downloaded an extension: PHP serverby brapifra . It works well but there's something wrong with either the code, or the ini_set() function – user123 Nov 25 '20 at 08:17
  • From my experience when I tested SMTP locally using XAMPP it worked but when taking it online to a GoDaddy server for example I encountered many issues (due to ports) and it did not work. Bottom line is that if you are using a hosting provider such as GoDaddy I would go with a Sendgrid API call using a bearer token. – Luke Galea Nov 25 '20 at 08:17
  • Does this answer your question? [Using Gmail SMTP to send email with PHP](https://stackoverflow.com/questions/5759419/using-gmail-smtp-to-send-email-with-php) – El_Vanja Nov 25 '20 at 08:20
  • It doesnt sadly – user123 Nov 25 '20 at 08:45
  • I used ini_set('SMTP','gmail.com' ); ini_set('smtp_port',2525); Yet It still displays the same error – user123 Nov 25 '20 at 08:51
  • What is on line 31 in webform.php? – JM-AGMS Nov 25 '20 at 14:23
  • Have you tried other ports or just `2525`? [Here's](https://www.mailgun.com/blog/which-smtp-port-understanding-ports-25-465-587/) a relatively short read about SMTP ports. – El_Vanja Nov 25 '20 at 16:33
  • @JM-AGMS mail($to,$userSubject,$body); it says that i must use ini_set(), yet every port cant connect? – user123 Nov 25 '20 at 17:10
  • @El_Vanja I have , but none display the same error message : mail(): Failed to connect to mailserver at "smpt.gmail.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() – user123 Nov 25 '20 at 17:12

0 Answers0