PHP Mail Script not sending mail - php

I am working on a simple contact form and the php script is not sending an email to my email... The script is running succesfully and it produces the HTML output seen in the code below. I am trying to figure it out, yet I just cannot get it to send the email. I have looked at tutorials and appear to have the same exact code as a lot of the tutorials out there. I am using a form to get user's input seen here,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dummy Code - Contact</title>
</head>
<body>
<form action="send_simpleform.php" method="post">
<p>Your name<br />
<input name="sender_name" type="text" size="30" /></p>
<p>Email<br />
<input name="sender_email" type="text" size="30" /></p>
<p>Message<br />
<textarea name="message" cols="30" rows="5"></textarea></p>
<input name="submit" type="submit" value="Submit" />
</form>
</body>
</html>
My PHP script can be seen here as well,
<?php
$msg = "Email sent from www.dummycode.com\n";
$msg .= "Sender's Name:\t $_POST[sender_name]\n";
$msg .= "Sender's E-mail:\t $_POST[sender_email]\n";
$msg .= "Sender's Message:\t $_POST[message]\n";
$to = "henry.harris#xxxxxxxxxxx.com";
$subject = "DummyCode.com";
$headers = "From: My web site <www.dummycode.com>\n";
$headers .= "Reply to: $_POST[sender_email]\n";
mail($to, $subject, $msg, $headers);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dummy Code Contact Sent</title>
</head>
<body>
<h1>The following email has been sent</h1>
<p>Your Name:<br />
<? echo "$_POST[sender_name]"; ?>
<p>Your Email Adress:<br />
<? echo "$_POST[sender_email]"; ?>
<p>Message:<br />
<? echo "$_POST[message]"; ?>
</p>
</body>
</html>
I know I am pretty knew at this and if this is a stupid error on my part, then I'm sorry. I am pretty dumb. Thanks in advance. Please don't hate.
-Henry

The only thing you can do in your script, is check the return value of the mail() function.
If true, the message was successfully accepted for delivery. However, that does not mean that the message was actually sent.
Simple example:
if (mail(...))
{
// mail was accepted for delivery
}
else
{
// not accepted
}

You should take a look at swiftmailer. Setup is fast an simple and it's VERY well documented. Error handling is top notch!

Well, I have had situations where mails seemed sent but I can't see it in the inbox I'm sending it to. So here are a few things I do:
Don't send from localhost except you have a mail server that can send outbound messages
Preferably, send or test your mail script on your remote server
Ensure the senders email address is same as the originating domain
Don't use reply emails that are different from sender's email - it could be eaten up as a spam or junked by most email clients
Verify the allowed smtp port of your server and set that
Verify the smtp server and set that
Provide actual credentials for the sending email
Place the user's email somewhere in the body of the message with a mailto: link so you could quickly reply them. (If they are sending to you using the script). If you are sending to them, no need specifying a different reply-to
Ensure to specify the encoding charset, newline character and possibly word-wrap features that should word
Try using a tested PHP Class from phpclasses.org
I take them one at a time and usually get my mails sent doing what's right. In your code, try adjusting these lines and testing too:
$headers = "From: My web site <www.dummycode.com>\n"; //Same as originating domain or email (just for tweaks)
$headers .= "Reply to: $_POST[sender_email]\n"; //Should be same as sender's email for a try

Related

Contact form experiencing 30-second delay on submittal

I'm updating a Contact Us form on a website. It seems like straightforward HTML and PHP, but I'm experiencing a 30-second delay from when I click the Submit button to when the mail() call finishes and the code displays the confirmation message on the page. The email sends successfully, and I get no error in the error log. The current contact form on our site does not experience this delay.
I tried testing some super-simple code and I'm having the same issue. I've been testing the files from a /test/2023contactform folder off the root folder, but I tried testing from the root folder, too, and have experienced the same delay.
Here's my super simple test code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test email form</title>
</head>
<body>
<form name="contact" method="post" id="contact" action="test_email.php">
<button type="submit">Send Message</button>
</form>
</body>
</html>
<?php
$to = "myemail#mydomain.com";
$subject = "My Subject";
$txt = "Hello world!";
$headers = "From: myemail#mydomain.com" . "\r\n" .
"CC: myemail#mydomain.com";
mail($to,$subject,$txt,$headers);
echo 'Thank You! Your message has been sent. We will contact you shortly.';
?>
If I comment out the mail() line, the echo message displays immediately upon clicking the submit button.
If I remove the parameters from the mail() line, the echo message displays immediately and I get an error log entry saying "mail() expects at least 3 parameters."
I parsed through the PHP for the site's current form and I don't see any special coding that I think would modify the response time.
My hosting service said that since the email went through and no errors were generated it isn't a problem with my code communicating with their server.
I'm at as loss as to what to try next. Any suggestions?

