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: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

Complete function:

function sendMail() {

$to = "mymail@gmail.com";
$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 <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

$header2 = "From:abc@somedomain.com \r\n";
$header2 = "Cc:afgh@somedomain.com \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...";
}
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Uland Nimblehoof
  • 862
  • 17
  • 38
  • look at this very carefully `$header2 = "From:abc@somedomain.com \r\n"; $header2 = "Cc:afgh@somedomain.com \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"*. ;-) – Funk Forty Niner Mar 03 '16 at 16:21
  • plus, I don't see where you are using `$headers2` in the mail's header – Funk Forty Niner Mar 03 '16 at 16:22
  • ... and with `$header2` the `From:` address will be missing because of a missing `.` in `.=` : `$header2 = "From:abc@somedomain.com \r\n"; $header2 = "Cc:afgh@somedomain.com \r\n";` - the CC will override the From – CD001 Mar 03 '16 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. – Uland Nimblehoof Mar 03 '16 at 16:25
  • @CD001 that's what I was telling the OP, *in so many words* ;-) – Funk Forty Niner Mar 03 '16 at 16:25
  • @Fred-ii- oh, thought you were referring to the whitespace after the email addresses ;) – CD001 Mar 03 '16 at 16:27
  • again: where are you intending to use `$headers2` in `mail()`? @UlandNimblehoof I almost submitted an answer here. Good thing I didn't press on the "Post your answer" button. – Funk Forty Niner Mar 03 '16 at 16:27
  • @UlandNimblehoof if you're wanting to send 2 seperate headers, you will need to use 2x seperate `mail()`'s. – Funk Forty Niner Mar 03 '16 at 16:29
  • Guys, some misunderstanding, I want use $headers, not $header2;) – Uland Nimblehoof Mar 03 '16 at 16:29
  • @UlandNimblehoof so get rid of all declarations of `$header2`. PHP is going over your entire code, regardless and finds a broken chain here. – Funk Forty Niner Mar 03 '16 at 16:30
  • I'm not too familiar with mail headers but is it okay to have a `"To"` header even though `$to` is specified ? – WheatBeak Mar 03 '16 at 16:31
  • @WheatBeak http://php.net/manual/en/function.mail.php `$headers .= 'To: Mary , Kelly ' . "\r\n";` yes, it's valid. Edit: see last comment below. – Funk Forty Niner Mar 03 '16 at 16:32
  • @WheatBeak beat me to it... but yeah, that's effectively setting the `To:` header twice... thing is though, they're two completely different email addresses - so while it may be valid, it may not be what's expected – CD001 Mar 03 '16 at 16:32
  • oh..ok `$to = 'aidan@example.com' . ', '; // note the comma $to .= 'wez@example.com';` yeah he needs to concatenate if he wants to do that. OP took that example from the manual but didn't use it correctly. – Funk Forty Niner Mar 03 '16 at 16:32

2 Answers2

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:abc@somedomain.com \r\n";
$header2 = "Cc:afgh@somedomain.com \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 = "mymail@gmail.com";
$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:abc@somedomain.com \r\n";
$headers .= "Cc:afgh@somedomain.com \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.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    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 Mar 03 '16 at 16:44
  • 1
    @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. – Funk Forty Niner Mar 03 '16 at 16:46
  • Thank you all for comments and advices! I've decide to use PHPMailer instead. – Uland Nimblehoof Mar 04 '16 at 14:43
  • @UlandNimblehoof You're welcome. I'm glad to hear that, *cheers* – Funk Forty Niner Mar 04 '16 at 14:45
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 = 'user@youremail.com';                 // 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('from@youremail.com', 'Mailer');
$mail->addAddress('joe@youremail.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@youremail.com');               // Name is optional
$mail->addReplyTo('info@youremail.com', 'Information');
$mail->addCC('cc@youremail.com');
$mail->addBCC('bcc@youremail.com');

$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';
}
Nick
  • 1,032
  • 16
  • 27
  • 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. – Uland Nimblehoof Mar 04 '16 at 10:43