how to send a message to email with php - php

I'm trying to send a message to the email a user provides in the contact form. The problem is the message never gets sent, but I always arrive at a blank page where my php code is located. Nothing warns me of any error in my code. Can anyone explain why this is happening and offer a solution to the problem?
<form action="site.php" method="POST">
<input
type="text"
class="form"
name="email"
placeholder="Your email address"
/>
<button class="submit" type="submit">Join Waitlist</button>
</form>
<?php
if (isset($_POST["submit"]))
{
$mailTo = $_POST["email"];
$mailFrom = "Dumele";
$message = "https://docs.google.com/forms/d/1lpj2XnKW4HT_qHFfGwpUxcvzPmK2USZ0MGSDP0XCqfg/edit";
$subject = "Welcome to Dumele";
$txt = "Thank you for your interest in Dumele. We're glad to have
you join our network and mission to enhance the technological
innovation of our African diaspora. Below is a link to a survey
we would like you to answer so we can better assist you.\n\n".message;
$headers = "From: ".mailFrom;
(mail($mailTo, $subject, $txt, $headers));
header("Location: index.php?mailsend");
}
?>

First of all make sure you enabled error reporting. You can check another Stackoverflow question and it's answers here about it.
As I see in your code you have syntax errors. You didn't place $ sign before variable names. For example you typed $headers = "From: ".mailFrom; instead of $headers = "From: ".$mailFrom; Let's fix it:
<?php
if (isset($_POST["submit"]))
{
$mailTo = $_POST["email"];
$mailFrom = "Dumele";
$message = "https://docs.google.com/forms/d/1lpj2XnKW4HT_qHFfGwpUxcvzPmK2USZ0MGSDP0XCqfg/edit";
$subject = "Welcome to Dumele";
$txt = "Thank you for your interest in Dumele. We're glad to have
you join our network and mission to enhance the technological
innovation of our African diaspora. Below is a link to a survey
we would like you to answer so we can better assist you.\n\n".$message;
$headers = "From: ".$mailFrom;
(mail($mailTo, $subject, $txt, $headers));
header("Location: index.php?mailsend");
}
Now with the mail() function of PHP; some servers disables mail() function for security purposes. If so; you can use SMTP to securely send your emails. To use SMTP in PHP of course you need additional processes but some free software packages and libraries like PHPMailer or SwiftMailer can help you about it.

This is looking for a form value with the name "submit":
if (isset($_POST["submit"]))
But there's no form element in the HTML with that name. So this will always be false. Give your submit button that name:
<button class="submit" type="submit" name="submit">Join Waitlist</button>
It shouldn't necessarily need a value, it would just default to an empty string. But it needs a name in order for the browser to send anything at all with that key.
As an aside, your mail server may reject the message since this is not really an email address:
$mailFrom = "Dumele";
For completeness... It looks like your PHP variables are also syntactically incorrect. Variable names need to begin with a $. For example, this:
$headers = "From: ".mailFrom;
Should be this:
$headers = "From: ".$mailFrom;
The same error would need to be corrected anywhere you're mis-using variable names.

Use value attribute in button tag. You are testing
if(isset($_post['submit']))
But what is submit? You should use value attribute and give a value submit i.e. Submit

Related

retrieve the receiver's email address using html

So I am sending an HTML e-mail which is an invitation, and I would want users to click Join! if they would want to join the invitation, but then I have no idea how to retrieve their e-mail addresses. I can only get hits of how many "Yes"es I have got, I can't see which e-mail has actually said yes. How can I get their e-mail address too? beside providing them with a text field asking them to fill it up.
Any thoughts? thanks :)
My cade so far:
<html>
<body>
<form action="myserver.com" method="POST" target="_blank">
<input type="hidden" name="answer" value="yes"/>
<input type="submit" name="submit" value="Join!"/>
</form>
</body>
</html>
For the server side, I am using PHP to retrieve the data. Could anything be done from there? I can't use PHP or JS in my e-mails as far as I know.
Since you mention PHP, why not send the email programmatically?
Assuming you have a working SMTP configuration (see Local SMTP), all email adresses in an array, you could use PHP to send out the emails.
See the examples at the resources for specific headers needed for HTML
<?php
$email_addresses = array('foo#bar.tld', 'no#name.it');
// subject
$subject = 'Invitation';
// message
$message = '
<html>
<body>
<form action="myserver.com" method="POST" target="_blank">
<input type="hidden" name="answer" value="yes"/>
<input type="hidden" name="email" value="%placeholder%" />
<input type="submit" name="submit" value="Join!"/>
</form>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set - general part of the header
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Invitation <your_email#example.com>' . "\r\n";
// iterate over all email addresses
foreach ($email_addresses as $address)
{
// specific version of header
$header = $headers . 'To: ' . $address . ' . "\r\n";
// specific version of message
$msg = str_replace('%placeholder%', $address, $message);
// Mail it
mail($address, $subject, $msg, $header);
}
?>
As always, there are many ways to rome, so you could also input a link into the mail which has the email address (or some other sort of ID) as GET parameter.
Take special care with encodings (ISO vs UTF).
Also, your webserver will be the sender of the Email, so it might get caught in spamfilters if the address differs from the FROM field.
Getting PHP's mail() function to work can be a bit tricky, but the internet is full of solutions, so good luck!
You would have to put their address in the href url as a parameter. It is the only way to get it back.
I won't bother putting all the PHP mail code in here but when you pass something like this:
$address = "their.email#gmail.com";
$msg = '... click here ...'
mail($address, $subject, $msg, $header);
It will render in the email like this:
click here
When they click it, it will send the parameter to your webpage, where you can access it with $_GET['email']

