Sending email does not work - php

Hi I am trying to send an email after a user enter some contact data but it seems that the email does not get sent as expected.I do not work with php very often so I might have missed something.Here is my code:
function sendEmail($data){
$to = 'alexandru.nistor.89#gmail.com';
$subject = 'From My Portfolio Website!';
$message = $data[3]['value'];
$headers = 'From: ' . $data[0]['value'] . " " . $data[1]['value'] . "\r\n" .
' Reply-To: '. $data[2]['value'] . "\r\n";
mail($to, $subject, $message, $headers);
}
Now before you ask the function gets called I have checked with the debugger and the data is added corectly to the variables the only problems is that the mail does not arive to the destination.Am I doing somethign wrong?

Check what function mail() returns.
If returns false it means it's error in php. If it returns true it means it is accepted for delivery but not necesery will reach its destination. In this case it's more network related.
More info at docs: http://php.net/manual/en/function.mail.php

Related

How to order the PHP output in an email?

I've created a web site and I'm using this link for a JS pop up form to be emailed using PHP.
I also used the code from here and everything works except for a couple things. When I don't remove some variables, the order of information is out of place when it emails.
And when I keep all the variables, I get the following error in a log and nothing sends until I remove them:
PHP Warning: mail() expects at most 5 parameters, 7 given in xxxxxxxxxxxxxxxxxxxxx/quote.php on line 41
Below is the code where the error is coming from:
else{
$Company = $_POST['company'];
$Email = $_POST['vemail'];
$Name = $_POST['name'];
$Number = $_POST['number'];
$Info = $_POST['info'];
$headers = 'From:'. $email2 . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email2 . "\r\n"; // Carbon copy to Sender
// Message lines should not exceed 70 characters (PHP rule), so wrap it
// $message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
mail("info#bvcdenver.com", $Company, $Email, $Name, $Number, $Info, $headers);
echo "Your quote request has been sent successfuly ! Thank you for your interst. You are being redirected back to xxxxxxxxxxxx in 5 seconds.";
}
How can I send all the variable? Order won't matter if I can get them all to send.
Note: I don't have a lot of scripting experience. This site is created using only HTML/CSS and these PHP and JS sections. So ideally I'd like to not change the entire site.
You need to proper use the php mail function() as it is stated here http://php.net/manual/en/function.mail.php.
The max number of parameters is 5:
- TO ( in your case: "info#bvcdenver.com" )
- SUBJECT ( in your case: $Company )
- MESSAGE ( in your case: $Email )
the last 2 are additional headers and additional parameters.
If you want to send all the data "email, name, number and info" you should organize a string/text variable and put it on the 3rd place
like:
$message = $Email . " - " . $Name . " - " . $Number . " - " . $Info;
mail("info#bvcdenver.com", $company, $message, $headers);
This should do the work and you can customize the message numbers how you want, with html or raw new lines, and get a proper template.
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';//pass every variable into message
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

Send Mail from MODx Template

I'm trying to send an email from a MODx template, either just using PHPmail or with MODx's ModMail class. Needless to say neither way is working.
I'm writing the code in a MODx snippet, and including that snippet in my template. When using PHPmail, and with the form action omitted (so that the form submits to the current URL), the page refreshes but no mail is sent.
When I try to use ModMail, nothing happens at all. But I'm not quite sure how to actually call the send mail code in this case, so the code is just sitting there doing nothing.
This is my PHPmail attempt:
<?php
$to = $_POST['email'];
$name = $_POST['name'];
$query = $_POST['message'];
$subject = "Query from " . $name;
$message = "You're received a query from " . $name . ", their email address is " . $to . ".\r\nThey said:\r\n" . $query;
$headers = 'From: MyPersonalEmail#gmail.com' . "\r\n" .
'Reply-To: MyPersonalEmail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
echo $to;
echo $name;
echo $query;
echo $subject;
echo $message;
echo $headers;
mail($to, $subject, $message, $headers);
?>
And this is with ModMail:
<?php
$message = $_POST['message'];
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,'MyPersonalEmail#gmail.com');
$modx->mail->set(modMail::MAIL_FROM_NAME,'Johnny Tester');
$modx->mail->set(modMail::MAIL_SUBJECT,'Check out my new email template!');
$modx->mail->address('to','MyPersonalEmail#gmail.com');
$modx->mail->address('reply-to','MyPersonalEmail#gmail.com');
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
There is a MODX extra available called QuickEmail, that could check the internal mail functionality.
I do all the email handling via the MODX extra FormIt. Look at the rtm for it, it is quite easy to get running. It can handle a lot of the stuff you want to do and prevent (like spam, multisubmit) when having a form.
https://docs.modx.com/extras/revo/formit/formit.tutorials-and-examples/formit.examples.simple-contact-page
Don't try and invent a new solution. Most stuf can be done by using or extending existant MODX extras.

