so I'm new at this website stuff and im doing at part of my uni course, my tearcher gave me the code.
This in my code for my sendmail.php I tested my contact form on my website and I get the 'Failure!' message that is in the code but that is all I get, nothing else.
when it was working I would get the email but know information that was put in to the contact form and I haven't change the coding. could someone please help me? Thank you
<?php
/*Here you are going to declare the variables*/
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//Save visitor name and entered message into one variable:
$formcontent="VISITOR NAME: $name\\n\\nFEEDBACK: $message";
$recipient = "denisedaykinphotography#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email\\r\\n";
$mailheader .= "Reply-To: $email\\r\\n";
$mailheader .= "MIME-Version: 1.0\\r\\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Failure!");
echo "Thank You!";
?>
Keep your $variables outside of "strings".
meaning:
$name = "Bob";
"hello $name" != "hello " . $name;
It is highly likely that hosting/your computer where the script is put at the moment doesn't support sending mails.
If this is your computer, you could put use to this answer I provided in a similar thread and setting up your own SMTP server (that is the service responsible for sending emails), or using a free one from Internet.
If this is not your computer and you can not really change configs/server configuration, etc., then I suggest you to move the script somewhere else.
The value of variable $mailheader is wrong. Now, an echo($mailheader) displays something like:
From: user#example.org\r\nReply-To: user#example.org\r\nMIME-Version: 1.0\r\n
which is wrong.
The header lines must be separated by the <CR><LF> pair of characters, i.e. the characters having the codes 13 and 10. Because they are not printable, in PHP double-quoted strings these characters are encoded as "\r" and "\n".
The same remark about the content of you message.
Regarding this issue, the code should look like this:
$formcontent = "VISITOR NAME: $name\n\nFEEDBACK: $message";
$recipient = "denisedaykinphotography#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
These answer does not really address your problem of mails not being sent but, before checking the PHP configuration and other mail-related issues, make sure the values you provide to PHP mail() function are correctly formatted (as explained above).
Related
I'm having an issue with a contact form. The email message is correctly sent but in my inbox appears as "From: anonymous#web.godns.net" and not "From: web#mywebsite.com", and goes directly to SPAM.
I've been looking for similar issues but no one gives the specific answer. I guess if the code is having a syntax mistake or it's a server problem.
This is the PHP file:
<?php
// Check for empty fields
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone']) || empty($_POST['message']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
http_response_code(500);
exit();
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = "myemail#gmail.com";
$subject = "Mensaje Web de >> $name";
$body = "Recibiste mensaje a través del formulario en la web.\n"."Estos son los datos:\n\nNombre y Apellido: $name\n\nEmail: $email\n\nTeléfono: $phone\n\nTexto del mensaje:\n$message";
$headers= "From: web#mywebsite.com" . "\r\n" .
"Reply-To: $email \r\n" .
'X-Mailer: PHP/' . phpversion();
if(!mail($to, $subject, $body, $headers))
http_response_code(500);
?>
Does anyone figure out what's wrong here?
Thank you,
Alejandra | aleare.design
It may solve your problem or not but it's always a good idea to set a proper sender address. In good old mail() that needs to be done with the $additional_parameters parameter and the syntax is -f followed by an email address with no display name:
mail($to, $subject, $body, $headers, '-fweb#mywebsite.com')
Additionally, make sure that your local SMTP server allows sending messages in the name of web#mywebsite.com. If it doesn't, perhaps you need to get another server and make use of authentication, something that mail() doesn't allow.
In any case, it's really hard to get email right with this function since you need to do everything by yourself and email protocol is not trivial. For instance, I think your code is vulnerable to email headers injection. It's easier to just use a third-party library like Swift Mailer or PHPMailer.
P.S. What are you trying to accomplish with strip_tags(htmlspecialchars())? This will just make user input unreadable for not obvious gain.
This might help you, I don't get it why you put so many text in the header
// Create the email and send the message
$to = "youremail#yourdomain.com"; // Add your email address inbetween the " " replacing username#yourdomain.com - This is where the form will send a message to.
$subject = "Website Contact Form: $name";
$body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email\n\nPhone: $phone\n\nMessage:\n$message";
$header = "From: noreply#adamar.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$header .= "Reply-To: $email"; // this is to automatically reply to your correspondent email rather than your noreply email, I separate it from your header.
also mind to change the variable $headers to $header? Maybe it might help. Wish all the best of it. Cheers!
Hallo I've got a problem with sending html E-mails with a dyanamic Link via PHP.
That is my PHP Code:
$empfaenger = $_POST['email'];
$test = sha1($_POST['email']);
$betreff = "Test Mail";
$from = "From: Peter <tets#gmail.com>\n";
$from .= "Reply-To: test16#gmail.com\n";
$from .= "Content-Type: text/html\n";
$text = "Please click <p><a href='http://defaultpage/default.php?res=".$test."'>here</a></p>.";
mail($empfaenger, $betreff, $text, $from);
In the mail Log all looks normal like the mail is sended, but I didn't receive one. In PHP error Log also nothing. If I set $test to a specified string it's working as expected, but not with the dyanamic variable $test. I hope that someone can help me becuse I can't get rid of this problem.
Heres my little code
$to = 'target#adress.pl';
$adres='author#adress.pl';
define('ADR','author#adress.pl');
$subject='Invoice delivery confirmation for '.$adres;
$message=$adres.' confirmed invoice delivery';
$headers = "From: ".$adres."\r\n" .
"Reply-To: ".$adres."\r\nContent-Type: text/plain; charset=utf-8" ;
if (mail($to, $subject, $message, $headers)) echo ' Confirmation has been sent to '.$to.'<br />';`
When i use variables with or without concatenation then headers in sending message are empty - they are replaced with default admin adress. Offcourse variable $adress itself is not empty as it works properly in $subject and $message variables. But if i replace variables with ADR constant or plain text then they do work. Is it PHP, server settings or something else?
I'm not php savvy, can someone please provide a good strong code that I can use tailored for my form to prevent injections into my script? I would need to know exactly where to insert this code within my existing code.
My code is below. I left off the opening and ending php tags so all of the code would be visible.
$name=addslashes($_POST['name']);
$email=addslashes($_POST['email']);
$website=addslashes($_POST['website']);
$services=addslashes($_POST['services']);
$comments=addslashes($_POST['message']);
// you can specify which email you want your contact form to be emailed to here
$toemail = "email#domainname.com"; $subject = "Creative Design Consultation Request";
$headers = "MIME-Version: 1.0\n" ."From: \"".$name."\" <".$email.">\n" ."Content-type: text/html; charset=iso-8859-1\n";
$body = "Name: ".$name."\n"
."Email: ".$email."\n"
."Website: ".$website."\n"
."Services: ".$services."\n"
."Comments:\n".$comments;
mail($toemail, $subject, $body, $headers);
echo "Thank you for submitting your request";
This might be what you're looking for:
Sanitize filters php
I have a simple php email script where I wish to include an image at the bottom. When I add the image tags like below the email just shows <img src="http://domain.com/images/logo.png" /> instead of the actual image. Any ideas why?
<?PHP
$email = $_POST["emailaddress"];
$to = "you#youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$headers .= "Content-type: text/html\r\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n
Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: info#domain.com\n";
$usermessage =
"
Thank you for joining our mailing list.
We hope to see you very soon!
Address 1
Address 2
<img src=\"http://domain.com/images/logo.png\" />
";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
$fh = fopen("email.xml", "a");
fwrite($fh, "$email\r\n");
fclose($fh);
?>
You're not passing the Content-Type header with the right message. $headers does contain the right header, but it is sent with a plain text message, whereas $userheaders does not contain the Content-Type header, but the message associated with it does contain some HTML
Replace
$userheaders = "From: info#domain.com\n";
with
$userheaders = "From: info#domain.com\r\n";
$userheaders = "Content-type: text/html\r\n";
and it should work perfectly
This is a word press plugin but if you delete everything BUT the XmailBaby class it should work for you nicely.
That code is a nice piece of work that sends emails very good.
It is just a basic version but it should be enough for you.
Take a look through the code, you might find it interesting.
http://plugins.svn.wordpress.org/xmail-the-right-way/trunk/xmail.php
You need to specify html headers. Rather than do this yourself, you can use a well-established method that supports sending HTML emails, such as PHPMailer:
http://phpmailer.worxware.com/