0

I'm sending a transactional HTML email from WordPress when a form is submitted. The email contains information like the person's name, for example, but I also wanted to add a link in the email to a google map.

Link example:

https://www.google.co.uk/maps/place/London/@51.528308,-0.3817765,10z/data=!3m1!4b1!4m2!3m1!1s0x47d8a00baf21de75:0x52963a5addd52a99

<a> example:

<a target="_blank" href=" ' . $google_map . ' " style="color:#267ec8; font-size:14px; line-height:22px; font-weight:normal" color="#267ec8"><span style="color:#267ec8; font-size:14px; line-height:22px; font-weight:normal">See map</span></a>

$google_map is the variable containing the google map URL which is dynamically added by the WP backend. Unfortunately, the link doesn't work when the email is received. Does anyone know how to fix this?

serraosays
  • 7,163
  • 3
  • 35
  • 60
ttmt
  • 5,822
  • 27
  • 106
  • 158

2 Answers2

1

If is possibile you should format the content of the email body adding somethings like this

<?php 

$google_map = 'https://www.google.co.uk/maps/place/London/@51.528308,-0.3817765,10z/data=!3m1!4b1!4m2!3m1!1s0x47d8a00baf21de75:0x52963a5addd52a99';

echo '<a target="_blank" href=" ' . $google_map . 
' " style="color:#267ec8; font-size:14px; line-height:22px; 
     font-weight:normal" color="#267ec8">
<span style="color:#267ec8; font-size:14px; line-height:22px;
  font-weight:normal">See map</span></a></td>';   
?>
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
1

The simplest answer to your question would be to put <?php> tags around the $google_map variable your <a> tag like this:

<a target="_blank" href="<?php echo $google_map; ?>">See map</span></a>

I'm assuming you have defined that variable somewhere in your PHP document but you shouldn't be putting hard coded variables into WordPress templates like this. If it's a custom field and this PHP variable needs to be printed inside of a WordPress loop, the solution look would look more like this:

<a target="_blank" href="<?php the_field('google_map_variable_input'); ?>">See map</span></a>

If you are generating the HTML email yourself, you need to define many more parameters than you have laid out here. This a basic example adapted from a web tutorial HTML email that should give you a general sense of how to format an HTML email in PHP:

<?php
  // Setup sending address parameters for eventual php mail() call
  $to = 'someone@example.com';
  $subject = 'Email Update About XYZ';
  $from = 'person@example.com';

  // To send HTML mail, the Content-type header must be set
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

  // Create email headers
  $headers .= 'From: '.$user_email."\r\n".
      'Reply-To: '.$user_email."\r\n" .
      'X-Mailer: PHP/' . phpversion();

  // Create email body
  // Again, assumes $google_map is defined somewhere else
  $message = '<html><body>';
  $message .= '<a target="_blank" href="';
  $message .= $google_map;
  $message .= '" style="color:#267ec8; font-size:14px; line-height:22px; font-weight:normal" color="#267ec8"><span style="color:#267ec8; font-size:14px; line-height:22px; font-weight:normal">See map</span></a>';
  $message .= '</body></html>';

  // Send email
  if(mail($to, $subject, $message, $headers)){
      echo 'Your mail has been sent successfully.';
  } else{
      echo 'Unable to send email. Please try again.';
  }
?>
serraosays
  • 7,163
  • 3
  • 35
  • 60