PHP mail() issue

I have a php page that includes an inquiry form that refers to itself as the form action.
Once completed the form writes to a database within a try-catch construct. I want to send an email to the administrator to say that someone has added themselves to the database.
All of the code works until:
if(mail($to, $subject, $message, $headers, '-f' . $from))
{ #And finally send them a thanks
header('Location: thanks.html.php');
exit();
} else {
echo 'Email did not send';
exit();
}
The code above this block all works because I get a write into the database, and the 'if' test passes because the redirect to the thanks page also works! What have I missed?
Seems like you are trying to make use of additional parameters.
Exerpt from PHP Manual
The additional_parameters parameter can be used to pass an additional
parameter to the program configured to use when sending mail using the
sendmail_path.
<?php
mail('nobody#example.com', 'the subject', 'the message', null, '-fwebmaster#example.com');
?>
Route : 2
Try something like this.
Your $from is contained in the $headers variable itself , so you don't have to specify.
<?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();
if(mail($to, $subject, $message, $headers))
{ #And finally send them a thanks
header('Location: thanks.html.php');
exit();
} else {
echo 'Email did not send';
exit();
}
?>
AT last...The answer. Nothing wrong with the code! I was falling over the web hosts anti spam filter in mail. Because I was testing the script with my own email address, it refused to send the mail assuming it was spam! As soon as I put in a completely new email address it sent the email!
Thanks to everyone that offered help!

How do I include the submitters email in a PHP form?

I am using a simple HTML form for an event registration and I would like to send a PHP confirmation email to the registrant with the same information that is going to the administrator.
Here are the details:
$emailFromName = $_POST['name'];
$emailFrom = $_POST['email'];
$emailFromPhone = $_POST['phone'];
I would like to use the value from $emailFrom in the following email address to:
$emailTo = "name#domain.com", \"$emailFrom\";
edit: Added new configuration:
$emailHeaders = 'From: "Conference" <noreply#conference.org>' . 'CC: . $emailFrom .';
This obviously doesn't work, but you get the idea of what I am trying to.
Thanks!
You will need to add headers to the mail function...
for example:
$headers = 'From: Family History Conference <noreply#familyhistoryconference.org>' . "\r\n" .
'Reply-To: noreply#familyhistoryconference.org' . "\r\n" .
'CC: ' . $emailFrom . "\r\n";
and then
mail($to, $subject, $message, $headers);
References:
PHP Mail Function
You put "From: $name <$emailFrom>" in the additional parameters of the mail() function:
http://php.net/manual/en/function.mail.php
So, basically you want to CC a copy of the message to the user as well as the administrator?
It depends on which PHP library you're using to send it. If you're using the default PHP mail() function, you'll need to add additional headers:
// Additional headers
$headers .= 'Cc: ' . $emailFrom . "\r\n";
// Mail it
mail('admin#email.com', $subject, $message, $headers);

PHP mail() function does not work on web-host

