html - PHP mail form doesn't complete sending e-mail -
html - PHP mail form doesn't complete sending e-mail -
<?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 , seek again!</p>'; } } ?>
i've tried creating simple mail service form. form on index.html page, submits separate give thanks submission page thankyou.php above php code embedded. code submits perfectly, never sends email.. please help...
there variety of reasons script appears not sending emails. it's hard diagnose these things unless there obvious syntax error. without 1 need run through checklist below find potential pitfalls may encountering.
make sure error reporting enabled , set study errorserror reporting essential rooting out bugs in code , general errors php encounters. error reporting needs enabled receive these errors. placing next code @ top of php files (or in master configuration file) enable error reporting.
error_reporting(-1); ini_set('display_errors', 'on'); set_error_handler("var_dump");
see this stack overflow answer more details on this.
make sure phone callmail()
function it may seem silly mutual error forget place mail()
function in code. create sure there , not commented out.
your web server should logging attempts send emails through it. location of these logs vary (you may need inquire server administrator located) can commonly found in user's root directory under logs
. within error messages server reported, if any, related attempts send emails.
when error suppression operator @
prepended look in php, error messages might generated look ignored. there circumstances using operator necessary sending mail service not 1 of them.
if code contains @mail(...)
may hiding of import error messages help debug this. remove @
, see if errors reported.
it's advisable when check error_get_last()
right afterwards concrete failures.
the mail()
function:
returns true
if mail service accepted delivery, false
otherwise. of import note because mail service accepted delivery, not mean mail service reach intended destination.
this of import note because:
if receivefalse
homecoming value know error lies server accepting mail. isn't coding issue server configuration issue. need speak scheme administrator find out why happening. if receive true
homecoming value not mean email sent. means email sent respective handler on server php. there still more points of failure outside of php's command can cause email not sent. so false
help point in right direction whereas true
not mean email sent successfully. of import note!
oftentimes, various reasons, emails sent through php (and other server-side programming languages) end in recipient's spam folder. check there before troubleshooting code.
to avoid mail service sent through php beingness sent recipient's spam folder, there various things can do, both in php code , otherwise, minimize chances emails marked spam. tips michiel de mare include:
use email authentication methods, such spf, , dkim prove emails , domain name belong together, , prevent spoofing of domain name. spf website includes wizard generate dns info site. check reverse dns create sure ip address of mail service server points domain name utilize sending mail. make sure ip-address you're using not on blacklist make sure reply-to address valid, existing address. use full, real name of addressee in field, not email-address (e.g."john smith" <john@blacksmiths-international.com>
). monitor abuse accounts, such abuse@yourdomain.com , postmaster@yourdomain.com. means - create sure these accounts exist, read what's sent them, , deed on complaints. finally, create really easy unsubscribe. otherwise, users unsubscribe pressing spam button, , impact reputation. see how create sure email send programmatically not automatically marked spam? more on topic.
make sure supply mail service headerssome spam software reject mail service if missing mutual headers such "from" , "reply-to":
$headers = array("from: from@example.com", "reply-to: replyto@example.com", "x-mailer: php/" . php_version ); $headers = implode("\r\n", $headers); mail($to, $subject, $message, $headers);
make sure mail service headers not have syntax error invalid headers bad having no headers. 1 wrong character takes derail email. double-check create sure syntax right php not grab these errors you.
$headers = array("from from@example.com", // missing colon "reply to: replyto@example.com", // missing hyphen "x-mailer: "php"/" . php_version // bad quotes );
make sure recipient value correct sometimes problem simple having wrong value recipient of email. can due using wrong variable.
$to = 'user@example.com'; // other variables .... mail($recipient, $subject, $message, $headers); // $recipient should $to
another way test hard code recipient value mail()
function call:
mail('user@example.com', $subject, $message, $headers);
this can apply of mail()
parameters.
to help rule out email business relationship issues, send email multiple email accounts at different email providers. if emails not arriving @ user's gmail account, send same emails yahoo account, hotmail account, , regular pop3 business relationship (like isp-provided email account).
if emails arrive @ or of other email accounts, know code sending emails email business relationship provider blocking them reason. if email not arrive @ email account, problem more related code.
make sure web host supports sending emailsome web hosting providers not allow or enable sending of emails through servers. reasons may vary if have disabled sending of mail service need utilize alternative method uses 3rd party send emails you.
an email technical back upwards (after trip online back upwards or faq) should clarify if email capabilities available on server.
if onlocalhost
, create sure have mail service server set up if developing on local workstation using wamp, mamp, or xampp, email server not installed on workstation. without one, php cannot send mail service default.
you can overcome installing basic mail service server. windows can utilize free mercury mail.
you can utilize smtp send emails. see this great answer vikas dwivedi larn how this.
enable phps custommail.log
in add-on mtas , phps log file, can enable logging mail()
function specifically. doesn't record finish smtp interaction, @ to the lowest degree function phone call parameters , invocation script.
ini_set("mail.log", "/tmp/mail.log"); ini_set("mail.add_x_header", true);
see http://php.net/manual/en/mail.configuration.php details. (it's best enable these options in php.ini
or .user.ini
or .htaccess
perhaps.)
php's built in mail()
function handy , gets job done. there alternatives offer more powerfulness , flexibility including handling lot of issues outlined above. 1 might consider using popular phpmailer or swiftmailer, or older pear::mail.
php html email
Comments
Post a Comment