How can I set up a contact form on my website? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I've done my research on this but can't seem to get it to work.
I'm looking to add a contact form to my website that sends an email directly to me. I've watched videos and used code I've found online but nothing works. I even temporarily disabled my website and uploaded just a blank contact form (code below) and a php file (code below) with my only results being that the echo command at the end of the PHP file DOES show up. The email, though, still does not send.
What am I missing? Thank you!
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Temp</title>
</head>
<body>
<form method="post" action="send.php" name="contact_form">
<p>
<input name="name" type="text" />
</p>
<p>
<input name="email" type="text" />
</p>
<p>
<textarea name="message"></textarea>
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit" />
</p>
</form>
</body>
</html>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "email#myemail.co";
$subject = "Contact Form Submission";
mail ($to, $subject, $message, "From: " . $name);
echo "Your message has been sent. You can expect to hear from us soon.";
?>
I'm sure this is a duplicate, but I just wanted to mention a couple of things to check.
Does your mail() call return true? I would check that. It's possible that your PHP installation is not set up correctly to send mail. There are countless posts on how to check and configure that, which I would suggest reviewing if you haven't already (here's one, for example).
Depending on where you're hosting this, your host's configurations may restrict any outgoing mail that is not from the domain you're hosting. (I've had this problem myself on shared hosts.) Here you're setting the "from" header as the name of the person submitting the form (which would look something like: "From: John Doe"). This may be a problem either on the sending or receiving end of the email (either side rejecting it because it doesn't come from an email address, or a valid email address, etc). Try setting the "from" value to an email address valid on your host (e.g., "myname#mydomain.com"). Then, just include the person's name and email address in the $message of the email.
Hope that helps.

php mail function not sending email when From address is yahoo

Not sure if anyone else has experienced this but i have a simple form that sends out a email.
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="phone" id="phone" value="<?php echo $phone; ?>" />
<textarea name="message" rows="20" cols="20" id="message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
When submitted i have the following:
if ($_POST) {
$email_to = "myemail#yahoo.com";
$subject = "Contact Form";
$message = "Phone: {$phone}\r\nMessage: {$msg}";
$headers = "From: sendingemail#yahoo.com" . "\r\n";
mail($email_to,$subject,$message, $headers);
}
When the form is submitted the mail function returns true but no email gets sent however when i change the FROM email to anything else outside of yahoo such as something#gmail.com the email comes through. Anyone know how to solve this issue?
Yahoo marked your mail as spam and is probably just ignoring it.
This is probably true for hotmail as well.
Best thing todo is find yourself a good SMTP mail pluging/module (phpMailer for instance) and use the credentials of a legit mail account. This way you are sending mail trough a dedicated mail server and changes are you won't be marked as spam anymore.
Do notice however than when you sent loads of (simular) mails or your script gets hacked and is used for spamming, changes are that your legit mailserver becomes blacklisted or (if you are lucky) blocks your account as beeing unsafe.

