PHP mail not working

Hello all,

I’m trying to send an email from a contact form but I can’t get it to work properly.

Firstly, the line breaks aren’t showing up on the email when I get it and secondly I can’t set additional headers to set the from address, if I try to set them the email doesn’t send.

This is my code

[php]
if (!empty($_POST[‘name’]) && !empty($_POST[‘email’]) && !empty($_POST[‘message’])) {
$name = trim($_POST[‘name’]);
$company = trim($_POST[‘company’]);
$phone = trim($_POST[‘phone’]);
$email = trim($_POST[‘email’]);
$event_value = trim($_POST[‘event-calendar’]);
$event_type = trim($_POST[‘event-type’]);
$message = trim($_POST[‘message’]);
} else {
$error_message = ‘Please fill in all required fields’;
return $error_message;
}

$email_content = 'From: ’ . $name . ‘\n\n’.
'Company: ’ . $company . ‘\n\n’.
'Phone: ’ . $phone . ‘\n\n’.
'Email: ’ . $email . ‘\n\n’.
'Event info: ’ . $event_value . ’ ’ . $event_type . ‘\n\n’.
'Message: ’ . $message;

if (!mail(‘[email protected]’,‘test message’,$email_content)) {
$error_message = ‘Error sending email’;
return $error_message;
} else {
header("Location: " . BASE_URL . “contact?success=true”);
exit;
}
[/php]

Now it works if I don’t put additional headers on the email, but the line breaks aren’t working and I need to set the From header otherwise it just shows up as the server name or whatever it is. If I put 'From: ’ . $email after the $email_content variable then it just comes up with ‘Error sending email’ when I try to send it.

Any advice?

Thanks,
Adam

PHPMailer works better for this, if you aren’t going to use a service.

How are you trying to add the additional headers? It does make a difference.

Newline characters need double quotes to be escaped, single treats them as a literal \n text.

I’m basically adding the From header like this

[php]
//$email can be a different value depending on earlier logic
$from = 'From: ’ . $email;

if (!mail(‘[email protected]’,‘test’,$email_content,$from)) {
$error_message = “Error sending email”;
return $error_message;
} else {
//mail sent successfully
}
[/php]

Add this to the end… I will say that you are susceptible to header injection with you current code, though. You aren’t validating anything and are just sending things as is, that leads to trouble.

[php]$from = “From: $email \r\n”;[/php]

I got the line breaks working but I still get an error when sending the email with the from header?

[php]
$headers = “From: [email protected]\r\n”;
if (!mail(‘[email protected]’,‘test’,$email_content,$headers) {
//do something
}
[/php]

I changed the variable name but that’s about it, surely it can’t be because of that?

Any ideas?

Try this version and see if the issues are still there:

[php]

<?php $headers = array(); $headers[] = "MIME-Version: 1.0"; $headers[] = "Content-type: text/plain; charset=iso-8859-1"; $headers[] = "From: Sender Name "; $headers[] = "Bcc: Recipient Name "; $headers[] = "Reply-To: Recipient Name "; $headers[] = "Subject: {$subject}"; $headers[] = "X-Mailer: PHP/".phpversion(); mail($to, $subject, $email, implode("\r\n", $headers));[/php] [url=http://php.net/manual/en/function.mail.php]Copied from PHP Docs: mail()[/url]
Sponsor our Newsletter | Privacy Policy | Terms of Service