0

I am trying to call the mail function, but whenever I put it in the script, the page does not load.

I have the following code for my php.ini file in XAMPP:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=localhost
; http://php.net/smtp-port
smtp_port=80
auth_username = XX_MYEMAIL_XX
auth_password = XXXXX_MYPASSWORD_XX

I have a 64-bit computer, but an error message said it was missing a sendmail_from, so I gave this variable a value. I have Mercury running from XAMPP, but I don't know if I configured anything that needs to be configured.

I get the following error

mail(): Failed to connect to mailserver at "localhost" port 80, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

I used the following php code:

<?php     
$header = "From: varunsingh87@yahoo.com";
$to_email = 'VSpoet49@gmail.com';
$subject = 'Testing PHP Mail';
$message = 'This mail is sent using the PHP mail function';
if (mail($to_email, $subject, $message)) {
 echo "<p>Email sent!</p>";
} else {
 echo "<p>Email not sent.</p>";
}
?>

Below this is the default html tags.


Update

I removed the sendmail_from and set the smtp_port to 25.

mail(): Bad Message Return Path i


Related

Marvin
  • 853
  • 2
  • 14
  • 38

3 Answers3

1

Learn how to use PhpMailer and don't mess with this embarrassing mail function.

https://github.com/PHPMailer/PHPMailer/wiki/Tutorial

With this class you will send all messages with and without authorization, with or without tls/ssl and attachments (files, images).

!!! Install smtp server: hmailserver on localhost first !!!

https://www.hmailserver.com/download

And create your domain email mailbox.

Regards

Baranix
  • 11
  • 2
1

First, I never heard for mail server listening on port 80. I have XAMPP installed too, but with configured with "smpt_port=25".

Second, you have "SMTP=localhost" so in order to send email you must have installed mail server on your machine, for example "Mercury" from XAMPP package.

Third, it can be very tricky to properly send email using "mail()" function (authentication, spam detection...) so best solution is to avoid usage of "mail()" function and use some robust library/component/script for that.

It is good advice by Baranix to learn how to use PhpMailer or SwiftMailer (my favourite) and configure them to target real, well configured mail server on real hosting.

  • 1. I updated that 2. I have Mercury running but I don't know if I configured it 3. I understand that, but that doesn't mean the `mail()` function _shouldn't_ work, right? – Marvin Oct 12 '19 at 20:47
  • Sorry, I never used Mercury. "mail()" function will NOT work if "SMTP" and "smtp_port" settings are incorrect, mail server must be at that location and listenting at that port. – Miroslav Ćurčić Oct 12 '19 at 21:52
0

http://php.net/manual/language.operators.errorcontrol.php

Let us learn about the @ symbol, but be warned of a possible fake return status.

People typically try this first though

Try Catch Block