I'm having issues sending emails using the php mail() function. I know the php script I have works because I have an identical copy of it on another web-hosting company and it works there.
I think it has to do with the web-hosting company itself. Do any of you know what I need to do in order to make it work? Is there something I need to tell them to install? I think they're running on Apache.
Thanks,
Amit
For clarification purposes, here is the mail-script.
<?php
$to = 'my#email.com';
$subject = 'Contact from your website';
$message =
'Below are details from the Contact Us Form ' . "\n\n" .
'Name: ' . $_REQUEST['name'] . "\n\n" .
'Telephone Number: ' . $_REQUEST['phone'] . "\n\n" .
'E-mail address: ' . $_REQUEST['email'] . "\n\n" .
'Comments: ' . $_REQUEST['comments'];
$email = $_REQUEST['email'];
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=ISO-8859-1";
//SPAM CHECK
$str = $_REQUEST['spam'];
$strE = $_REQUEST['email'];
if( $str != "10" || $strE == "")
{
echo "<div align='center' style='color:red'>One or more of the form fields were incorrect, you will be redirected to the contact page within 3 seconds.</div>";
?><meta http-equiv="refresh" content="3;URL=http://engineercreativity.com/samples/biz/contact"><!-- EDIT THIS -->
<?php
} else {
mail ($to, $subject, $message, $headers);
?>
<meta http-equiv="refresh" content="0;URL=http://engineercreativity.com/thankyou.html"> <!-- EDIT THIS AS WELL -->
<!--
<div class="text" align="center" style="text-align: center; color: green;">
<br/>
Thank you for contacting us!
<br/>
The message was succesfully sent!
</div>
-->
<?php
}
?>
If it is a dedicated server, make sure you have postFix Mail installed (http://www.postfix.org/)
I faced this error today itself as the SMTP server was not available (i assumed it as there by default, but not)
Are you performing any kind of checks on the mail function? It should return true if it's executing successfully - knowing that would help us cut down on other possible reasons you may not be receiving the mail, such as filters, server or smtp configuration etc. Doing something like:
if (mail($to, $subject, $body, $header)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
Should give you a better idea, and should die outright if the function does not exist for some reason. Php's mail function is incredibly finicky on free web hosts, since it's commonly abused for spam purposes.
Posting full headers also can help legitimate messages pass spam tests.
$headers = "Return-path: <sendingemail#test.com>\n";
$headers .= "Reply-to: <sendingemail#test.com>"."\n";
$headers .= "Content-Type: text/html; charset=windows-1252\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$headers .= "From: <sendingemail#test.com>\n";
$headers .= "X-Priority: 3\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Organization: My Organization\r\n";
$headers .= "\n\n";
Write a really simple script, like
<?php
mail('your_mail#example.com', 'test subject', 'test msg') or die('no mail()');
echo 'mail sent.';
Execute it, and make sure the mail is not caught by your spam filter (if you can afford it, set up your own domain/DNS server, netcat -l -p 25 is sufficient).
If that doesn't work, contact the support of your web hoster. Do they have an FAQ or any other documentation?
Whatever the solution, check your mail()'s output.
Most common solution
Ask your hosting company if your current web host has SMTP set up to relay mail from your scripts. If they say "no", then they might have another SMTP host for you to use like smtp.example.com, or you'll have to use another SMTP relay (check with your current e-mail provider).
Alternative
The SMTP server you're talking to might not understand what your script is saying. I've seen situations before where my mail script will work with Postfix but not qmail. This is easily solved by using a third party e-mail library: there are tons out there, but my favorite is Flourish's (http://flourishlib.com/docs/fEmail).
mail() function of php, will send your mail to junk only. Instead use SMTP php mailer function.
Why we should use SMTP instead PHP mail():
SMTP log in to an actual account on a mailserver and send the mail through SMTP to another mail server. If the mail server is configured correctly, your mails are sent from an actual account on a mailserver and will not wind up flagged as spam.
Mail sent with the mail() function is sent with sendmail in most cases. There is no authentication going on and it will almost always be flagged as spam if you use the "From:" in the extra headers.
This is because if you take a look at an original email file in say, gmail, you will see the headers that are sent. You are actually sending from user#serverhostname.tld and not someone#example.com like you had told the mail function to do.
If you use SMTP and view the original the email is actually sent from someone#example.com
You can download SMTP class from:
https://code.google.com/a/apache-extras.org/p/phpmailer/source/browse/trunk/class.smtp.php?r=170
http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html

Categories