Jump to content

Recommended Posts

Hello everyone!
Can someone help me regarding with php mail function. The code submits perfectly, but never sends an email. 
Here is my code:

 

<?php 
$errors = '';
$myemail = 'juandelacruz@gmail.com';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
    empty($_POST['email']) || 
    empty($_POST['phone']) ||
    empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$phone = $_POST['phone'];
$message = $_POST['message'];

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Message from: $name";
    $email_body = "Message from website contact form. ".
    " Here are the details:\n \n     NAME: $name \n 
    EMAIL ADD: $email_address \n 
    PHONE: $phone \n
    MESSAGE: $message";  
    
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html\r\n";
    $headers = "From: $email_address\n";
    //'Reply-To: reply@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: thank-you');

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/
Share on other sites

Just from the internet.

here is the form code:

<form method="post" name="contactform" id="contact-form" action="contact-form-handler">
          <div class="error-container"></div>
          <div class="row">
            <div class="col-md-4">
              <div class="form-group">
                <input name="name" id="name" type="text" class="form-control required input_field" placeholder="Your Name" />
              </div>
            </div>
            <div class="col-md-4">
              <div class="form-group">
                <input name="email" id="email" type="text" class="form-control validate-email required input_field" placeholder="Email Address" />
              </div>
            </div>
            <div class="col-md-4">
              <div class="form-group">
                <input name="phone" id="phone" type="text" class="form-control validate-phone required input_field" placeholder="Phone Number" />
              </div>
            </div>
          </div>
          <div class="form-group">
            <label>Message</label>
            <textarea name="message" id="message" class="form-control required" rows="10" placeholder="Your Message"></textarea>
          </div>
          <div class="text-right"><br>
            <button class="btn btn-primary solid blank" name="submit" type="submit">Send Message</button>
          </div>
        </form>

 

I believe your problem is the From address.  PHP mail function must have a from address that is on your server and it looks like you are using the client's email address as if that user was sending you an email when in fact your web app on your domain is doing the sending.

As ginerjm mentioned, you'll want to avoid letting the user supply the From address. Partly because your mail server isn't going to be authorized to send mail on behalf of email services like Gmail, Yahoo, etc. If you're interested in learning more, you could look up Email Spoofing.

Side note: PHP has a built-in validator for email addresses. See the first example here:
https://www.php.net/manual/en/filter.examples.validation.php

If you have a company setting then there is probably already an account for this setup.  Can't be the first time a web app has had to send mail for your company.

Try adding this line to your script or just execute in its own script.  MIght solve your riddle

echo "sendmail_from is ".ini_get('sendmail_from')."<br>";
 

Edited by ginerjm
29 minutes ago, Juan said:

May I know where specifically do I put these line?
echo "sendmail_from is ".ini_get('sendmail_from')."<br>";

You should be able to temporarily add it anywhere in your PHP code. Note that you're only adding it for "debugging" purposes. It would be removed once you know what the value contains. More information about the function can be found here:
https://www.php.net/manual/en/function.ini-get.php

Actually, if you are working for people who are administering all of this, you should be able to get the proper setup for sending email from them and not have to rebuild the wheel all by yourself.

And if you have to ask us how to execute one single line of php code, perhaps you are in the wrong job.

Note that the mail() function returns true/false to indicate whether the message was accepted for delivery. If you haven't already, you'll want to check what the function is returning. It should be noted, however, that a true value doesn't mean the message was actually delivered. More information about mail() can be found here:
https://www.php.net/manual/en/function.mail.php

 

Also, it may help if enable all PHP errors/warnings to be shown. This can be accomplished by adding the code below to your PHP. Be sure to remove the code when you are done with the debugging process.

<?php
//REPORT ALL PHP ERRORS
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

 

I'm so sorry guys! 
I'm having a very hard time on this.

I saw another code from the internet, tried it out. It's also not sending but it has an error message

array(5) { ["name"]=> string(6) "qwerty" ["email"]=> string(16) "fordix@gmail.com" ["phone"]=> string(7) "1234567" ["message"]=> string(10) "sadfsdasdf" ["submit"]=> string(0) "" }


My new code looks like these:
 

<?php
var_dump($_POST);
// Check for empty fields
if(empty($_POST['name'])  || 
    empty($_POST['phone'])  ||
    empty($_POST['email']))
{
    $errors .= "\n Error: all fields are required";
}

$name = strip_tags(htmlspecialchars($_POST['name']));
$email = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));

// Create the email and send the message
$to = 'juan@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$subject = "Website Contact Form:  $name";
$text = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email\n\nPhone: $phone\n\nMessage:\n$message";
$header = "From: info@storgeweddings.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
mail($to,$subject,$text,$header);
header('Location: thank-you');

?>

gmail has recently stopped accepting emails that are being sent by unknown, unauthenticated email clients/systems, such as a sending mail server at some random web hosting. you must generate an OAuth token and use it to authenticate when sending to gmail. see the following post and thread it is in -

prior to this, you were required to use smtp authentication against your gmail mailbox, which the php mail() function does not support, so you need to use either the phpmailer or swiftmailer class in any case.

 

Edited by mac_gyver
  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.