This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am having trouble with sending emails through PHP.
So I have a form which submits to a PHP script which sends an email:
<?php
$to = "myemail#email.com";
$subject = "[Contact Form]";
$name = $_POST["name"];
$contactNumber = $_POST["contactNumber"];
$email = $_POST["email"] ;
$message = $_POST["message"];
$body = "Someone has sent a new message from the contact form. \n \n Message from: " . $name . "\n Contact Number: ". $contactNumber ."\n Email: ". $email ."\n \n Message: ". $message;
if (mail($to, $subject, $body)) {
echo ("<p>Email successfully sent!</p>");
} else {
echo ("<p>Email delivery failed…</p>");
}
?>
And the email is sent fine when for example the message is one line such as:
"Hi there how is it going?"
And fine if it is multiple lines such as
"Hi there
how is it going?"
But when I try and type a message with a comma such as
Hello there,
how is it going?
It fails?
Is there a way I can just treat the whole thing as a string possibly? Would this also fail on any other characters or is this issue just because of the way I am writing the PHP script?
This might be an obvious fix but I am new to PHP so apologies! I have tried looking around for an answer but nothing seems to fix what I am looking for.
Thanks for any help!
Try using headers in your mail function, like this:
$headers = 'MIME-Version: 1.0\r\n';
$headers .= 'Content-type: text/html; charset=UTF-8\r\n';
mail($to, $subject, $body, $headers)
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have a php script that sends an email from an HTML form. The issue is that the sender shows as CGI-Mailer in my inbox.
How can I set the sender address to be that of the sender and not CGI-Mailer?
<?php session_start();
if(isset($_POST['Submit'])) {
$youremail = 'info#complexny.com';
$fromsubject = $_POST['fname'];
$subject = $_POST['fname'];
$fname = $_POST['fname'];
$url = $_POST['url'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$headers = "From: $mail \n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$message = $_POST['message'];
$to = $youremail;
$subject = ''.$fromsubject. ' is interested in a project with you.';
$body = '
Client: '.$fname.'
Phone Number: '.$phone.'
URL: '.$url.'
E-mail: '.$mail.'
Message:
'.$message.'
';
echo "<p style='text-align:center'>Thank you for your feedback. We will be in contact shortly.<br/>Continue to <a href='/'>The Company/a></p>";
mail($to, $subject, $body);
} else {
echo "You must write a message. </br> Please go to <a href='/contact.php'>Contact Page</a>";
}
?>
You are not passing the additional_headers parameter to the mail function. Change the line with the call to mail to:
mail($to, $subject, $body, $headers);
I would suggest using PHPMailer rather than mail(). You can see how to do what you're after in the answer to this question: Setting replyTo field in email
Quoting from that question:
$this->phpmailer->AddReplyTo($replyEmail,$fromName); //this is email2#example.com
$this->phpmailer->SetFrom($fromEmail, $fromName); //this is email1#example.com
You can find more info on PHPMailer here: https://github.com/PHPMailer/PHPMailer
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm attempting to run an email form, which takes text from the user's input forms and send them to my desired email (seen as $myemail in the php below).
<form method="post" name="contact_form" action="Contact.php">
Your Name: <input type="text" required name="name">
Email Address: <input type="text" required name="email">
Message: <textarea required name="message"></textarea>
<input type="submit" value="Submit">
</form>
<?php
$errors = '';
$myemail = 'enterDesiredEmail';
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
$errors .= "\n Error: all fields are required";
}
$name = null;
$email_address = null;
$message = null;
$name = $_POST["name"];
$email_address = $_POST["email"];
$message = $_POST["message"];
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",$email_address)) {
$errors .= "\n Error: Invalid email address";
}
if( empty($errors)) {
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
}
?>
I spat the above code out onto a webpage and uploaded the file to my server. Upon accessing the webpage online, filling in content to the form, and clicking sbmit, the page refreshed with the inputs and textarea clear as expected. When I checked the email I had set, no message was received. What could I do to correct this code so that the desired email address actually receives a message?
Many thanks.
Some pointers that may well solve your issue without specifically telling you what the cause of this issue actually is:
1) Use Error Logging.
2) Check that you have a valid mail server setup on your server. As stated in comments by Jason K.
3) Use a mailing library such as PHPMailer. It sidesteps a huge amount of the headache.
4) Check your emails are valid with the correct code, principly using filter_var rather than obtuse regexes.
Also check emails are Sanitized (FILTER_SANITIZE_EMAIL) as well.
5) If not using or setting up a library such as PHPMailer or SwiftMailer then you need to check your mail headers are exactly correct.
BONUS
There seems to be some conflicting accounts of what to use for email line endings [including headers], but I would suggest PHP_EOL or \r\n (the same thing on Linux servers). \n is not suitable. As stated in comments by Barmar.
Good luck
You should try to use a library such as PHPMailer. Themail() function is sometimes not flexible enough to achieve what you need in most cases. Also the mail() function requires a local mail server. In addition, PHPMailer offers a lot of addons such as the ability to set up attachments or the ability to send HTML emails to your users. Using a library like this also means that you emails will be sent out almost all the time since it it tested and used by a lot of people.
You can find a tutorial for using PHPMailer here: https://www.sitepoint.com/sending-emails-php-phpmailer/
Example code taken from the website using PHPMailer:
<?php
require_once "vendor/autoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "from#yourdomain.com";
$mail->FromName = "Full Name";
//To address and name
$mail->addAddress("recepient1#example.com", "Recepient Name");
$mail->addAddress("recepient1#example.com"); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo("reply#yourdomain.com", "Reply");
//CC and BCC
$mail->addCC("cc#example.com");
$mail->addBCC("bcc#example.com");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
You need to interpolate the actual $email variable inside the double quote from :
$headers = "From: $myemail\n"; to
$headers = "From: ${myemail}"."\r\n";
If you Specify additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n).
This is an example from php doc:
`<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Happy coding!
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
Ive been trying this out the whole day but I cant figure out how to send an email from my html contact form containing the information from the form to my email address. Im new to php.
Ive tried running this by uploading it to free web hosting. I get the message "success!" when I press the submit button on my html form but no email is actually sent.
Any help is appreciated.
PHP script:
<?php
//Subject
$subject ="Contact Form Submission";
// Name
$name =$_POST['InputName'];
// Message
$message =$_POST['InputMessage'];
//Mail of Sender
$email =$_POST['InputEmail'];
//From
$header = "From:$name<$email>";
$send_contact=mail("myemail#gmail.com",$subject,$message,$header);
//Check if mail was sent
if($send_contact){
echo "Success!";
}
else {
echo "Error!";
}
?>
EDIT: Figured it out after one whole day of trial and error. The problem was with the free web host I was using. Changed hosts and the code started working fine. Hope this helps someone in the future. Thanks all for the help.
I have a pretty good idea why your code is not working. It happened to me a long time ago. The reason why your code is not working is because :
When you pass "from" in headers, php expects an existing email account of your
server. For example : $headers = 'From: emailacc#yourserver.com';
So first thing you gotta do is create an email account on your server. And then put the From in header to the email address that you've just created.
The From field in the $headers is not the From as you think.
<?php
$email = $_POST["InputEmail"];
$subject = $_POST["InputSubject"];
$message = "From: ".$email.", ".$_POST["InputMessage"]; // you put the email address from the input form here
$headers = 'From: emailacc#yourserver.com'; // here is the email address specified from which u want to send the email.(i.e. your server email address)
mail($to, $subject, $message, $headers)
?>
I'm sure this will do the job :)
Have a shot at this.
I changed you're $header variable around a little bit, so that rather than setting the email as "$email", It'll actually pass through the posted email entered in the form. This apply's to the name too.
I also made it so that you pass the mail function through the parameters of the if statement, rather than setting a new variable.
$headers = "From: " . $name . "<" . $email . ">"; // notice new concatenation
if(mail("myemail#gmail.com", "Contact Form Submission", $message, $headers)){
// success message
} else {
// error message
}
Really hope this helps! :)
Try adding spaces after the "=" that might be the problem,
If that doesn't work you could try to use this
<?php
$emailvariable = $_POST['InputEmail']
$to = 'example#gmail.com';
$subject = "Form"
$message = $_POST['InputMessage'];
$headers = "From: $emailvariable";
mail($to, $subject, $message, $headers);
?>
Hope this helps
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I'm trying to learn a bit of basics of PHP, but as always I start from living examples like mailing. I'm not sure my tutorial was prepared good or I just made a couple of silly mistakes. There may be a problem with " ' because I haven't figured out which one should be where :) So, my mail is not sending an email, and definitely it's not going to spam so I believe I screw up ' and " part. Thanks for help!
$name = $_POST['nick'];
$visitor_email = $_POST['email'];
$visitor_tel = $_POST['tel'];
$message = $_POST['msg'];
$email_from = 'mail#mail.pl';
$email_subject = "Nowe zlecenie: ";
$email_body = "Nowe zlecenie od $name.\n".
"Email kontaktowy: $visitor_email".
"Telefon kontaktowy: $visitor_tel".
"Zlecenie: $message".
$to = 'mail#mail.pl';
$headers = "Od: $email_from \r\n";
mail($to,$email_subject,$email_body,$headers);
For debugging do like below:
if(mail($to,$email_subject,$email_body,$headers))
echo "mail sent";
else
echo "not send";
I have a website I'm working on with a contact form. Simple Name, Email, and Message fields. However, I'm not receiving the email from the form; though I do get the "Message Sent" success option.
I had found a way to test wp_mail to see if that was it, using https://gist.github.com/butlerblog/5c9b805529c419b81447#file-test_wp_mail-php and that works just fine - so I have no clue what the problem is.
<?php
//loading wordpress functions
require( '../../../wp-load.php' );
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
$to = get_option('admin_email'); //Enter your e-mail here.
$subject = get_theme_option(tk_theme_name.'_contact_contact_subject');
$from = $_POST['contactname'];
$name = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $name <$from>\n";
$headers .= "Reply-To: $subject <$from>\n";
$return = $_POST['returnurl'];
$sitename =get_bloginfo('name');
$body = "You received e-mail from ".$from." [".$name."] "." using ".$sitename."\n\n\n".$message;
$send = wp_mail($to, $subject, $body, $headers) ;
if($send){
wp_redirect($return.'?sent=success');
}else{
wp_redirect($return.'?sent=error');
}
?>
As I said, it does hit
wp_redirect($return.'?sent=success')
so I have no clue what is wrong.
EDIT:
Did a variable dump
To: uriah.h.brown#gmail.com
Subject: E-Mail from HollyBrown.Net
From: TestName
Name: test#yahoo.com
Message: TestMessage
Headers: From: test#yahoo.com Reply-To: E-Mail from HollyBrown.Net
Return: http://www.hollybrown.net/contact/
Sitename: The Artwork of Holly Brown
Body: You received e-mail from TestName [test#yahoo.com] using The Artwork of Holly Brown TestMessage
Send: 1
Warning: Cannot modify header information - headers already sent by (output started at /home/panoramicpanda/hollybrown.net/wp-content/themes/widely/sendcontact.php:21) in /home/panoramicpanda/hollybrown.net/wp-includes/pluggable.php on line 1178
The header already sent error leads me to believe output has already been started before you try and send the headers. In this case, the redirect won't work. Try this instead:
/loading wordpress functions
require( '../../../wp-load.php' );
ob_start(); // Starts data buffer.
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
$to = get_option('admin_email'); //Enter your e-mail here.
$subject = get_theme_option(tk_theme_name.'_contact_contact_subject');
$from = $_POST['contactname'];
$name = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $name <$from>\n";
$headers .= "Reply-To: $subject <$from>\n";
$return = $_POST['returnurl'];
$sitename =get_bloginfo('name');
$body = "You received e-mail from ".$from." [".$name."] "." using ".$sitename."\n\n\n".$message;
$send = wp_mail($to, $subject, $body, $headers) ;
ob_end_clean();// Purges data in buffer.
if($send){
wp_redirect($return.'?sent=success');
}else{
wp_redirect($return.'?sent=error');
}
Notice the ob_start and ob_end_clean. This will throw your output into the "buffer" and output it clean. The reason this is a problem, is because if any markup is sent BEFORE your sendmail (ie: html generated through templates etc, then the headers have already been started. At that point, unless your server configuration specifically allows you to modify already sent headers, then you will get the error you are getting. Using output buffering allows you to send the data as you intend.