Juan Posted July 1, 2022 Share Posted July 1, 2022 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> Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/ Share on other sites More sharing options...
requinix Posted July 1, 2022 Share Posted July 1, 2022 HTML 4? mail() function? Where are you getting this from? Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597795 Share on other sites More sharing options...
Juan Posted July 1, 2022 Author Share Posted July 1, 2022 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> Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597800 Share on other sites More sharing options...
ginerjm Posted July 1, 2022 Share Posted July 1, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597808 Share on other sites More sharing options...
Juan Posted July 1, 2022 Author Share Posted July 1, 2022 May I know how to supply the From address? What email is that? Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597809 Share on other sites More sharing options...
ginerjm Posted July 1, 2022 Share Posted July 1, 2022 You have a domain, correct? You have your own email accounts, no? Setup an account to be used for just this purpose and put that into your email's header. Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597810 Share on other sites More sharing options...
cyberRobot Posted July 1, 2022 Share Posted July 1, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597812 Share on other sites More sharing options...
Juan Posted July 1, 2022 Author Share Posted July 1, 2022 I will ask the domain/hosting administrator regarding with the email...Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597813 Share on other sites More sharing options...
ginerjm Posted July 1, 2022 Share Posted July 1, 2022 (edited) 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 July 1, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597814 Share on other sites More sharing options...
Juan Posted July 1, 2022 Author Share Posted July 1, 2022 May I know where specifically do I put these line? echo "sendmail_from is ".ini_get('sendmail_from')."<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597815 Share on other sites More sharing options...
cyberRobot Posted July 1, 2022 Share Posted July 1, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597816 Share on other sites More sharing options...
ginerjm Posted July 1, 2022 Share Posted July 1, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597817 Share on other sites More sharing options...
Juan Posted July 1, 2022 Author Share Posted July 1, 2022 Guys, i already put the the email needed in the From address, still won't work..... The hosting/domain admin just created the needed email. Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597821 Share on other sites More sharing options...
cyberRobot Posted July 1, 2022 Share Posted July 1, 2022 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597824 Share on other sites More sharing options...
cyberRobot Posted July 1, 2022 Share Posted July 1, 2022 I forgot to mention that you'll need to comment out your redirect when checking the return value from mail(). //header('Location: thank-you'); Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597825 Share on other sites More sharing options...
Juan Posted July 1, 2022 Author Share Posted July 1, 2022 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597826 Share on other sites More sharing options...
Juan Posted July 1, 2022 Author Share Posted July 1, 2022 Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597828 Share on other sites More sharing options...
mac_gyver Posted July 1, 2022 Share Posted July 1, 2022 (edited) 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 July 1, 2022 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597832 Share on other sites More sharing options...
ginerjm Posted July 1, 2022 Share Posted July 1, 2022 Is this domain storgeweddings.com\ actually YOUR domain? Is your script running on this SAME domain name? And - is it spelled correctly??? Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597833 Share on other sites More sharing options...
mac_gyver Posted July 1, 2022 Share Posted July 1, 2022 if you use the email address, that was just created for you at your web hosting, as the To: email address, you can probably get the mail() function to work. Quote Link to comment https://forums.phpfreaks.com/topic/314983-php-mail-not-sending-to-gmail/#findComment-1597835 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.