19 Comments

  1. I want to use your php code but I am not sure where to put it or how to trigger it
    Ideally, I would like to have a button using this sort of code:

    however onClick would send an email to me to tell me the user is interested or wants more information about something

  2. So Andrew what you want to do on this case is setup a page to do this the email.

    Create a [WordPress page template](http://codex.wordpress.org/Page_Templates) with a code that will parse some values, like below;

    “`php
    $post = get_post( absint( $_GET[‘_post_id’ ) );
    $email = esc_attr( $_GET[‘_email’] );
    $name = esc_attr( $_GET[‘_name’] );

    $to = “your_email_here@yoursite.com”;
    $subject = “Interest on ” . $post->post_title . ” by ” . $email;
    $content =
    “Name: {$name}” .
    “Email:
    {$email}” .
    “Product:
    {$post->post_title}” .
    “Product link:
    ” . get_permalink( $post ) . ““;

    $headers = array(
    ‘Reply-To’ => $name . ‘<' . $email . '>‘,
    );

    add_filter( ‘wp_mail_content_type’, ‘set_html_content_type’ );

    $status = wp_mail( $to, $subject, $content );

    // Reset content-type to avoid conflicts — http://core.trac.wordpress.org/ticket/23578
    remove_filter( ‘wp_mail_content_type’, ‘set_html_content_type’ );

    function set_html_content_type() {
    return ‘text/html’;
    }

    // If status correct then redirect the user to the product page again
    if ( $status ){
    wp_redirect( get_permalink( $post ) );
    } else {
    // if the status of the email is false do something
    }

    “`

    With that code in place you could send the user via a link to the page with the need params:

    Exemple: `http://yoursite.com/interest?_post_id={$ID}&_email={$user_email}&_name={$user_name}`

    Don’t forget to URL encode the user name and user email. Hope this will help you reach your goal.

    1. So, my only concern with using Customer.io is that that will add up a price that in most cases people don’t want to have.

      And by suggesting another service you will not remove the need to know where to put the code to send the email.

  3. Gustavo, I opened an account with sendgrid, installed their wordpress plugin and did all of the settings correctly. Sent an email via SMTP and got an error message. Then changed the email settingt to API and it said that the email was sent out. Also the SendGrid dashboard said that the email was sent out but the email was never received. Did check in spam folder and nothing. DOes using sendgrid bypass the issues of the shared server being blacklisted? I use Hostgator and the first test I did with email I got the email in spam folder. Subsequent tests that I did email was never received and it was after that that I started researching this issue and found this email. I thought this would solve my problems. Any ideas?

    1. Hi Roberto,
      In most cases it shouldn’t have a problem delivering a simple email, when you are using SendGrid you will not have problems with a server been blacklisted, although you could have your domain blacklisted.

      Try to check if your SPF and DKIM are configured on your SendGrid account, [this article about white labeling your domain](https://sendgrid.com/docs/User_Guide/whitelabel_wizard.html) should point you in the right direction:

      If you are still in trouble after trying that, do a simple test of using MailGun just to see if there is something on SendGrid (their API) that is not right on your case.

  4. Hi, this is working well for me. I’m using the mailgun wordpress plugin and the wp_mail() call. However, I cant seem to set the ‘From’ and ‘Reply-to’ headers manually.

    If I pass the headers array as the 4th argument, as in your example, it still sends from ‘WordPress’ and no reply-to is set in my message source either.

    Do you know if the WP mailgun plugin overrides the headers passed into wp_mail() ?

  5. Hi Chris,

    From what I understand the Mailgun should allow you to change the `reply-to` depending on how you configured their system (on their website).

    Can you post a code sample?

    Use the following template:

    \`\`\`php

    // Code

    \`\`\`

    1. Hi Vinay,

      Where are you getting this `wp_mail` undefined error? I mean `wp_mail` should always be defined within WordPress. Give me more information to help you here on this one. ?

  6. Hey,

    i am trying to send an email if a event in the “the-events-calendar-plugin” is about to happen in around one week.

    Is this possible without writing hundred lines of code?

    Many thanks!

    Julian

  7. Hi, – I was hoping that you could help me – having a heck of a time changing the recipient email from an array . Try number 1001 is this, try number 1000 did not have the esc_attr on $recipient. I have tried it like green eggs and ham, in the class, through woo and through wp_mail directly and nothing seems to work on the $recipient – was able to get it to work in a class but only to send it to the predefined woo emails (customer and admin)

    “`php
    function ecard_email_send(){
    $sitename = get_bloginfo(‘name’);
    $siteurl = site_url();
    $cardurl = ($siteurl . ‘/’ . $post_title);
    $recipient = esc_attr($lineItem[‘recipient_email’]);
    $headers = array(‘Content-Type: text/html; charset=UTF-8’);
    $subject = ($lineItem[‘sender_name’] . ‘ has sent you an Ecard from ‘ . $sitename);
    $sitename = get_bloginfo(‘name’);
    $siteurl = site_url();
    $cardurl = ($siteurl . ‘/’ . $post_title);

    $content = array(
    ‘name’ => $lineItem[‘recipient_name’],
    ‘verse’ => $lineItem[‘ecard_message’],
    ‘greeting’ => $lineItem[‘ecard_greeting’],
    ‘closing’ => $lineItem[‘ecard_closing’],
    ‘sender’ => $lineItem[‘sender_name’],
    ‘image’ => $ecardbackground[0],
    ‘link’ => $cardurl,
    ‘value’ => ‘View your card online’
    );

    ob_start();
    include(MY_PLUGIN_PATH . ‘/templates/ecard_email.php’);
    $message = ob_get_contents();
    ob_end_clean();

    $status = wp_mail( $recipient, $subject, $message, $headers );
    exit();
    }
    “`

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.