0

I have this function and after doing some research i'm still not being able to find out why this isn't working I am new to PHP and would like some help. thank you.

sendemail.php

<?php
    header('Content-type: application/json');
    $status = array(
        'type'=>'success',
        'message'=>'Mail Sent. Thank you , we will contact you shortly. '
    );

    $name = @trim(stripslashes($_POST['name'])); 
    $email = @trim(stripslashes($_POST['email'])); 
    $subject = @trim(stripslashes($_POST['subject'])); 
    $message = @trim(stripslashes($_POST['message'])); 

    $email_from = $email;
    $email_to = 'help@addonevents.in';//replace with your email

    $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

    mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
    echo json_encode($status);
    die;

My form

<form name="contact-form" method="POST" action="sendemail.php">
                <label>Name *</label>
               <input type="text" name="name" required="required">
                <label>Email *</label>
                <input type="email" name="email" required="required">
                <label>Phone</label>
                <input type="number">
                 <label>Subject *</label>
                <input type="text" name="subject" required="required">
                 <label>Message *</label>
                 <textarea name="message"  required="required" rows="8"></textarea>
                 <button type="submit" name="submit" required="required">Submit Message</button>
</form> 

can anyone help? I am getting the success message but no mail to my inbox Thank you.

1 Answers1

1

As far as I know, almost nobody uses PHP's mail function to send emails professionally. Even official documentation on that function says:

It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.

I used PHPMailer, which is quite easy to setup and use. I think it is the most popular mailing library in PHP. You can get it here.

The documentation from PHPMailer describes well what is wrong with mail function.

Many PHP developers utilize email in their code. The only PHP function that supports this is the mail() function. However, it does not provide any assistance for making use of popular features such as HTML-based emails and attachments.

Formatting email correctly is surprisingly difficult. There are myriad overlapping RFCs, requiring tight adherence to horribly complicated formatting and encoding rules - 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.

Here is a small (official) example on how to use PHPMailer:

<?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', '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');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$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';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

So don't reinvent the wheel, and use proven and reliable library to do it.

Yerke
  • 2,071
  • 2
  • 25
  • 23