1

I know this is really common issue, but I have problem with headers. This function send email only when I send mail with $header2. In other cases I get fail echo. Does not working with $headers.

I don't know why it is happening, also it doesn't work with php.net default headers below:

$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

Complete function:

function sendMail() {

$to = "[email protected]";
$subject = "This is subject";

$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

$header2 = "From:[email protected] \r\n";
$header2 = "Cc:[email protected] \r\n";
$header2 .= "MIME-Version: 1.0\r\n";
$header2 .= "Content-type: text/html\r\n";

$retval = mail ($to, $subject, $message, $headers);

if( $retval == true ) {
    echo "Message sent successfully...";
}else {
    echo "Message could not be sent...";
}
}
14
  • look at this very carefully $header2 = "From:[email protected] \r\n"; $header2 = "Cc:[email protected] \r\n"; $header2 .= "MIME-Version: 1.0\r\n"; - what's missing? Hint: It happened to me once... and took me a 1/2 hour "once" just to find what was breaking my code. I said "once". ;-) Commented Mar 3, 2016 at 16:21
  • plus, I don't see where you are using $headers2 in the mail's header Commented Mar 3, 2016 at 16:22
  • ... and with $header2 the From: address will be missing because of a missing . in .= : $header2 = "From:[email protected] \r\n"; $header2 = "Cc:[email protected] \r\n"; - the CC will override the From Commented Mar 3, 2016 at 16:23
  • $retval = mail ($to, $subject, $message, $headers); in this part. In place $headers when I will put $headers2 it will work. I know there($header2) is missing dot, but when Ill put that dot (.=), It is not working again;/ Only in case of $header2 function is correct. Iam frontend, dont wannt to learn next new phpmailers and others, and budget for this one is really small, and I dont wannt to use symfony2. Commented Mar 3, 2016 at 16:25
  • @CD001 that's what I was telling the OP, in so many words ;-) Commented Mar 3, 2016 at 16:25

2 Answers 2

2

Firstly, just get rid of this entire code block since there is no reference to your usage of the $headers2 variable. Do go over my answer in its entirety.

$header2 = "From:[email protected] \r\n";
$header2 = "Cc:[email protected] \r\n";
//       ^ BROKEN concatenate
$header2 .= "MIME-Version: 1.0\r\n";
$header2 .= "Content-type: text/html\r\n";

PHP is going over your entire code, regardless. And it contains a broken concatenate.

However, it's unclear as to what you want to do here and have obviously taken examples from the manual on mail() http://php.net/manual/en/function.mail.php then just blindly adding stuff in there.

Sidenote: The To: is already pre-determined as to where it should be sent to, so the use of To: in the headers isn't required.

function sendMail() {

$to = "[email protected]";
$subject = "This is subject";

$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= "From:[email protected] \r\n";
$headers .= "Cc:[email protected] \r\n";

$retval = mail ($to, $subject, $message, $headers);

if( $retval == true ) {
    echo "Message sent successfully...";
}else {
    echo "Message could not be sent...";
}
}

and if the goal here is to send 2 seperate mails, then you will need to use 2 seperate mail() functions and 2 different header variables.

Something to which you can consult, as per an answer I once gave for another question:


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Sign up to request clarification or add additional context in comments.

4 Comments

Probably worth noting that because the From header is mandatory it's not the broken concatenation in and of itself (no syntax error there) but that fact that it's removing that mandatory header...
@CD001 True. Yet OP's question is a tad unclear. A total rewrite would be the way to go and for them to read up on the function. I included a link to an answer I gave (in regards to, and if) they want to send 2 seperate mails, probably for the person sending the request.
Thank you all for comments and advices! I've decide to use PHPMailer instead.
@UlandNimblehoof You're welcome. I'm glad to hear that, cheers
1

Please stop using the php mail() function! My advice will be to use PHPMailer and sending the emails via SMTP.

Here is a small code snippet using PHPmailer:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtpserver.com';  // Specify main SMTP server. If you dont have one us GMAL or mandrill
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '[email protected]';                 // SMTP username
$mail->Password = 'pass';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
$mail->addAddress('[email protected]');               // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

1 Comment

I wanted to avoid this, but in fact, this way is so much better. I'v done it with PHPmailer with custom php code for include attachments in form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.