4

I'm new to php and I'm tying to set up a simple contact form script. I keep getting this message:

Warning: mail(): Bad Message Return Path in C:\xampp\htdocs\php_101\action.php on line 16

Here's my php code. Thanks:

ini_set('SMTP','smtpout.secureserver.net');
ini_set('smtp_port',80);
$name = $_POST['name'];
$to = "bgreen@oblivy.com";
$from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
echo $subject, " from ", $name, " at ", $from, "<br/>";
echo $message;
$retval = mail( $to, $subject, $message );
Binyamin Green
  • 83
  • 1
  • 2
  • 11
  • You sure your SMTP port is 80? – Devon Bessemer Jul 18 '18 at 14:20
  • No. I couldn't find my php.ini file. i'm using Xampp, and it has a php.ini-production and -developement file. I changed the ini-production file to smtp_port = 80. My smtp is supposed to be 80, among others. Do you know what port gmail would be? – Binyamin Green Jul 18 '18 at 14:26
  • 3
    SMTP port is almost certainly not 80, HTTP runs on port 80. SMTP is usually 25, 465, and 587 depending on the provider and SSL/TLS settings. – Devon Bessemer Jul 18 '18 at 14:28
  • Is this the incoming or outgoing? It says outgoing is either 80, 465, 3535 or 25. Thanks. – Binyamin Green Jul 18 '18 at 14:32
  • 2
    Read the second note in the section about the `additional_headers` argument, [in the documentation](https://secure.php.net/manual/en/function.mail.php#refsect1-function.mail-parameters), about adding a `From` header. I think this might apply to your case. You probably haven't configured a `From` address in `php.ini` and are not setting one explicitly in your code either. – Decent Dabbler Jul 18 '18 at 14:53
  • In my php.ini-production file, It says the following `[mail function] ; For Win32 only. ; http://php.net/smtp SMTP = smtpout.secureserver.net ; http://php.net/smtp-port smtp_port = 465 ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = bgreen@oblivy.com ` – Binyamin Green Jul 18 '18 at 15:21
  • What is the output of `ini_get( 'sendmail_from' );`? Is it the same as `sendmail_from` you just pasted? – Decent Dabbler Jul 18 '18 at 15:27
  • Also, see what happens if you explicitly set the `From` header through the `additional_headers` argument of `mail()`. (Please view the docs for how to do this, if you don't already know how to.) – Decent Dabbler Jul 18 '18 at 15:29
  • Lastly, if all that fails and/or appears to be properly configured, are you sure you are allowed to send mail through that SMTP server with the `From` address you are using? Perhaps the SMTP server is telling you the `From` address is not allowed to send mail through them. – Decent Dabbler Jul 18 '18 at 15:30
  • I've added ` ini_get( 'sendmail_from' ); ini_set('SMTP','smtpout.secureserver.net'); ini_set('smtp_port',465);` It just keeps loading. – Binyamin Green Jul 18 '18 at 15:33
  • Sorry, I meant what is the output of `echo ini_get( 'sendmail_from' );`. – Decent Dabbler Jul 18 '18 at 15:33
  • Same. and same without `echo ini_get( 'sendmail_from' );` – Binyamin Green Jul 18 '18 at 15:35
  • So, are you telling me `echo ini_get( 'sendmail_from' );` doesn't print out an e-mailaddress? If so, you indeed need to add a `From` address to `mail()`, because the `php.ini` configuration that is used by your installation doesn't have one configured. – Decent Dabbler Jul 18 '18 at 15:36
  • I have. it just keeps loading. `$retval = mail( $to, $subject, $message, "From:bgreen@oblivy.com" );` – Binyamin Green Jul 18 '18 at 15:39
  • 1
    From the [docs on secureserver.net](https://products.secureserver.net/email/email_outlook.htm) it appears you need to activate SMTP relaying, but more importantly that it needs authentication, but authentication is not supported by `mail()`, so I think you are out of luck. However, [you could use other mail packages](https://stackoverflow.com/questions/10455469/authentication-php-mail) that support authentication. – Decent Dabbler Jul 18 '18 at 15:50

2 Answers2

3

As per your comments, you're trying to use Gmail server to deliver messages to oblivy.com. Google will never allow that unless you're a registered and authenticated user. Servers that accept messages from anonymous parties for any destination are called open relays:

An open relay (sometimes called an insecure relay or a third-party relay) is an SMTP e-mail server that allows third-party relay of e-mail messages. By processing mail that is neither for nor from a local user, an open relay makes it possible for an unscrupulous sender to route large volumes of spam. In effect, the owner of the server -- who is typically unaware of the problem -- donates network and computer resources to the sender's purpose. In addition to the financial costs incurred when a spammer hijacks a server, an organization may also suffer system crashes, equipment damage, and loss of business.

They became a problem in the late 1990s and blacks lists of open relays were created, so mail server administrators could just reject any incoming mail from such servers.

Thus you need to create a Gmail account and provide its credentials when sending mail. Additionally, if you enable two-factor authentication you also need a dedicated application key to use here (instead of your regular password).

And to send email with authentication you need a full-fledged mail library that supports SMTP-AUTH (and possibly encryption). Good old mail() doesn't.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
2

In my case that was commented

sendmail_path=...

in php.ini file

See https://www.phpflow.com/php/how-to-send-email-from-localhost-using-php/

giacoder
  • 980
  • 8
  • 21