Trying to insert linebreaks within php variable

Please bear with me as I'm still very new to this. I'm simply trying to ensure the email message body below has linebreaks where I put <br/>. However when I run the script the message body displays exactly as it is in the script even though it works perfectly in the editor I used to compose this email at Stackoverflow.
I realize its something very small and subtle. /n also didnt seem to work. Thanks for your help! I'm not sure i understand how to use nl2br in my particular context. Again thanks for your patience and help.
$subject=" You're exclusive guide is only a click away!";
$message="Thanks for subscribing .$name! <br/> Click below to confirm your email and email and access your guide <br/> http://acmecorp.net/guide <br/> Acmecorp.net <br />Phone: 800-123-4468";
$headers = 'From:AcmeCorp<info#acmecorp.net>';
mail( $email, $subject, $message,$headers );`
Substitute <br/> with "\n":
$subject=" You're exclusive guide is only a click away!";
$message="Thanks for subscribing .$name! \n Click below to confirm your email and email and access your guide \n http://acmecorp.net/guide \n Acmecorp.net \nPhone: 800-123-4468";
$headers = 'From:AcmeCorp<info#acmecorp.net>';
mail( $email, $subject, $message,$headers );`
You want to use HTML tags in a mail obviously. So, you have to send your mail in HTML, by adding a content type revelant in the headers.
Just before your mail() call, add $headers .= "Content-type: text/html\r\n"; to have something like this:
$subject = " You're exclusive guide is only a click away!";
$message = "Thanks for subscribing .$name! <br/> Click below to confirm your email and email and access your guide <br/> http://acmecorp.net/guide <br/> Acmecorp.net <br />Phone: 800-123-4468";
$headers = 'From:AcmeCorp<info#acmecorp.net>';
$headers .= "Content-type: text/html\r\n";
mail($email, $subject, $message, $headers);

Ajax contact form and UTF-8

