Button send an Email with shortcode
-
Hi,
I would like to have a button which send an Email (to the person who clicked, personn known because she is loggued.)
I am using a shortcode to do that, but i don’t succeed in good writing this shortcode.
/* Shortcode bouton */ function boutoncode($atts, $content = null) { extract(shortcode_atts(array( 'lien' => '#', 'couleur' =>'bleu' ), $atts)); $codecode = ' <input class="bouton '.$couleur.'" type="submit" name="envoyer" value="Cliquez ICI pour recevoir par Email le CODE PROMO" />'; $postcode = $_POST["envoyer"]; if ( isset( $_POST["envoyer"] ) ) { $codemail ='envoi du mail'; } return $codecode . $postcode . $codemail; } add_shortcode('boutoncode', 'boutoncode');I hav a problem with the condition.
I don’t know how to write this.I found somme example like these:
if valeur=oui $retour=super else $retour=non end-if return $retourBut i want to have something like
`when I cliked on the button, i have a value
then i return an Email’Could you help me with this ?
many thanks
-
when I do the following code, of course the button doesn’t appear and the return return the code (the condition doesn’t work in a way)
function boutoncode($atts, $content = null) { extract(shortcode_atts(array( 'lien' => '#', 'couleur' =>'bleu' ), $atts)); $codecode = ' <input class="bouton '.$couleur.'" type="submit" name="envoyer" value="Cliquez ICI pour recevoir par Email le CODE PROMO" />'; $postcode = $_POST['envoyer']; $emailcode =' if ( isset($postcode) ) { echo "TEST"; // include("send-email-contact.php"); }'; return $emailcode; } add_shortcode('boutoncode', 'boutoncode');I am searching
You should first check if $_POST[‘envoyer’] is set before doing anything with the value, otherwise your code will be throwing off undefined variable warnings. If it is not set, the button was not clicked.
You should not echo out even debug data from a shortcode handler, the output will not be where you expect it, Instead just return the debug data and WP will echo it out at the proper location.
You need to wrap the submit button element with <form> tags, using appropriate values for action and method attributes. Without this the submit button does not know where to send the data. The action attribute value can be an empty string to cause the current page to reload. The method attribute should be assigned “POST”.
Many thanks bcworkz….
of course !
I began with a thing like<a class="bouton '.$couleur.'" href="'.$lien.'" target="_blank">' . do_shortcode($content) . '</a>';then,as I wanted an action, i have changed for an input .. and forgot the method=”post”…
i think i have already tried (because i tried a lot of things) … i do it and come back.
then I used <form>
function boutoncode($atts, $content = null) { extract(shortcode_atts(array( 'lien' => '#', 'couleur' =>'bleu' ), $atts)); $codecode = ' <form method="post" > <input class="bouton '.$couleur.'" type="submit" name="envoyer" value="Cliquez ICI pour recevoir par Email le CODE PROMO" /> </form>'; $postcode = $_POST['envoyer']; $emailcode =' if ( isset($postcode) ) { echo "TEST"; // include("send-email-contact.php"); }'; return $codecode . $postcode . $emailcode; } add_shortcode('boutoncode', 'boutoncode');and with
– $codecode : i have the button on which i have to click
– $postcode : i have now a value (of course… )
– $emailcode : i have the code written on the screenif ( isset($postcode) ) { echo "TEST"; // include("send-email-contact.php"); }that’s this part i don’t really know how to do…. having the Email sending and in the same time the button (all in return?)..
i am still searchiing .. i continue..
i have tried this code
/* Shortcode bouton */ function boutoncode($atts, $content = null) { extract(shortcode_atts(array( 'lien' => '#', 'couleur' =>'bleu' ), $atts)); $codecode = ' <form method="post" > <input class="bouton '.$couleur.'" type="submit" name="envoyer" value="Cliquez ICI pour recevoir par Email le CODE PROMO" /> </form>'; $postcode = $_POST['envoyer']; if ( isset($postcode) ) { $emailcode =' TEST'; // include("send-email-contact.php"); } return $codecode . $postcode . $emailcode; } add_shortcode('boutoncode', 'boutoncode');and when I click, my post is written and ‘TEST’ too..
so maybe it works.
i’m going to try to send an Email…
then I come bakc (with the solution I hope”okay, i still have problem. I don’t know how to exectite the action “send an Email”.
I have :
function boutoncode($atts, $content = null) { extract(shortcode_atts(array( 'lien' => '#', 'couleur' =>'bleu' ), $atts)); $codecode = ' <form method="post" > <input class="bouton '.$couleur.'" type="submit" name="envoyer" value="Cliquez ICI pour recevoir par Email le CODE PROMO" /> </form>'; $postcode = $_POST['envoyer']; if ( isset($postcode) ) { //$emailcode = " wp_mail( 'myemail@gmail.com', 'test shortcode', do_shortcode($message), $headers, $attachments )"; $headers = 'From: Maplus'; $emailcode = mail('myemail@gmail.com', 'test shortcode', 'test', $headers); // include("send-email-contact.php"); } return $codecode . $emailcode; } add_shortcode('boutoncode', 'boutoncode');ANd when I click, that returns 1 (not true or false but 1).
And when in the code I use = ‘ ‘ for $emailcode like
$emailcode = 'mail('myemail@gmail.com', 'test shortcode', 'test', $headers)';that return the text of the code…. that writes the text of the code on the screen.
DO you see which problem is it?
Could you help me?many thanks for your help
okay, i have tried to take out the action “send an Email” from the return.
I have this code :
function boutoncode($atts, $content = null) { global $wpdb; extract(shortcode_atts(array( 'lien' => '#', 'couleur' =>'bleu' ), $atts)); $codecode = ' <form method="post" > <input class="bouton '.$couleur.'" type="submit" name="envoyer" value="Cliquez ICI pour recevoir par Email le CODE PROMO" /> </form>'; $postcode = $_POST['envoyer']; if ( isset($postcode) ) { // Code PHP pour traiter l'envoi de l'email - header général if (!preg_match("#^[a-z0-9._-]+@(hotmail|live|msn).[a-z]{2,4}$#", $email)) { $passage_ligne = "\r\n"; } else { $passage_ligne = "\n"; } $headers = 'From: MAplus'.$passage_ligne; $headers .= "MIME-Version: 1.0".$passage_ligne; $headers .= 'Content-type: text/html; charset=UTF-8' .$passage_ligne; mail('myEmail@gmail.com', 'test shortcode', 'test', $headers); echo 'ENVOI ?'; // include("send-email-contact.php"); } return $codecode; } add_shortcode('boutoncode', 'boutoncode');I have the button, and when I click, the text “ENVOI?” appears, but i don’t receive any Email.
Then, i don’t know if I can send an Email like this or if I am wrong with using mail() founction.
Many thanks for you help
when I use the following code
function boutoncode($atts, $content = null) { global $wpdb; extract(shortcode_atts(array( 'lien' => '#', 'couleur' =>'bleu' ), $atts)); $codecode = ' <form method="post" > <input class="bouton '.$couleur.'" type="submit" name="envoyer" value="Cliquez ICI pour recevoir par Email le CODE PROMO" /> </form>'; $postcode = $_POST['envoyer']; if ( isset($postcode) ) { // Code PHP pour traiter l'envoi de l'email - header général if (!preg_match("#^[a-z0-9._-]+@(hotmail|live|msn).[a-z]{2,4}$#", 'monemail@gmail.com')) { $passage_ligne = "\r\n"; } else { $passage_ligne = "\n"; } $headers = 'From: MAplus'.$passage_ligne; $headers .= "MIME-Version: 1.0".$passage_ligne; $headers .= 'Content-type: text/html; charset=UTF-8' .$passage_ligne; mail('monemail@gmail.com', 'test shortcode', 'test', $headers); if (mail('maity_max@yahoo.fr', 'test shortcode', 'test', $headers)==true) { echo 'ENVOI'; } else { echo ' PB ENVOI'; } // include("send-email-contact.php"); } return $codecode; } add_shortcode('boutoncode', 'boutoncode');the text “ENVOI” is written. Son that means that the function mail () return true… but i don’t receive any Email…
have you got a tip for me ?
many thanks
There’s any number of reasons why you did not receive mail. Did you check your spam folder? Communication could have failed farther down the line, external to both your server and your mail service. You are likely on shared hosting, your server could have been blacklisted due to actions of another server user. Your server may not be configured to send mail.
You might try wp_mail() instead of mail(). It’s a little more robust in trying to get email sent out. Try entering your domain in the DNSreport field at dnsstuff.com. Check the MX and MAIL sections of the resulting report.
wouaouh, okay that is a really good answer !
Many thanks bcworkz.Yes, i have check my spam but nothing there…
I have also already tried with wp_mail (as i saw it wasn’t working, i tried a few things … 😉 )
and the according to what you said, i am going to keep wp_mail.then, i will check DNSreport…. there is an issue actually with the mail of the hosting. But i thought that didn’t interfer with what i do…. i check..
thanks again !
You’re welcome! It’s sounding like some sort of hosting issue. I hope you can resolve this easily.
HI ,
Okay, the last code works… it was a hosting issue. That’s why the Email sending didn’t work…
so, many thanks, the last code I sent work….
solved !
The topic ‘Button send an Email with shortcode’ is closed to new replies.