php pear e-mail with bcc - php

I'm trying to send an e-mail with a bcc. I can send it with a bcc fairly easily, but that's not quite what I want. The code that does work is
$message = new Mail_mime();
$message->setTXTBody($_text);
...
$body = $message->get();
$thisSubject = $subject;
if (isset($_POST['_cc_sender'])) {
$extraheaders = array("Bcc"=>$from_email,
"From"=>$from_email,
"Subject"=>$thisSubject);
} else {
$extraheaders = array("From"=>$from_email, "Subject"=>$thisSubject);
}
$headers = $message->headers($extraheaders);
$mail_obj = Mail::factory("mail");
$mail_obj->send($to, $headers, $body);
Where an e-mail form can set "_cc_sender" to have the submitter receive a copy. The problem is that exposes the $to address. To hide it, I figured I'd just send the e-mail to the submitter with a bcc to the intended recipient, as so:
$message = new Mail_mime();
$message->setTXTBody($_text);
...
$body = $message->get();
$thisSubject = $subject;
if (isset($_POST['_cc_sender'])) {
$extraheaders = array("Bcc"=>$to,
"From"=>$from_email,
"Subject"=>$thisSubject);
$send_to = $from_email;
} else {
$extraheaders = array("From"=>$from_email, "Subject"=>$thisSubject);
$send_to = $to;
}
$headers = $message->headers($extraheaders);
$mail_obj = Mail::factory("mail");
$mail_obj->send($send_to, $headers, $body);
However, when I do that, I end up getting two messages sent to the $from_email. One seems to be the bcc because my (Thunderbird) message filters treat them differently - moving one into a subfolder.
To be clear, if I just use a bcc, the submitter gets a blind copy while the intended recipient gets the e-mail. However, when I try to switch who gets the bcc, both e-mails go to the submitter.
If I switch the bcc to a cc, I see both headers are set correctly (it is going to the $from_email with a cc to $to). However, the e-mail still gets sent to the $from_email twice.
I've done var_dumps of everything I can think of and they just show what I'd expect. From what I understand, the "to:" field / recipients is set in $mail_obj->send - it doesn't show up in my var_dumps - so I can't figure out why the send is messing up.
One thing I have discovered is that changing the Mail::factory parameter from "mail" to "sendmaiL" has removed the second e-mail. Now I'm just getting the one to the $send_to address. This suggests the problem is with the mail program, which may be handling the headers strangely.
I'd seen another post which suggested there should be a difference between the recipients list and the headers. When using "sendmail" (but not "mail"), that seems to be true. With "sendmail", the recipients list (first parameter in $mail_obj->send) is not shown in the headers - it's all a "bcc". Adding $from_email to the $send_tolist achieves the results I was looking for:
$message = new Mail_mime();
$message->setTXTBody($_text);
...
$body = $message->get();
$thisSubject = $subject;
if (isset($_POST['_cc_sender'])) {
$send_to = $from_email . "," . $to;
} else {
$send_to = $to;
}
$extraheaders = array("From"=>$from_email,
"Subject"=>$thisSubject);
$headers = $message->headers($extraheaders);
$mail_obj = Mail::factory("sendmail");
$mail_obj->send($send_to, $headers, $body);

Related

Php mail() vs Yahoo: Can someone Simply Explain Steps required for YAHOO to receive mail from php mail function?

