Error PHP mail function doesn t complete sending of e-mail

0 votes

I've tried creating a simple mail form. The form itself is on my index.html page, but it submits to a separate "thank you for your submission" page, thankyou.php, where the above PHP code is embedded. The code submits perfectly, but never sends an email. How can I fix this?

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: yoursite.com';
    $to = 'contact@yoursite.com';
    $subject = 'Customer Inquiry';
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
        if (mail ($to, $subject, $body, $from)) {
            echo '<p>Your message has been sent!</p>';
        } else {
            echo '<p>Something went wrong, go back and try again!</p>';
        }
    }
?>
Jul 30, 2020 in Laravel by kartik
• 37,520 points
2,366 views

1 answer to this question.

0 votes

Hello @kartik,

If you are using an SMTP configuration for sending your email, try using PHPMailer instead. 

I created my email sending this way:

function send_mail($email, $recipient_name, $message='')
{
    require("phpmailer/class.phpmailer.php");

    $mail = new PHPMailer();

    $mail->CharSet = "utf-8";
    $mail->IsSMTP();                                      // Set mailer to use SMTP
    $mail->Host = "mail.example.com";  // Specify main and backup server
    $mail->SMTPAuth = true;     // Turn on SMTP authentication
    $mail->Username = "myusername";  // SMTP username
    $mail->Password = "p@ssw0rd"; // SMTP password

    $mail->From = "me@walalang.com";
    $mail->FromName = "System-Ad";
    $mail->AddAddress($email, $recipient_name);

    $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
    $mail->IsHTML(true);                                  // Set email format to HTML (true) or plain text (false)

    $mail->Subject = "This is a Sampleenter code here Email";
    $mail->Body    = $message;
    $mail->AltBody = "This is the body in plain text for non-HTML mail clients";
    $mail->AddEmbeddedImage('images/logo.png', 'logo', 'logo.png');
    $mail->addAttachment('files/file.xlsx');

    if(!$mail->Send())
    {
       echo "Message could not be sent. <p>";
       echo "Mailer Error: " . $mail->ErrorInfo;
       exit;
    }

    echo "Message has been sent";
}

Hope this is helpfull!!
Thank you!!

answered Jul 30, 2020 by Niroj
• 82,840 points

Related Questions In Laravel

0 votes
1 answer

How to configure Laravel mail.php to use built-in mail function?

Hello, To do the same as mail() PHP ...READ MORE

answered Dec 3, 2020 in Laravel by Niroj
• 82,840 points
3,223 views
0 votes
1 answer

Error:Composer Out of memory in Laravel?

Hey @kartik, This Error  happens in almost all ...READ MORE

answered Mar 30, 2020 in Laravel by Niroj
• 82,840 points
9,692 views
0 votes
1 answer

Error:Laravel PHP Command Not Found

Hello @kartik, Try the follwing snippet: nano ~/.bash_profile And ...READ MORE

answered Jul 30, 2020 in Laravel by Niroj
• 82,840 points
7,128 views
0 votes
1 answer

How can I echo the version of the current Laravel version in php using the view?

Hello @kartik, This is the way how to ...READ MORE

answered Aug 10, 2020 in Laravel by Niroj
• 82,840 points
1,199 views
0 votes
1 answer

How to download and install Lavavel framework?

Hey @kartik, First you must have xampp install ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,840 points
1,443 views
0 votes
1 answer

Display Laravel in browser by using cmd promt?

Hello, First you need to have laravel install ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,840 points
1,221 views
0 votes
1 answer

How can we get started with Laravel through Xampp?

Hii, First you need to start Apache and ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,840 points
1,017 views
0 votes
1 answer

How to change Laravel official name to any customize name?

Hey, You just need to go Laravel folder through ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,840 points
3,056 views
0 votes
1 answer

Error:sending email via php mail function goes to spam

Hello @kartik, Try changing your headers to this: $headers ...READ MORE

answered Jul 30, 2020 in Laravel by Niroj
• 82,840 points
11,085 views
0 votes
1 answer

How can I get the error message for the mail() function?

Hello @kartik, You can use error_get_last() when mai l()returns false. $success = mail('example@example.com', ...READ MORE

answered Jul 30, 2020 in Laravel by Niroj
• 82,840 points
9,587 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP