My PHP PHP code is not working

My email code does not work.

<?php if(isset($_POST['send'])){ $to = " info@erbimages.com " ; // change all the following to $_POST $from = $_REQUEST['Email'] ; $name = $_REQUEST['Name'] ; $headers = "From: $from"; $subject = "Web Contact Data"; $fields = array(); $fields{"Name"} = "Name"; $fields{"Email"} = "Email"; $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } $subject2 = "Thank you for contacting us."; $autoreply = "<html><body><p>Dear " . $name . ",</p><p>Thank you for registering with ERB Images.</p> <p>To make sure that you continue to receive our email communications, we suggest that you add info@erbimages.com to your address book or Safe Senders list. </p> <p>In Microsoft Outlook, for example, you can add us to your address book by right clicking our address in the 'From' area above and selecting 'Add to Outlook Contacts' in the list that appears.</p> <p>We look forward to you visiting the site, and can assure you that your privacy will continue to be respected at all times.</p><p>Yours sincerely.</p><p>Edward R Benson</p><p>Edward Benson Esq.<br />Founder<br />ERB Images</p><p>www.erbimages.com</p></body></html>"; $headers2 = 'MIME-Version: 1.0' . "\r\n"; $headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers2 .= 'From: noreply@erbimages.com ' . "\r\n"; mail($from, $subject2, $autoreply, $headers2); $send=false; if($name == '') {$error= "You did not enter your name, please try again.";} else { if(!preg_match("/^[[:alnum:]][a-z0-9_.'+-]*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$/",$from)) {$error= "You did not enter a valid email address, please try again.";} else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); } if(!isset($error) && !$send) $error= "We have encountered an error sending your mail, please notify service@erbimages.com "; } }// end of if(isset($_POST['send'])) ?> <?php include("http://erbimages.com/php/doctype/index.php"); ?> <?php include("http://erbimages.com/php/head/index.php"); ?> <div class="newsletter"> <ul> <form method="post" action="http://lilyandbenson.com/newletter/index.php"> <li> <input size="20" maxlength="50" name="Name" value="Name" onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;"> </li> <li> <input size="20" maxlength="50" name="Email" value="Email" onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;"> </li> <li> <input type="submit" name="send" value="Send" id="register_send"> </li> </form> <?php ?> </ul> <div class="clear"></div> </div> <div class="section_error"> <?php if(isset($error)) echo '<span id="section_error">'.$error.'</span>'; if(isset($send) && $send== true){ echo 'Thank you, your message has been sent.'; } if(!isset($_POST['send']) || isset($error)) ?> <div class="clear"></div> </div> </body> </html> 
+4
source share
3 answers

Read the documentation here http://php.net/manual/en/function.mail.php

Specially this part:

"The optional_parameters parameter can be used to send additional flags as command line parameters to a program configured to be used when sending mail, as defined by the sendmail_path configuration parameter. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail parameter.

The user who is running the web server should be added as a trusted user in the sendmail configuration to prevent the "X-Warning" header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is / etc / mail / trusted -users. "

Thus, you need to install sendmail on your system, and the user who is running the web server must have the right to send email through sendmail .

Check /var/log/apache2/error.log (if you are on Linux and you are using an Apache server) or some kind of log for your web server to find any key.

0
source

I do not know your specific needs for this, but if it is for a professional project, you should use something like PHPMailer, which will abstract the entire question asked by mail from your code.

Since your current code does not work if the recipient mail client does not support the HTML message, for example. What happens if you need to use an SMTP server instead of the php mail function? etc...

Moreover, SMTP RFC is not actually enforced among SMTP servers; this type of thing will be handled by PHPMailer.

+1
source

1) "My email code does not work" is not an error message - you need to be specific regarding your expectations and why your code does not meet these expectations.

2) You do not need all this code to replicate the problem.

You will most likely receive much more help if you are specific and provide relevant information and omit non-essential things.

The code itself is bad. In addition to the absence of any relevant comments and error traps, they are widely open to abuse by inserting an email header. Your regex for checking email addresses will discard valid addresses and accept invalid ones. IIRC, using curly braces around array indices, is deprecated. But this is probably not because it does not meet your expectations.

Assuming mail is not delivered at all, try:

 set_error_reporting(E_ALL); init_set("display_errors", 1); trigger_error("If you can't see this then your error reporting is not working"); mail(" your_address@example.com ", "test","hello world"); 

Most email problems are due to incorrect php configuration or problems with email processing systems.

There is a very good article on diagnosing software problems and getting help to solve them here . Please read it.

+1
source

Source: https://habr.com/ru/post/1312441/


All Articles