I have seen thousands of similar questions asked on this topic. And for sure am aware of the "MARKED AS DUPLICATE QUESTION" thing in SO.
However, it is still not Clear how or what one has to do in simple terms to have yahoo Inbox emails from a PHP mail() function.
In the Yahoo site, they give a sample script to send mails like
Link http://help.yahoo.com/l/us/yahoo/smallbusiness/webhosting/php/php-03.html
$email = "EMAIL TO";
$subject = "Test Message";
$msg = "This is a test message";
//$eLog="/tmp/mailError.log";
//Get the size of the error log
//ensure it exists, create it if it doesn't
//$fh= fopen($eLog, "a+");
//fclose($fh);
//$originalsize = filesize($eLog);
mail($email,$subject,$msg);
//NOTE: I commented out unneeded lines
Using this basic approach found in the Yahoo's own legitimate website fails.
The second suggestion would be (for PERL) but can be converted to PHP with some editing:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
$title='mail test';
$to='MAIL ADDRESS TO SEND TO';
$from= 'EMAIL#YOURDOMAIN.COM';
$subject='Using Sendmail';
open(MAIL, "|/usr/sbin/sendmail -t");
## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL "This is a test message from Yahoo \n";
close(MAIL);
print "<html><head><title>$title<
/title></head>\n<body>\n\n";
## START HTML content
print "<h1>$title</h1>\n";
print "<p>A message has been sent from $from to $to";
## END HTML CONTENT
print "\n\n</body></html>";
Link: http://help.yahoo.com/l/us/yahoo/smallbusiness/webhosting/php/php-17.html
After few edits to make it PHPish it looks like:
<?php
///#!/usr/bin/perl
$title='_01_mail test';
$to='user_name#yahoo.com, user_name2#gmail.com';
$from= 'info#companyname.com';
$subject='_01_Using Sendmail';
## Mail Header
$headers = "To: $to\n";
$headers .= "From: $from\n";
$headers .= "Subject: $subject\n\n";
## Mail Body
$message = "<html><head><title>$title<
/title></head><body>
<h1>$title</h1>
<p>A message has been sent from $from to $to\n\n
</p></body></html>";
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>
The user_name2#gmail.com for GMAIL sends the Email as required but user_name#yahoo.com somehow does not go anywhere.
So, what should we do?... If YAHOO's own samples are not working, then:
a) Is PHP mail() function getting deprecated?... If so, what is the alternative?
b) Should the function still be valid, how do we come up with YAHOO Inbox friendly PHP Codes?
c) What is the best practice for PHP mail() Function?
EDIT:
Additional tests.
Just tested it in this format Suggest by PHP mail() function not sending email:
$subject = "subject";
$message = "message";
$to = "USER_NAME_HERE#yahoo.com";
$type = "plain"; // or HTML
$charset = "utf-8";
$mail = "no-reply#".str_replace("www.", "", $_SERVER["SERVER_NAME"]);
$uniqid = md5(uniqid(time()));
$headers = "From: ".$mail."\n";
$headers .= "Reply-to: ".$mail."\n";
$headers .= "Return-Path: ".$mail."\n";
$headers .= "Message-ID: <".$uniqid."#".$_SERVER["SERVER_NAME"].">\n";
$headers .= "MIME-Version: 1.0"."\n";
$headers .= "Date: ".gmdate("D, d M Y H:i:s", time())."\n";
$headers .= "X-Priority: 3"."\n";
$headers .= "X-MSMail-Priority: Normal"."\n";
$headers .= "Content-Type: multipart/mixed;boundary=\"----------".$uniqid."\""."\n\n";
$headers .= "------------".$uniqid."\n";
$headers .= "Content-type: text/".$type.";charset=".$charset.""."\n";
$headers .= "Content-transfer-encoding: 7bit";
STILL YAHOO DOES NOT INBOX THE MAIL.
EDIT2
I went to this Link: http://www.forensicswiki.org/wiki/Evolution_Header_Format
YAHOO said the header should be like this:
Subject: header test
From: Username <username#sendinghost.com>
To: Username <username#receivinghost.com>
Content-Type: text/plain
Date: Sat, 28 Jul 2007 11:52:35 +0200
Message-Id: <1185616355.19231.0.camel#localhost>
Mime-Version: 1.0
X-Mailer: Evolution 2.10.1
Content-Transfer-Encoding: 7bit
Thested with PHP mail() func. ... GMail received, again, YAHOO Rejected... I Get NO Error in the Logs
I Finally got a laaaaarge smile on my face.
Working together with #DaveRandom, He helped me come up with these codes:
NOTE: The code bellow uses PHPMailer
<?php
$senderName = 'Erick Best'; //Enter the sender name
$username = 'erickbestism#yahoo.com'; //Enter your Email
$password = 'passwordHere';// Enter the Password
$recipients = array(
'erickbestism#gmail.com' => 'Erick Best',
'erickbestism#yahoo.com' => 'Yahoo User',
);
///That's all you need to do
//No need to edit bellow
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
// Set up SMTP
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.mail.yahoo.com";
$mail->Port = 587; // we changed this from 486
$mail->Username = $username;
$mail->Password = $password;
// Build the message
$mail->Subject = 'PHPMailer mail() test';
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
$mail->AltBody = 'This is a plain-text message body';
$mail->addAttachment('images/phpmailer_mini.gif');
// Set the from/to
$mail->setFrom($username, $senderName);
foreach ($recipients as $address => $name) {
$mail->addAddress($address, $name);
}
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
And this WORKED like VOODOO!... It sent mails to any provider. Including **YAHOO**
Hope it helps someone!
try this:
<?php
include("Mail.php");
$recipients = "mailto#example.com";
$headers["From"] = "mailfrom#example.com";
$headers["To"] = "mailto#example.com";
$headers["Subject"] = "Test message";
$body = "TEST MESSAGE!!!";
$params["host"] = "example.com";
$params["port"] = "25";
$params["auth"] = true;
$params["username"] = "user";
$params["password"] = "password";
// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory("smtp", $params);
$mail_object->send($recipients, $headers, $body);
?>
where username and password are for a yahoo account.

Email header not working PHP

I'm using BlueHost and I can't seem to get my email form to work. This is the PHP:
$to = "test#email.com";
$subject = "test";
$message = "test message";
$from = $_POST['cf_email'];
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo $headers;
This doesn't seem to send the email. However, if I add ANY string in front of the email, then it works. e.g.:
$from = "zz".$_POST['cf_email'];
Can anyone tell me what I'm doing wrong? Thanks.
You cannot use any personal email ($_POST['cf_email']) in the 'From' header.
Also you have to add 'reply-to'.
Please check below thread for more info.
problem with php mail 'From' header

php email - $to multiple recipients

