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.
Related
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);
?>
I'm trying to use noreply to send out emails, but it doesn't work - it wont sent anything. I am using this test-file:
<?php
$to = 'myemailhere';
$subject = 'You received an email message!';
$message = 'This is a message in the email body form.';
$headers = 'From: noreply#example.com' . "\r\n" .
'Reply-To: noreply#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
What could be the problem? By the way, I am using rackspacke, if anyone should know about it.
Thanks in advance!
If you're running on a Rackspace cloud server, you will have to configure the server yourself to be able to send mail. The mail() function relies on the OS configuration to handle emailing. If you are on a Rackspace cloud site, you will likely need to contact Rackspace support for help.
Personally I dodge this bullet altogether by using PEAR's SMTP class. This is a full SMTP implementation in PHP and since it does not rely on any outside configuration or module, it is fully portable. It has saved me a LOT of trouble.
http://pear.php.net/package/Mail
http://pear.php.net/package/Net_SMTP
Note: The PEAR site is having some loading issues atm for me. Give it a minute and it should load.
what is exception? use try catch block. Else try this code
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
my site is hosted on name.com, and to test this I uploaded a simple file called contact.php with this at the top.
<?php
$to = '~~~~';
$subject = 'enquiry from ';
$name = $_POST['name'];
$email - $_POST['email'];
$message = $_POST['message'];
if ($_POST){
mail($to, $subject, $message, $header);
$feedback = "Sent";
}
?>
So, when I click the submit button it sends the mail. Inside a p tag I have echo $feedback, which shows up after I click submit.
The mail does not send? Anything I'm doing wrong here, or do I need to configure my cPanel in some way?
Since header is optional and is left undefined, removing it should resolve your issue.
Be aware that using the $_POST content directly into a email is a security risk for you!
THere are good email libraries that have tools to avoid abuses. (For example: ZendMail, PHPmailer)
Having said that, on your code you are missing the header and have a small mistake on $email = (not -) $_POST['email'];, you can use this:
$header = 'From: from#name.com' . "\r\n" .
'Reply-To: from#name.com' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
You have not set header variable which is optional, there is a typo while setting $email variable, you've use - instead of =
your updated code,
<?php
$to = 'a#a.com';
$subject = 'enquiry from ';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if ($_POST){
mail($to, $subject, $message);
$feedback = "Sent";
}
?>
I'm good with HTML, CSS and not too bad with jQuery but php I'm a total loss! Could I get some help with this Form, what is the best way to add protection into this existing php code I use on my site.
This is the page with the tutorial for all the code http://jorenrapini.com/blog/css/jquery-validation-contact-form-with-modal-slide-in-transition
This is my site REMOVED LINK
This is the php from the tutorial used for the form that I would appreciate some assistance with adding proper protection.
<?php
//declare our variables
$name = $_POST['name'];
$email = $_POST['email'];
$message = nl2br($_POST['message']);
//get todays date
$todayis = date("l, F j, Y, g:i a") ;
//set a title for the message
$subject = "Message from Your Website";
$body = "From $name, \n\n$message";
$headers = 'From: '.$email.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'Content-type: text/html; charset=utf-8' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//put your email address here
mail("youremail#domain.com", $subject, $body, $headers);
?>
<!--Display a thankyou message in the callback -->
<div id="mail_response">
<h3>Thank you <?php echo $name ?>!</h3><br />
<p>I will answer your message soon as possible.</p><br /><br /><br />
<h5>Message sent on: </h5>
<p><?php echo $todayis ?></p>
</div>
------ Okay to clarify this a bit more ------
http://codeutopia.net/blog/2008/10/16/how-to-csrf-protect-all-your-forms/
This is one of the many articles I've been reading over the past two weeks on Cross-Site Request Forgeries so how do you apply or add basic protection similar to this to a form like I am using
mail("youremail#domain.com", $subject, $body, $headers);
can send you the mail, obviously, but you need to create a message to check if the mail function was not successful.
So,
if(mail("youremail#domain.com", $subject, $body, $headers)){
echo 'Your email is sent';
}else{
echo 'email is not sent';
}
The above code, will guarantee, that if there was a problem with mail() the user would see a costume-built message than the actual error exposing your code and directory
You have to filter the postvalues against xss. For txt-mails u should strip out all tags in a professional way. Dont use only strip_tags()! For your header data strip out all whitespaces too.
You should take a look at the ESAPI project, while the PHP version is not suitable for production use (based on what they write), you can still get an understanding of what input validation and security means for your service.
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