-1

I am sending a confirmation email using the mail function built in to PHP. However, it is only working locally on my server which has been assigned my university. The site is http://adonnelly759.students.cs.qub.ac.uk/thatch/.

When a contact form is sent it should send an email to myself under an admin email address and a confirmation of the email to the clients email address which they provided.

I have set up the code like follows:

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <Thatch>' . "\r\n";

// Sending the email to the users provided e-mail
mail($email, $userSub, $userMsg, $headers);

Update: Decided to ditch the mail() function and go for SwiftMailer. Simple and easy to use. Thanks for all the help.

Aidan Donnelly
  • 95
  • 1
  • 10

1 Answers1

1

The PHP mail() function isn't the best way to go. Go to mail-tester.com and copy the random email address that is shown, now send an email to this address using your code, return to mail-tester.com and see the results. I suspect you'll have a very slow score and this is why your email isn't being delivered.

Instead you can use PHPMailer. Below is some sample code. https://github.com/PHPMailer/PHPMailer

This should work but you can also enable SMTPAuth along with a username and password and then you'll be sending the emails out in a robust way that will give you a much better chance of them arriving at their intended destination.

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$body = '<p>this is a <strong>test</strong> email</p><p><img src="cid:qrcode" /></p>';

$mail = new PHPMailer(true);

try {
    $mail->isSMTP();
    $mail->Host = 'localhost';
    $mail->SMTPAuth = false;

    $mail->setFrom('chris@me.com', 'Chris');
    $mail->addAddress('me@gmail.com');

    $mail->isHTML(true);
    $mail->Subject = 'This is a PHPMailer Test';
    $mail->Body    = $body;
    $mail->AltBody = $body;

    $mail->send();

    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}
mario
  • 138,064
  • 18
  • 223
  • 277
Chris
  • 4,186
  • 11
  • 44
  • 79