Even this must have been be asked many times, I will ask again since I cannot get it to work.
I am using php mail($to, $subject, $message, "From: $mysite<$myemail>\nX-Mailer:PHP/" .phpversion()); to send email to a single recipient.
Now I need to sent it to more than one recipients. I know that normaly I could do:
$to = "emailA#here.com,emailB#there.com";
But I need the one of the recipients to be the user that fills in the form e.g.:
//get all form details
$email = $_POST['email'];
$to = "$email,emailB#there.com";
The above ($to) I don't know if it is correct or not but is not working for me...
If I leave only the $to = "$email"; it gets send to $email (meaning that my rest of the code is ok).
Any suggestion on what is or may be wrong here?
Thank you.
Add a CC to your header.
$header ="From: $mysite<$myemail>" . PHP_EOL;
$header .= 'CC: emailB#there.com' . PHP_EOL;
//Rest of headers here

Sending multiple CC's and BCCs with PHP PEAR MAIL

I have a project that I am working on at my job and I am using Pear's mailing. I need to use smtp because we need to be able to track everything from our mailserver. And users need to be able to log in before sending a company based email. We cannot use php's mail function fo this.
My problem is that I cant find any documentation on the net for sending CC and Bcc as well as sending multiple BCCs. It is very easy to do with php' mail funciton . All you do is add it to the $header variable like so
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
This is my code for the php function where I use PEAR
function sender_mail($email,$subject,$mailmsg, $cc, $bcc){
include("Mail.php");
/* mail setup recipients, subject etc */
//DEFAULT VALUE OF FROM
$from = "noreply#addata.net";
//GET EMAIL OF USER
$result = mysql_query("SELECT email, email_pass FROM u_perinfo WHERE user_id = '$_SESSION[uid]'")
or die("There was an error when grabbing your email information");
if(mysql_num_rows($result) > 0){
$row = mysql_fetch_array($result);
if($row[0] != ''){
$from = $row[0];
}
$email_pass = $row[1];
}
$recipients = "$email";
$headers["From"] = "$from";
$headers["To"] = "$email";
$headers["Subject"] = $subject;
$headers["Cc"] = "$cc"; //Line added by Me to see if it works
$headers["Bcc"] = "$bcc"; //Line added by Me to see if it works
//$mailmsg = "Welcome to Addatareference.com! \r\n\r\nBelow is your unique login information. \r\n\r\n(Please do not share your login information.)$accountinfo";
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "smtp.emailsrvr.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "$from";
$smtpinfo["password"] = "$email_pass";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail_object->send($recipients, $headers, $mailmsg);
}
I have been trying to find a solution to this with no real info coming back my way. If someone could help me out with this I would be greatly appreciated.
There is a problem with Bcc though when using the PEAR Mail function: Thunderbird displays the Bcc header, so the recipient is not hidden, which is the whole point of Bcc. It is also displayed in the To: header list (since you have to include the Bcc list in recipients).
Edit: SOLVED - I found from another site that the solution is just to include the Bcc list in the $recipients, and not in any of the headers. It works!
Thus:
$bcc = "foo#example.com";
$to = "bar#example.com";
$headers = array(..other stuff.., 'To' => $to, ..rest of header stuff); // No Bcc header!
$recipients = $to.", ".$bcc;
$mail = $smtp->send($recipients, $headers, $message);
Edit #2: Acknowledging my source - http://raamdev.com/2008/adding-cc-recipients-with-pear-mail/
Have you tried to add multiple addresses "," separated?
$headers['Cc'] = 'cc#example.com, bb#example.com, dd#ex.com';
This might work, according to line 218 in the source.
You just need to add all to, cc, bcc in $recipients variable. Header decides where to send.

How can I update a table to include an email when it is received using PHP?

I am trying to send an email from PHP using PHPmailer().
I have included the following syntax to display the content in a table.
I need to update the table to display the email when the email is received.
How can I accomplish this?
$message ="<html><body> <table><tr><td>HELLO</td></tr></table></body></html>";
$body = eregi_replace("[\]",'',$message);
$mail = new PHPMailer();
$mail->From = $email;
$mail->FromName = $Name;
$mail->CharSet = "utf-8";
$mail->Subject = "GoodNoon:".$Name."";
$mail->IsHTML(true);
$mail->AltBody = "To view the message, please use an HTML compatible email viewer";
$mail->WordWrap = 50;
$mail->MsgHTML($body);
// send as HTML
$mail->AddAddress("mail#mail.de", "name");
if(!$mail->Send()) {
echo "Emai Not sent: " . $mail->ErrorInfo;
}
else {
echo "Email Sent";
}
First of all, you might want include a body tag. Replace:
<html><table><tr><td>HELLO</td></tr></table></html>
with
<html><body><table><tr><td>HELLO</td></tr></table></body></html>
Second:
How do the email contents look?
What e-mail client are you using?
Is HTML not being rendered at all, or just the <table> elements?
Sidenote: please try to provide more context when asking questions. You are more likely to receive positive and good responses to your questions if you take the time to review your question and make sure all the neccessary information is there and there are no (or at least, not many) formatting errors.
The problem is, I think, you miss the Content-type.
I use this :
$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn"; // or UTF-8
$headers .= "From: $from\r\n";
mail($to, $subject, $message, $headers);

Categories