0

I'm trying to write a code to send emails from a contact form, I'm doing tests on a ubuntu server and here is the php config on this server:

enter image description here

And here is my php code (I've tested this same code in other online production server and works perfectly):

<?php 
$ToEmail = 'dukecapitan@gmail.com'; 
$EmailSubject = 'Site contact form'; 
$mailheader = "From: ".$_POST["email"]."\r\n"; 
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
$MESSAGE_BODY = "Name: ".$_POST["name"].""; 
$MESSAGE_BODY .= "Email: ".$_POST["email"].""; 
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"]).""; 
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); ?>

This code isn't working on my Ubuntu server, what can I do? I don't want to use libraries. I read that in Linux server there is no need to install anything and no need to add a SMTP configuration on the code, even in the php manual give some basics examples to send emails and those don't have any SMTP code. What is the problem with this code?

halfer
  • 19,824
  • 17
  • 99
  • 186
Capitan Duke
  • 135
  • 1
  • 2
  • 16
  • Are you receiving an error code or is it simply not sending? –  May 18 '18 at 09:24
  • @MikeRodham when I execute the code it's said Failure and the email doesnt arrieve. – Capitan Duke May 18 '18 at 09:26
  • Any particular reason why you're against libraries? Because [PHPMailer](https://github.com/PHPMailer/PHPMailer) would make your life so much easier. Since the only error the mail() function actually returns is true if send false if not. Also, just because mail() can return true DOES NOT mean it will reach it's intended destination. –  May 18 '18 at 09:30
  • The only other thing I can think of why this mail wouldn't send is if the server is local and SMTP hasn't been properly configured. –  May 18 '18 at 09:33
  • @MikeRodham because I'm new with php and will get complicated with libraries, I'm sure with a simple code will work and my code works in other server and I dontknow why in this server doesnt work. But if this ain't gonna work I will install the PHPMailer. :) – Capitan Duke May 18 '18 at 09:33
  • @MikeRodham the server is not localhost and the SMTP is configured with my own email server. – Capitan Duke May 18 '18 at 09:36
  • PHPMailer isn't a complicated library, I know libraries can seem quite daunting to a new PHPer but they're actually a lot easier than you think. A little bit of trial and error and you'll be smiling in no time. Also, are you sure that your server is successfully speaking to your SMTP server? –  May 18 '18 at 09:36
  • @MikeRodham with the PHPmailer I will keep the same code or I need to write a new code to adjust to the phpmailer library? – Capitan Duke May 18 '18 at 09:49
  • You'll need to adjust your code to the PHPMailer library which they give a very helpful example for you [on their GitHub](https://github.com/PHPMailer/PHPMailer) –  May 18 '18 at 09:50
  • @MikeRodham Thanks Mike I will try it!!!! – Capitan Duke May 18 '18 at 09:51

1 Answers1

1

This isn't the answer you wanted but probably the best way to tackle given our discussion on the main question.

A PHPMailer Example

You'll just need to adapt your current code to your variables listed in your original code.

Source: PHPMailer Example - Official Package GitHub

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $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();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}