i'm using ajax contact form, downloaded from: http://youhack.me/2010/07/22/create-a-fancy-contact-form-with-css-3-and-jquery/
Everything works ok except UTF as i can't use cyrilic symbols when submitting.
The php:
$name = $_POST['name']; // contain name of person
$email = $_POST['email']; // Email address of sender
$web = $_POST['web']; // Your website URL
$body = $_POST['text']; // Your message
$receiver = "receiver#domain.com" ; // hardcorde your email address here - This is the email address that all your feedbacks will be sent to
if (!empty($name) & !empty($email) && !empty($body)) {
$body = "Name: {$name}\n\nSubject: {$web}\n\nMessage: {$body}";
$send = mail($receiver, 'Contact from domain.com', $body, "From: {$email}");
if ($send) {
echo 'true'; //if everything is ok,always return true , else ajax submission won't work
}
}
It uses jquery.validationEngine-en for validation.
My html already has "Content-Type" content="text/html; charset=utf-8" in header.
I'm new to php and jquery, so i would appriciate some guidance to make UTF-8 work when submitting.
Thanks :)
Edit: When i try to use cyrilic chars (čšćđ) on a required field i get ajax input error "Please use letters only". If i submit the form with cyrilic chars on a non-required field, i receive and email, all letters show ok except cyrilic, which are like this: Å¡.
Edit 2: When i set the recipient to gmail (webmail), cyrilic chars show up ok, except in one field, where Ajax doesnt let me use them (regex from Reinder answer).
When i set recipient in outlook (local) and submit the form, none of the cyrilic chars don't show up ok, example: ÄĹĄ oÄa ĹĄ ÄŽŠÄÄ
SOLVED Thanks to Reinder for guide and David! Will solve it today :)
having looked at the plugin you're using, I think this has to do with the validation regex inside jquery.validationEngine-en.js
when the validation is set to 'onlyLetter' it will check using
/^[a-zA-Z\ \']+$/
and none of your characters čšćđ are allowed here...
you need to create a language validation javascript for the language you're using and change that regular expression. For example, have a look at this post
The next thing is to check the encoding of your PHP file and your headers.
Place this at the top of your PHP code
header("Content-type: text/html; charset=utf-8");
Check if the values are correctly displayed when just outputting them in PHP, like so:
echo $name;
If they are correctly displayed in the browser and it's just the email that's incorrectly displaying the characters, then you need to pass an encoding header to the email too
example:
$headers = "From: $name <$email>\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
$body = "Name: {$name}\n\nSubject: {$web}\n\nMessage: {$body}";
$send = mail($receiver, 'Contact from domain.com', $body, $headers);
have a look at the mail function on the PHP.NET website
Rather than use the default PHP mail() function, I've found this come in handy when working with Japanese:
http://bitprison.net/php_mail_utf-8_subject_and_message

Fire an automated email when database updated?

I am trying to figure out the best way to send an automated email to a customer when we update our database with "frames in" i'm thinking javascript & php but don't really know how to implement as a nobo!?
My HTML Form showing checkbox that needs to fire email (only partially shown due to length)
<form action="<?php echo $editFormAction; ?>" name="form" method="POST">
<input name="frame_in" type="checkbox" id="long_tiny" value="yes" <?php if (!(strcmp($row_Recordset1['frame_in'],"yes"))) {echo "checked=\"checked\"";} ?> />
<input type="hidden" name="MM_update" value="form">
</form>
The php mail script (Not sure if completely correct)
$mailTo = $row_customer['email'];
$subject = 'Your Frames Now in!!';
$cName = $row_Recordset2['cName'];
$jobRef = $row_Recordset2['customer_ref'];
$ourRef = $row_Recordset2['our_ref'];
$jobTotal = $row_recordset2['amount'];
mail($mailTo, $subject,
$cName your job Ref: $jobref is now in
<br>
<h2>Details:</h2>
<p>Our ref: $ourRef<br>
Customer Ref: $jobref<br>
Job total: $jobtotal</p>
);
I really don't know how to tie it all together I'm guessing there would need to be error handling as not every customer has an email (But the error doesn't need to be shown)
$mailTo = $row_customer['email'];
$subject = 'Your Frames Now in!!';
$cName = $row_Recordset2['cName'];
$jobRef = $row_Recordset2['customer_ref'];
$ourRef = $row_Recordset2['our_ref'];
$jobTotal = $row_recordset2['amount'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = "$cName your job Ref: $jobref is now in
<br>
<h2>Details:</h2>
<p>Our ref: $ourRef<br>
Customer Ref: $jobref<br>
Job total: $jobtotal</p>";
if($_POST['frame_in'] == "yes") {
mail($mailTo, $subject,$message, $headers);
}
This is a very broad question. What you need to do is:
Process the form in $editFormAction
If $_POST['frame_in'] is set, do the following:
Get all customers
Send an email to the ones with an e-mail address
Publish a warning on your site for logged-in users (or in general...) that do not have an email address registered.
No aditional javascript needed.
Where exactly are you stuck?
You could try a database trigger that sends an email. The database itself would be sending out the e-mail, and would not involve php at all. This would also be more inclusive, as it would send an email regardless or who/what changes the field, including changes from PhpMyAdmin, or other webpages/scripts.
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
http://forums.mysql.com/read.php?99,33635,33635#msg-33635
This may be limited depending on your hosting environment. If you are using shared hosting, odds are you cannot utilize triggers for security reasons.

How to implement a Contact Us form in my HTML site?

I am looking for a means of implementing a "Contact Us" routine/page that I can apply to my site that is purely a HTML website only.
I am assuming this will need to be a PHP process to send emails from a Contact Us form, but I am unsure how to do it.
Since you're pure HTML now, I'm assuming you're trying to keep this simple?
Use a service.
As you said you were interested in looking into PHP, you could do this with one script (say, for example, Contact.php). In your Contact.php file you would put a simple HTML form with room for name, email address, short message, etc. This form can just submit the form data to the same page, process the data, and send out an email with PHP's mail() function. Doing it this way avoids the need to display your email address to the world.
Something simple like the following should get you started, although you are going to want to check any and all user input before mailing it to yourself!
<html>
<body>
<?php
// if the form was filled out and submitted, mail it
if ( isset($_REQUEST['subButton']) )
{
$email = $_REQUEST['email'] ;
$subject = "Contact Us request from site";
$message = $_REQUEST['message'] ;
mail( "secretEmailAddress#email.com", "Subject: $subject", $message, "From: $email" );
header("location: contact.html");
}
else
{
echo "<form method='post' action='Contact.php'>
Email: <input name='email' type='text'/><br/>
Message:<br/>
<textarea name='message' rows='10' cols='30'>
</textarea><br/>
<input type='submit' name='subButton' value='Contact Us'/>
</form>";
}
?>
</body>
</html>
The easiest way is to just use a mailto: link.
Contact Us
Mailto links are very insecure. expect to get spam up the wazoo.
Instead use javascript to simply mask it.
Put this where you want the link to be setup
var mailE1 = "contact";
var mailE2 = "yoursite.com";
var linktext = "Email Us";
document.write("" + linktext + "")

Categories