1

I have a form I am using to simply capture a name and email from a user. When the user submits the form an email is then fired to the specified recipient containing a link.

I had this all working but realised I needed to add a html footer to the email which was originally being read as plain text.

After doing some digging I have now tried to set the footer to read as html ,but the email no longer sends. I am not a php developer.

My question is have I missed something or am I just way off?

<?php // configure $from = 'Ether Solutions Sales <[email protected]>'; $sendTo = $_POST['remail']; $subject = "test email"; $name = $_POST['name']; $okMessage = 'your message has been sent successfully, please ask the recipient to check their inbox'; $errorMessage = 'There was an error while submitting the form. Please try again later'; // 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: '.$from."\r\n".'Reply-To: '.$from."\r\n" . 'X-Mailer: PHP/' . phpversion(); // Compose a simple HTML email message $message = '<html><body>'; $message .= '<p style="margin-bottom:30px;">regards dave</p>'; $message .= '<a href="http://www.domain.co.uk" title="link to domain website">www.domain.co.uk</a>'; $message .= '<p style="font-color:blue;">Office Tel: 0844 000 000</p>'; $message .= '<p style="font-color:blue;">Mobile: 0000 000 0000</p>'; $message .= '<p style="font-color:blue;">Technical Support: 0845 000 0000</p> '; $message .= '<p style="font-color:blue; font-weight:bold;">Some tagline here</p> '; $message .= '<img src="imageurl/test.png" alt="sample alt tag"/>'; $message .= '</body></html>'; // let's do the sending try { $emailText = "{$name} you have been sent an electronic document to be signed.\nTo view your document please click the following link URL: /www.domain.com"; mail($sendTo, $subject, $emailText, $message, $headers, "From: " . $from); $responseArray = array('type' => 'success', 'message' => $okMessage); } catch (\Exception $e) { $responseArray = array('type' => 'danger', 'message' => $errorMessage); } if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $encoded = json_encode($responseArray); header('Content-Type: application/json'); echo $encoded; } else { echo $responseArray['message']; } 
2

2 Answers 2

1

I tested your code and got this warning:

PHP Warning: mail() expects at most 5 parameters, 6 given in /home/ruemercc/public_html/lab/email2/mail.php on line 47

I would suggest you use PHPMailer library found here phpmailer

. It will allow you to even send bulk emails(Which I think will be an added advantege in your case). Have fun coding

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Mwangi, I have merged both the $message and $emailText and the html email now sends although I now have to figure out how I am going to parse the clients $name as $message is being used with html
Ok. Do you mean contatinating the $name into $message?
$message = '<html><body>'; $message .= '<p style="margin-bottom:30px;">regards '.$name.'</p>'; $message .= '<a href="domain.co.uk" title="link to domain website">www.domain.co.uk</a>'; $message .= '<p style="font-color:blue;">Office Tel: 0844 000 000</p>'; $message .= '<p style="font-color:blue;">Mobile: 0000 000 0000</p>'; $message .= '<p style="font-color:blue; font-weight:bold;">Some tagline here</p> '; $message .= '<img src="imageurl/test.png" alt="sample alt tag"/>'; $message .= '</body></html>';
Ie regards '.$name.'
Originally I had the $emailText variable contained within double quotes as it contained effectively just text which allowed me to add the $name variable via the {$name} approach. Now I have combined both into the $message variable which contains html and so I am using single quotes. I would like to add the $name variable at the start of the message to include the recipients name. Before replying I had tried .$name.which didn't work. I now have it working using $name. followed by the html string :) Thank you for pointing me in the right direction and for your patience today :)
|
1

Your call of the mail() wrong. You are setting the parameters not correct.

Take a look in the PHP Manual for mail().

From the php manual

mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] ) 

Your code mail($sendTo, $subject, $emailText, $message, $headers, "From: " . $from) uses message as $additional_headers which is wrong.

You have to merge your $message with the $emailText.

1 Comment

Thanks bro, I merged the two as suggested however I am now presented with a problem. Yes the email is sending as html which is great. But originally $emailText was also parsing the name variable at the start of the string. Can this still be achieved as $message is being used with html?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.