PHP mail() not sending a mail [duplicate] - php

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";

Related

How can I validate email in this PHP contact form? [duplicate]

This question already has answers here:
How to validate an email address in PHP
(15 answers)
Closed 3 years ago.
is there a way to validate an email in this form without rewriting it completely? It'd be great if I could just add something to this. I'm new to PHP and this form is taken from mmtuts video. If you know how to help, I'd be glad if you did.
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$number = $_POST['number'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "misavoz#seznam.cz";
$headers = "From: ".$mailFrom;
$txt = "Pan/í ".$name." vám poslal/a zprávu. Telefonní číslo: ".$number."\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.php?mailsend");
}
In the past I have used:
if(filter_var($email, FILTER_VALIDATE_EMAIL))
{
//send email
}
else
{
//show error
}
Source: https://www.php.net/manual/en/function.filter-var.php

PHP mail generator - wrong message structure [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I have a problem with php mail generator. I don`t understand my code put $visitor_email in message... Can you help me?
id = $_POST['ID'];
$tt = $_POST['TT'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($id)||empty($tt)||empty($visitor_email))
{
echo "ID, TT and email are required!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Wrong email!";
exit;
}
$email_from = 'xxx#mymail.com';//<== update the email address
$email_subject = "Something in title";
$email_body = "$message".
$to = "$visitor_email";//<== update the email address
$headers = "From: $email_from \r\n";
//Send the email!
mail($to, $email_subject, $email_body, $headers);
//done. redirect to thank-you page.
header('Location: end-page.html');
Replace . after the $message with ;
. will concatenate the variables.
$email_body = $message;

php email function not working with .org email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
So I am trying to send an email to notify the office admin of when an email has been submitted through a form. Everything worked with no problems when I used an email ending in ".com" However, when I tried using an email ending in ".org"
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$message = $_POST['message'];
if(isset($_POST['submit'])){
$to = "officeadmin#example.org"; // Office Admin Email
$from = $email_address; // User's Email
$subject = "Form Submission";
$content_admin = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $message;
$content_user = "Thanks again for contacting us. A dedicated staff member will contact you as soon as possible. Your message was: \n\n" . $message;
$headers_admin = "From:" . $from;
$headers_user = "From: Example Office";
mail($to,$subject,$content_admin,$headers_admin); //Notification email for admin
mail($from,'Thanks for contacting us',$content_user, $headers_user); //Copy of email sent to user
header('Location: contact_form_success.html'); //Redirect here after submit is clicked
}
Basic mail() function is not well configured so sometimes problem may arise, so you
Try PHPMailer, it is well configured php mail class for sending mail.

PHP Sending Email Comma error [duplicate]

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)

i cannot sending email by using this code [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I tried below code but it was not working correctly. please help me
<?php
$email = $_POST['email'];
$feedback = $_POST['feedback'];
$to = "balvant1#gmail.com";
$subject = "Mail from HRMSsystem for New Requirement";
$message .= "<b>Email : $email </b>";
$message .= "<b>feedback : $feedback </b>";
$header = "From:xyz#info.com \r\n";
$header = "Cc:balvant#gmail.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail($to,$subject,$message,$hearder);
if( $retval == true )
{echo "Message sent successfully...";}
else{ echo "Message could not be sent..."; }
?>
Thanks in advance.
Three things...
Most hosting companies don't allow you to "bounce" mail off of their servers so unless you own the domain info.com AND you are hosting it on their server, chances are your mail is being blocked.
As #Louis Loudog Trottier said, $retval = mail($to,$subject,$message,$hearder); should be $header not $hearder
You don't necessarily have to have "xyz#info.com" set up. I often use noreply#my-domain-name.tld (which is a non-existent email address) to send mail, BUT you do have to comply with #1.
Also, see the comment by #Svengali as well
Although not a direct answer to your question, using mail() to send your emails is seldom a good idea. Using an email class library is simpler and more straight forward in my opinion.
Please have a look at swiftmailer.org for example.

Categories