Jump to content

PHP function to send email to manual email entry


slawotrend
 Share

Recommended Posts

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 by slawotrend
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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          = "info@email.com"; // 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>

 

 

Untitledkhhghg.png

Edited by slawotrend
Link to comment
Share on other sites

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 by slawotrend
Link to comment
Share on other sites

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

 

Untitledrheruihuirh.png

Edited by slawotrend
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

"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

Link to comment
Share on other sites

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>";
             }
        }
            
    }
}
?>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by slawotrend
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.