slawotrend Posted March 27, 2019 Share Posted March 27, 2019 (edited) Hello Guys, I need your help as many PHPers here is more experienced in PHP coding than me. I have specific project I am working on and need a piece of code that can send an email from HTML form using PHP to the email address that is entered manually on the form, instead of standard sent to PHP code that is fixed within PHP script and executed during submission. I want sth that can grab manually enetered recipient's e-mail address, paste it to the PHP code and then use it to send the email to the recipient, instead of fixed sent to code. Something would say dynamically changed during the entry that can inject into PHP code the new address email entered on the form and then submit to it. Any ideas will be great. Thanks. Edited March 27, 2019 by slawotrend Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/ Share on other sites More sharing options...
gw1500se Posted March 27, 2019 Share Posted March 27, 2019 This is a typical application. I suggest you read up on PHPMailer. Once you start programming it, if you have issues post the code here for help. Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565652 Share on other sites More sharing options...
ginerjm Posted March 27, 2019 Share Posted March 27, 2019 Just to be sure. You want a form that captures the user's input of an email address. Then you want a php script that is called by that form that will grab the user's entered address and use it to send some kind of email to that address. Is that what you were saying? If so then Gw's response is a good one. If you are just beginning to write PHP code to send emails, read up on using the phpmailer class and download it an use it. It makes all future emailing projects that much simpler. Of course if my understanding of your question is incorrect, please try and re-state it so I/we can better help you. Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565653 Share on other sites More sharing options...
slawotrend Posted March 27, 2019 Author Share Posted March 27, 2019 (edited) I have already done my part and it works for standard fixed sender email address like this: if(isset($_POST["submitted"]) && $_POST["submitted"] == 1) { //Read POST request params into global vars $to_email = "[email protected]"; // I want here the manual entry on HTML form that will replaced fixed email with the one entered on the form $from_fullname = trim(strip_tags($_POST['emailaddress'])); $from_email = trim(strip_tags($_POST['email'])); $email_subject = trim(strip_tags($_POST['subject'])); $email_message = nl2br(trim(strip_tags($_POST['message']))); $security_code = trim(strip_tags($_POST['vpb_captcha_code'])); Please see the form I have in HTML in the pic attached Any suggestions? html is like this <div style="width:400px; text-align: left; float: left; height: 34px;"><input type="text" id="emailaddress" name="Supplier1" value="" class="input_fields" /></div> Edited March 27, 2019 by slawotrend Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565655 Share on other sites More sharing options...
gw1500se Posted March 27, 2019 Share Posted March 27, 2019 I guess we don't understand your question. You are already using $_POST to set the other parameters. Using it to set the to address is no different. Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565656 Share on other sites More sharing options...
slawotrend Posted March 27, 2019 Author Share Posted March 27, 2019 (edited) should it be sth like this where $to email and $from email are like: let's say HTML <div style="width:400px; text-align: left; float: left; height: 34px;"><input type="text" id="emailaddress1" name="Supplier1" value="" class="input_fields" /></div> <div style="width:400px; text-align: left; float: left; height: 34px;"><input type="text" id="emailaddress2" name="Supplier1" value="" class="input_fields" /></div> <div style="width:400px; text-align: left; float: left; height: 34px;"><input type="text" id="emailaddres3" name="Supplier1" value="" class="input_fields" /></div> and PHP part $to_email = trim(strip_tags($_POST['mailaddress1'])); $to_email = trim(strip_tags($_POST['emailaddres2'])); $to_email = trim(strip_tags($_POST['email address3'])); So basically when in HTML form the email will be set by manual entry the POST will use it? Edited March 27, 2019 by slawotrend Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565657 Share on other sites More sharing options...
gw1500se Posted March 27, 2019 Share Posted March 27, 2019 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565658 Share on other sites More sharing options...
slawotrend Posted March 27, 2019 Author Share Posted March 27, 2019 so easy lol thx for enlighment Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565659 Share on other sites More sharing options...
gw1500se Posted March 27, 2019 Share Posted March 27, 2019 Just one more point. Are you sending the email to a list of recipients or just one? If a list each $_POST variables should be set to one $to_email variable as a comma separated string. Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565660 Share on other sites More sharing options...
slawotrend Posted March 27, 2019 Author Share Posted March 27, 2019 (edited) I got you regarding $to email. $to_email = trim(strip_tags($_POST['mailaddress1, mailaddress2, mailaddress3'])); now, I want to grab all HTML form or text within all fields and submit it as HTML email not only the text/message field I have. should I do sth like this? PHP example part (supplier2 and 3 is mailaddress2 and 3 in post ) $email_message = nl2br(trim(strip_tags($_POST['mailaddress3']))); $email_message = nl2br(trim(strip_tags($_POST['mailaddress4']))); $email_message = nl2br(trim(strip_tags($_POST['email']))); $email_message = nl2br(trim(strip_tags($_POST['subject']))); $email_message = nl2br(trim(strip_tags($_POST['message']))); $security_code = trim(strip_tags($_POST['vpb_captcha_code'])); //Set up the email headers $headers = "From: $from_fullname <$from_email>\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "Message-ID: <".time().rand(1,1000)."@".$_SERVER['SERVER_NAME'].">". "\r\n"; if(empty($_SESSION['captcha_code']) || strcasecmp($_SESSION['vcaptcha_code'], $_POST['captcha_code']) != 0) { //Note: the captcha code is compared case insensitively. If you want case sensitive match, update the check above to strcmp() $submission_status = '<div class="info" align="left">Please enter the correct code.</div>'; } else { $vasplus_mailer_delivers_greatly = @mail($to_email, $email_subject, $email_message, $headers); if ($vasplus_mailer_delivers_greatly) { //Displays the success message when email message is sent $submission_status = "<div align='left' class='vpb_success'>The message has been sent successfully!</div>"; thx Edited March 27, 2019 by slawotrend Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565661 Share on other sites More sharing options...
gw1500se Posted March 27, 2019 Share Posted March 27, 2019 You are overwriting the message string each time so only the last part will be in the message. Note the .= for $headers. It means concatenate to what is already in the variable. You will need to use the same notation for setting $email_message to concatenate the data. Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565663 Share on other sites More sharing options...
slawotrend Posted March 27, 2019 Author Share Posted March 27, 2019 (edited) Should i do then sth like this in $email_message = nl2br(trim(strip_tags($_POST['mailaddress3, mailaddress4, email, subject, message']))); Edited March 27, 2019 by slawotrend Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565664 Share on other sites More sharing options...
ginerjm Posted March 27, 2019 Share Posted March 27, 2019 I'd like to point out that you have to use a "from" address that exists on your server/domain. It won't (probably) send something that has a "fake" from address - it has to be a legitimate one that your domain recognizes. As for the to address. You have to take each input value (post) and clean it and add it to the to address. $to_email = trim($_POST['email1']); $to_email .= ','.teim($_POST['email2']); $to_email .= ','.trim($_POST['email3']); (Note the .= usage) Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565665 Share on other sites More sharing options...
slawotrend Posted March 27, 2019 Author Share Posted March 27, 2019 3 minutes ago, ginerjm said: I'd like to point out that you have to use a "from" address that exists on your server/domain. It won't (probably) send something that has a "fake" from address - it has to be a legitimate one that your domain recognizes. As for the to address. You have to take each input value (post) and clean it and add it to the to address. $to_email = trim($_POST['email1']); $to_email .= ','.teim($_POST['email2']); $to_email .= ','.trim($_POST['email3']); (Note the .= usage) Thx will do like this, I am going to set all to smtp that doesn't require own email server and domain, but not at this stage yet. How to do with my previous reply to gw1500 i mean to send all content on the form within email message , I have suggested if can do sth like $email_message = nl2br(trim(strip_tags($_POST['mailaddress3, mailaddress4, email, subject, message']))); but not sure Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565666 Share on other sites More sharing options...
ginerjm Posted March 27, 2019 Share Posted March 27, 2019 NO YOU CAN'T. You have to reference each item from the form with its real name. $_POST['email1'], $_POST['email2'], etc.... As in: $to_mail1 = trim(strip_tags($_POST['mailaddress1'])); $to_mail2 = trim(strip_tags($_POST['mailaddress2'])); Then use the new vars $to_mail1 and $to_mail2 later from then on. If you are not sending your email as an html email you don't want to use nl2bt function on the data. Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565667 Share on other sites More sharing options...
ginerjm Posted March 27, 2019 Share Posted March 27, 2019 PS - are you reading the PHP manual for help at all? Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565668 Share on other sites More sharing options...
slawotrend Posted March 27, 2019 Author Share Posted March 27, 2019 2 minutes ago, ginerjm said: NO YOU CAN'T. You have to reference each item from the form with its real name. $_POST['email1'], $_POST['email2'], etc.... As in: $to_mail1 = trim(strip_tags($_POST['mailaddress1'])); $to_mail2 = trim(strip_tags($_POST['mailaddress2'])); Then use the new vars $to_mail1 and $to_mail2 later from then on. If you are not sending your email as an html email you don't want to use nl2bt function on the data. different way of my thinking and makes sense thanks. Now I've got it. Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565669 Share on other sites More sharing options...
slawotrend Posted March 27, 2019 Author Share Posted March 27, 2019 more tomorrow. thanks guys and talk to you soon , i guess Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565670 Share on other sites More sharing options...
ginerjm Posted March 27, 2019 Share Posted March 27, 2019 "different way of my thinking"..... Yes it is. It is the CORRECT way. No discussion. You can't reference different elements of any array by just using the indices without the array name itself. And - reading the manual on each of these functions you are using will definitely help you to understand how to use them and how to (correctly) write them. Here's a Link to the Functions in the PHP Manual Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565682 Share on other sites More sharing options...
slawotrend Posted March 28, 2019 Author Share Posted March 28, 2019 I'm back because the form send only to one address from the form instead to supplier 1-4 at the same time that's my code: Help will be great <?php session_start(); ob_start(); //ini_set('error_reporting', E_NONE); if(isset($_POST["submitted"]) && $_POST["submitted"] == 1) { //Read POST request params into global vars $to_email1 = trim(strip_tags($_POST['sup1'])); // supplier email on the form $to_email2 .= ','.trim(strip_tags($_POST['sup2'])); $to_email3 .= ','.trim(strip_tags($_POST['sup3'])); $to_email4 .= ','.trim(strip_tags($_POST['sup4'])); $part_no = trim(strip_tags($_POST['partno'])); $item_name = trim(strip_tags($_POST['item'])); $volume_exp = trim(strip_tags($_POST['volume'])); $quote_receipt = trim(strip_tags($_POST['quote'])); $from_sender = trim(strip_tags($_POST['sender'])); $from_email = trim(strip_tags($_POST['senderemail'])); $email_subject = trim(strip_tags($_POST['subject'])); $email_message = nl2br(trim(strip_tags($_POST['message']))); $security_code = trim(strip_tags($_POST['vpb_captcha_code'])); //Set up the email headers $headers = "From: $from_sender <$from_email>\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "Message-ID: <".time().rand(1,1000)."@".$_SERVER['SERVER_NAME'].">". "\r\n"; if($from_sender == "") { $submission_status = '<div class="vpb_info" align="left">Please enter your message.</div>'; } elseif($security_code == "") { $submission_status = '<div class="vpb_info" align="left">Please enter the security code.</div>'; } elseif(!isset($_SESSION['vpb_captcha_code'])) { $submission_status = '<div class="vpb_info" align="left">Sorry, missing session. Please refresh and try again.</div>'; } else { if(empty($_SESSION['vpb_captcha_code']) || strcasecmp($_SESSION['vpb_captcha_code'], $_POST['vpb_captcha_code']) != 0) { //Note: the captcha code is compared case insensitively. If you want case sensitive match, update the check above to strcmp() $submission_status = '<div class="vpb_info" align="left">Please enter the correct code.</div>'; } else { $mailer = @mail($to_email1, $to_email2, $to_email3, $to_email4, $email_subject, $email_message, $headers); if ($mailer) { //Displays the success message when email message is sent $submission_status = "<div align='left' class='vpb_success'>The message has been sent successfully!</div>"; } else { //Displays an error message when email sending fails $submission_status = "<div align='left' class='vpb_info'>Sorry, your email could not be delievered. <br>Please try again or contact website admin.</div>"; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565693 Share on other sites More sharing options...
ginerjm Posted March 28, 2019 Share Posted March 28, 2019 Why do you not have error reporting turned on? AND if you RTFM you would see that you are using the mail function badly. And PLEASE don't ever use the @ sign to suppress errors. If you have then you should SEE them. Plus - with the @ what is the point of capturing the result when you suppress it? Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565697 Share on other sites More sharing options...
gw1500se Posted March 28, 2019 Share Posted March 28, 2019 Also please use the code (<>) formatter and set it to PHP. Your code will be much easier to read. Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565698 Share on other sites More sharing options...
slawotrend Posted March 28, 2019 Author Share Posted March 28, 2019 (edited) 1 hour ago, slawotrend said: I'm back because the form send only to one address from the form instead to supplier 1-4 at the same time that's my code: Help will be great <?php session_start(); ob_start(); //ini_set('error_reporting', E_NONE); if(isset($_POST["submitted"]) && $_POST["submitted"] == 1) { //Read POST request params into global vars $to_email1 = trim(strip_tags($_POST['sup1'])); // supplier email on the form $to_email2 .= ','.trim(strip_tags($_POST['sup2'])); $to_email3 .= ','.trim(strip_tags($_POST['sup3'])); $to_email4 .= ','.trim(strip_tags($_POST['sup4'])); $part_no = trim(strip_tags($_POST['partno'])); $item_name = trim(strip_tags($_POST['item'])); $volume_exp = trim(strip_tags($_POST['volume'])); $quote_receipt = trim(strip_tags($_POST['quote'])); $from_sender = trim(strip_tags($_POST['sender'])); $from_email = trim(strip_tags($_POST['senderemail'])); $email_subject = trim(strip_tags($_POST['subject'])); $email_message = nl2br(trim(strip_tags($_POST['message']))); $security_code = trim(strip_tags($_POST['vpb_captcha_code'])); //Set up the email headers $headers = "From: $from_sender <$from_email>\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "Message-ID: <".time().rand(1,1000)."@".$_SERVER['SERVER_NAME'].">". "\r\n"; if($from_sender == "") { $submission_status = '<div class="vpb_info" align="left">Please enter your message.</div>'; } elseif($security_code == "") { $submission_status = '<div class="vpb_info" align="left">Please enter the security code.</div>'; } elseif(!isset($_SESSION['vpb_captcha_code'])) { $submission_status = '<div class="vpb_info" align="left">Sorry, missing session. Please refresh and try again.</div>'; } else { if(empty($_SESSION['vpb_captcha_code']) || strcasecmp($_SESSION['vpb_captcha_code'], $_POST['vpb_captcha_code']) != 0) { //Note: the captcha code is compared case insensitively. If you want case sensitive match, update the check above to strcmp() $submission_status = '<div class="vpb_info" align="left">Please enter the correct code.</div>'; } else { $mailer = @mail($to_email1, $to_email2, $to_email3, $to_email4, $email_subject, $email_message, $headers); if ($mailer) { //Displays the success message when email message is sent $submission_status = "<div align='left' class='vpb_success'>The message has been sent successfully!</div>"; } else { //Displays an error message when email sending fails $submission_status = "<div align='left' class='vpb_info'>Sorry, your email could not be delievered. <br>Please try again or contact website admin.</div>"; } } } } ?> Regaring to the problem the code works only for one $to_email, any suggestions, I have properly defined parameters in html and it only works for first $to_email HTML is like <input type="text" id="sup1" name="sup1" value="<?php echo strip_tags($_POST["sup1"]); ?>" <input type="text" id="sup2" name="sup2" value="<?php echo strip_tags($_POST["sup2"]); ?>" <input type="text" id="sup3" name="sup3" value="<?php echo strip_tags($_POST["sup3"]); ?>" <input type="text" id="sup4" name="sup4" value="<?php echo strip_tags($_POST["sup4"]); ?>" I am new PHP so any example code would help Edited March 28, 2019 by slawotrend Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565699 Share on other sites More sharing options...
ginerjm Posted March 28, 2019 Share Posted March 28, 2019 I already gave you my suggestion. Apparently you don't care for what I am saying. Good bye. Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565701 Share on other sites More sharing options...
slawotrend Posted March 28, 2019 Author Share Posted March 28, 2019 It's not that that I don't care. I am just new to PHP and your suggestions doesn't work. Is this forum to discuss things and get help from more experienced people or to be upset, because simply someone new may not understand everything? Quote Link to comment https://forums.phpfreaks.com/topic/308519-php-function-to-send-email-to-manual-email-entry/#findComment-1565702 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.