1

I have created a form that allows people to send an email from a website. The HTML code (see attached) calls a PHP script (see attached), and the email is supposed to be sent. The webpage displays the message "Email successfully sent", but I never actually received the email (it's not in spam either).

I have reached out to my hosting service (awaiting reply) to check whether PHP is supported or not. In the meantime, I would like to ensure that my code has no errors.

HTML:

<form action="message.php" method="post">
    <fieldset>
        <p>Name <span class="requiredAsterisk">*</span></p>
        <input name="name"/>
        <p>Email <span class="requiredAsterisk">*</span></p>
        <input name="email"/>
        <p>Message <span class="requiredAsterisk">*</span></p>
        <textarea name="message"></textarea>
    </fieldset>
    <fieldset>
        <input class="sendMessage w3-large" type="submit" value="Send Message"/>
    </fieldset>
  </form>

PHP:

<?php
$header = 'From: ' .$_POST['name'] ."\r\n" .'Reply-to: ' .$_POST['email'] ."\r\n" .'X-Mailer: PHP/' .phpversion();
if (mail("email@mail.com", "Email from website", $_POST['message'], $header)) {
    echo ("<p>Email successfully sent</p>");
} else {
    echo ("<p>Email failed</p>");
}
?>

Thanks in advance

  • 1
    When `mail` returns true, it just means that the email was accepted by the mail server. It doesn't mean that the email could be sent, was sent, or was delivered. – fubar Jun 08 '17 at 02:17
  • @fubar Does that mean this is a server-side issue? – Forrest Fan Jun 08 '17 at 02:28
  • 1
    Yes, I would say it's a server side issue. But that could be a configuration issue, blacklisting issue, anything. Hopefully your hosting company can provide some insight. – fubar Jun 08 '17 at 02:34
  • People should really stop upvoting duplicated questions... – Capsule Jun 08 '17 at 04:10

2 Answers2

1

A good alternative to the original mail() function in PHP would be something like PHPMailer. As stated in the github page:

the vast majority of code that you'll find online that uses the mail() function directly is just plain wrong! Please don't be tempted to do it yourself - if you don't use PHPMailer, there are many other excellent libraries that you should look at before rolling your own - try SwiftMailer, Zend_Mail, eZcomponents etc.

PHPMailer is super easy to set up and get going. This is the basic syntax to send an email:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // 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

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net');     // Add a recipient

$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';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
Brian Moreno
  • 977
  • 4
  • 11
  • 39
  • lol at "This is the basic syntax to send an email" :-) It's also completely useless if you don't have an SMTP at hand, as it will use the internal `mail()` function. – Capsule Jun 08 '17 at 03:34
  • Sure, but he mentioned he had a hosting service so he most likely does have an SMTP at hand. Even if he didn't have a hosting service he could use countless other services that provide an SMTP services like Yahoo or Gmail. @Capsule – Brian Moreno Jun 08 '17 at 03:49
  • Yahoo or Gmail would block you as soon as the form is abused because of the traffic it would generate and shared hostings generally don't offer auth-SMTP. I'm not saying it's not a valid answer but I'm mitigating the enthusiasm :-) – Capsule Jun 08 '17 at 03:52
  • True, they do have their down sides, but I'm just trying to help him out with an alternative in case he can't get it working with the `mail()` function. – Brian Moreno Jun 08 '17 at 03:56
  • Oh absolutely, but actually translating the OP's code into a `PHPMailer` syntax would look a bit less terrifying. For example, there's no multiple recipients, CC, BCC, attachment or alternate body as the message is dynamic. That's why I laughed at "This is the basic syntax to send an email". It's definitely not basic and might add a lot of confusion. – Capsule Jun 08 '17 at 04:09
0

(I would probably comment this, but my rep is too low) You should use $message = wordwrap($_POST['message'], 70, "\r\n"); so your message follows the documentation on mail(). Besides this, your code seems to be fine. As you mentioned, it is probably related to your hosting provider, as it is common place for them to block the mail function. Also see this question on it for some extras which may the cause of your problem.