PHP mail from an HTML form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I'm trying to setup a contact form on a website I'm working on, the code for which looks like this:
<form action="contact.php" method="POST">
<ul>
<li>
<label for="name">Name:</label><br>
<input type="text" name="name" />
</li>
<br>
<li>
<label for="email">E-mail:</label><br>
<input type="email" name="email" />
</li>
<br>
<li>
<div id="label">Enquiry:</div>
<select name="interest"><br>
<option value="Wedding">Wedding</option>
<option value="Engagement/Couple">Engagement or Couple</option>
<option value="Portrait">Portrait</option>
<option value="Family">Family</option>
<option value="Children">Children</option>
</select>
</li>
<br>
<li>
<label for="message">Message:</label><br>
<textarea name="message" cols="40" rows="6"></textarea>
</li>
<br>
<li>
<button class="submit" type="submit" value="Submit">Submit Form</button>
</li>
</ul>
</form>
Originally I just had the form action as a mailto:myemail#example.com but I would obviously prefer for the user to just submit the form and the email be sent instead of just opening their email application. I'm trying to build a PHP script to do this but am having no luck as yet, probably because this is my first time ever even looking at PHP, let alone trying to write it.
I have looked at many different examples and delved into the PHP manual for help but whatever I write leads to the same result, pressing the submit button on my form and receiving nothing in my inbox. So I started a sort of fault finding process to try and help determine the problem. I would start with really basic PHP scripts and then try and add more to each one and see where it broke.
I started by changing the form action to the php test script as below:
<?php
phpinfo() ;
?>
This worked fine and displayed the php info page as it should. Great so I know that my host supports php and has it installed. This is not the problem then. Second I changed the form action to this code below:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
And again it worked as expected showing the Hello World paragraph. After this I attempted the most basic php mail script I could think of which looked like this:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
mail('myemail#example.com', 'Contact Form', 'This is a test');
?>
</body>
</html>
All this succeeds in doing when I press the submit button on my form is displaying a blank page with the url of my php script in the address bar of my browser and from this I have received no email. Not in my inbox or junk folder, nowhere. As this is such a simple script I have two trains of thought at the min. 1 is that I'm just doing something very basic wrong, which is a high possibility considering my extremely limited knowledge of php. 2 is that something more complicated is going on, maybe php settings of my host are not correct for what I'm attempting to do? I don't know.
I apologise for this question being so long winded, I just wanted to show what I'd done so far in the hope that it will help you guys with any assistance you can give me. Any help with my problem is greatly appreciated, thank you.
One thing to consider is that your email may be getting blocked by your email provider. I have a host with Heart Internet and I've tried sending an email from the website being hosted by Heart to my AoL account and the email does not show up at all. But any emails going to Gmail do. This is because AoL has blocked all emails coming from Heart because of websites producing spam.
I will input some PHP code below which should work:
// Input your desired location instead of email address
$to = 'email address';
$subject = 'Test email';
$message = '<html><body>';
$message .= '<h1>This is an email.</h1>';
$message .= '<p>I hope you like it.</p>';
$message .= '</body></html>';
$headers = 'MIME-Version: 1.0\r\n';
$headers .= 'Content-Type: text/html; charset=ISO-8859-1\r\n';
mail($to,$subject,$message,$headers);
Or you are running from localhost.
Or your SMTP settings in your php.ini file aren't configured correctly
My host has blocked mail server for the package I'm on. Something I should've checked but in my frustration I've overlooked it. Thanks for not answering my stupid question with stupid answers guys. Sorry to have made you read all that for nothing really but a fresh head and pair of eyes always works wonders!!
check at the top of the form, there is written <form action="contact.php" method="POST"> that's mean form values are posted to contact.php file where the mail functions is written to send email with form's data. Please show the code of contact.php

I finally was able to use cron... but I'm not sure if it will work with my code. How can i change it?

My webhost supports cronjobs. I am very new so I hav almost no idea what I'm doing. I scheduled cron to run a script that sends an email. But I don't know what to do! Here's my cron: 0 0 28 * * php -f /home/a7269592/contact.php Now how could I adjust my code so when It's the 28th, that code will send out an email. Here's the PHP:
<?php
if(isset($_POST['email']))
{
$headers = "From: Memory Jet <your_company#example.com>\r\n";
$to_visitor = $_POST["email"];
$common_data = $_POST["message"];
mail($to_visitor, "Your Memory", $common_data, $headers);
} ?>
Here's the html form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<html> <body><form method="post" action="contact.php">
Email: <input name="email" type="text"><br> name:<br>
<textarea name="name" rows="15" cols="40"></textarea><br>
Message:<br> <textarea name="message" rows="15" cols="40"></textarea><br>
<input type="submit"> </form> <body> <html>
</body>
So how would I change the php so that cron would schedule it?? Thanks in advance! -Ben
Not so far, i was in exactly the same situation. To my mind, you're trying to deligate mailing from exactly "at posting moment" to scheduled timetable. So that, first that you need to know is:
WHEN YOU'RE RUNNING CRON YOU HAVE NO ACCESS TO $_SERVER, $_POST, and other global variables
That's because you run PHP, NOT through the server, which is the AUTHOR of the global variables.
So, you need to create temporary storage for your mail, such as an mail task file (might be problem with at-execute time appending tasks, for example if you cron mailer sends mails, and at the same moment your frontend PHP script wants to add some tasks, so that the task file might me corrupted) or DB, or whatever your imagination can do.
After that, you need to set up your cron task, which would take for example first 500 mail tasks and mails it. Sure, you can mail all mail tasks at this moment, not a problem.
Hope, it helps.
I see what you are trying to do, but are you planning for it only to send an email if it the 28th or store it and send it on the 28th? If you are using the first option you should change it so it says this.
<?php
$today = getdate()
if(isset($_POST['email']) && $today['mday'] == 28)
{
$headers = "From: Memory Jet <your_company#example.com>\r\n";
$to_visitor = $_POST["email"];
$common_data = $_POST["message"];
mail($to_visitor, "Your Memory", $common_data, $headers);
} ?>
As for the storing, the comments are right, you should re-architect your code. The crontab will run the code without gettimg data from the form - web based and CLI requests are handled diffirently. I would definetely add another file to handle the form request and right it to a form or database. The one run by the crontab would read from that file or database.

Categories