4

I'm on Ubuntu VM.

I have this PHP

<?php

# --------------------------------------------------------------------------------
# Goal    : send an email
# Run     : curl 45.55.88.57/code/mail.php  | php


$to      = 'email@gmail.com';
$subject = '';
$message = 'hello';
$headers = 'From: john@gmail.com' . "\r\n" .
    'Reply-To: john@gmail.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

I ran this :

curl 45.55.88.57/code/mail.php  | php

I get this

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   176  100   176    0     0      2      0  0:01:28  0:01:00  0:00:28    45
<html>
<head><title>504 Gateway Time-out</title></head>
<body bgcolor="white">
<center><h1>504 Gateway Time-out</h1></center>
<hr><center>nginx</center>
</body>
</html>

Is my code is wrong, or is something wrong with my VM?

I don't get any emails.

halfer
  • 19,824
  • 17
  • 99
  • 186
code-8
  • 54,650
  • 106
  • 352
  • 604
  • 1
    Suggestion - STOP using PHP mail. Use proper SMTP server, buy it or configure it yourself and use library like SWIFT mailer. https://stackoverflow.com/questions/4565066/why-shouldnt-i-use-phps-mail-function – Vytenis Ščiukas Mar 02 '19 at 14:42
  • 1
    The mail function is not standalone. It requises a proper sendmail config. Check your server mail config http://php.net/manual/en/mail.requirements.php – Tuckbros Mar 08 '19 at 23:20
  • Where would u do that config ? – code-8 Mar 09 '19 at 00:24
  • well, it is config of the system command for sending mail (sendmail). defining smtp server, credentials to connect to it... – Tuckbros Mar 09 '19 at 07:49
  • is this hosted or you have it in your home ? ISP ? – Vidal Mar 11 '19 at 12:39
  • @kyo first you have to test it from the `CLI` inside from the VM to see if the PHP can send emails; if it can, then the problem relies in nginx/PHP(FPM?) configuration. You can use [`set_time_limit(0)`](http://php.net/manual/en/function.set-time-limit.php) at the top of your PHP script to have a no-time limited script execution. If the `CLI` php command fails, then you have to configure `sendmail` on the VM. – Christos Lytras Mar 14 '19 at 07:01

6 Answers6

4

dont forget to set your gmail setting. follow this tutorial for setting google smtp.

Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
vrie
  • 446
  • 4
  • 8
3

I did have this issue a couple of times and if you are in your home developing your ISP maybe blocking the port 25.

I saw that you are on the US, for example xfinity, att and others are blocking the port 25. This text is copied from their web-page Xfinity

From Xfinity page
Why is Port 25 for Email Submission Not Supported?

Email is used for important communications and Comcast wants to ensure that these
communications are as secure and as private as possible. As such, 
Comcast does not support port 25 for the transmission of email by our 
residential Internet customers. Much of the current use of port 25 is by 
computers that have been infected by malware and are sending spam 
without the knowledge of the users of those computers. 

ATT is doing the same, if you google you can find a lot of forum posts on the matter.

How to test
Do a telnet against different mail servers. Ex.

telnet gmail.com 25
telnet yahoo.com 25
telnet hotmail.com 25

This will test if your outbound connection is being block. Some ISP just drop the traffic and you will get a time out error, other can route the traffic to nowhere and you will get the gateway time out. This is the usual approach to eliminate DoD attacks and over saturate the routers CPU.

You should also test your inbound connection, almost all mail servers now connect back to test if your domain is valid and match your ip address if not it is going to get drop or send to junk or spam folder. Try to telnet from the outside to your ip address. Also if you are using NAT or PAT you should also have the appropriate port mappings to your internal ip address.

For this test you can use an external server or just an online service like an nmap port scanner. https://hackertarget.com/nmap-online-port-scanner/


If not a home user/server and telnet out was working or the presumption of port 25 being block is not true.

I will check this:
1. DNS resolve nslookup google.com and save ipv4 and ipv6 addreses
2. check IPv4 and ipv6 configurations
+ Disable ipv6 (This can also make gateway timeouts because not reaching ipv6 host and since smtp have a high timeout, gateway expires before) 3. telnet out to ipv4 address

Hope it helps.

Vidal
  • 2,605
  • 2
  • 16
  • 32
1

In your vm, just run a test php file with following code on your vm

<?php
  echo phpinfo();
?>

If you still get the same error, then it is a result of some server layer before your vm.

But if you see a different output, then you must see the PHP configuration info.

In the configuration, look for "Loaded configuration file", which will give you the exact path of your php.ini configuration file.

In php.ini, check your mail settings (http://php.net/manual/en/mail.configuration.php#ini.smtp), as mentioned by Tuckbros.

Make sure you have sendmail installed on your server and configured the 'sendmail_path' config in php.ini correctly.

Community
  • 1
  • 1
mano1693
  • 46
  • 2
  • 7
0

You could use PHPMailer. It's very easy to use. https://github.com/PHPMailer/PHPMailer
This is the standard code for sending a email: (i copied it)

$mail->SMTPDebug = 2;                                 // Enable verbose debug output
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

//Content
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();

More information:
https://github.com/PHPMailer/PHPMailer/wiki/Tutorial
https://www.youtube.com/watch?v=YtNraQxUTM0 https://www.sitepoint.com/sending-emails-php-phpmailer/

Let me know if this works.
PS. If you want to find out, why your mail isn't in your gmail inbox: Your HTTP headers of your mail() isn't secure. If your headers aren't secure, there could be a chance of header injection.
More information:
https://mediatemple.net/community/products/dv/204643950/understanding-an-email-header
https://www.gracefulsecurity.com/http-header-injection/

JVT038
  • 18
  • 1
  • 10
0

You can follow the steps for using free SMTP server from Google

STEP 1:

Allow access for less secure apps
Less secure app access

STEP 2: Do not use 2 factor authentication to access your Google account.
STEP 3: Set up POP

  1. On your computer, open Gmail. In the top right, click Settings

  2. Settings. Click Settings. Click the Forwarding and POP/IMAP tab. In

  3. the "POP download" section, select Enable POP for all mail or Enable

  4. POP for mail that arrives from now on. At the bottom of the page,

  5. click Save Changes.

STEP 3: Make changes on your email client for Outgoing Mail (SMTP) Server

smtp.gmail.com Requires SSL: Yes Requires TLS: Yes (if available) Requires Authentication: Yes Port for TLS/STARTTLS: 587

*** If you use Gmail with your work or school account, check with your administrator for the correct SMTP configuration.

You can follow the link : Gmail messages on other email clients using POP

naib khan
  • 928
  • 9
  • 16
0

You probably do not have any MTA (Mail Transfer Agent) running. The problem can be solved in several ways. If you want to use the e-mail address from an external server, it is easiest to install and configure some mta, for example very simple msmtp.

Slawomir Dziuba
  • 1,265
  • 1
  • 6
  • 13