Sending multiple emails php, but just arriving to first recipient - php

Basically I need to notify certain users of something happening in the application.
I bring the users from the database, everything works fine, but when i send the emails, only the first one in the list receives it, I tried switching the order they are brought, and still, so is not about a particular mail client.
I tried sending one by one and now sending all together and neither seems to work.
Have this ever ocurred before? I'm using some amazon server, with linux, but is a client's client's server so i can't really stick my head all over the config.
$ul = $this->q2ar("SELECT * FROM usr WHERE role_id in(1,7)");
//get admins from db
$ntf = '';
foreach($ul as $usr){
$headers = 'MIME-Version: 1.0 '." \r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . " \r\n";
$headers .= 'From: Centinelas <centinelas#serviciochevrolet.com.ec>' . " \r\n";
$body = "mail content";
$ntf .= "'".$usr['email']."'".', ';
}
$ntf = substr_replace($ntf ,"",-2);
$ml = mail($ntf,'Nuevo Caso en Centinelas Chevrolet',$body,$headers,'- fcentinelas#serviciochevrolet.com.ec');
return('email where sent to :<br/>'.$ntf);

Try sending an email to one person, like yourself or the sender then using the Cc to send to the rest.
$ul = $this->q2ar("SELECT * FROM usr WHERE role_id in(1,7)");
foreach($ul as $usr){
$ntf[] = $usr['email'];
}
$headers = 'MIME-Version: 1.0 '." \r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . " \r\n";
$headers .= 'From: Centinelas <centinelas#serviciochevrolet.com.ec>' . " \r\n";
$headers .= 'Cc: '.join($nft,",") . " \r\n";
$body = "mail content";
$ml = mail('centinelas#serviciochevrolet.com.ec','Nuevo Caso en Centinelas Chevrolet',$body,$headers,'- fcentinelas#serviciochevrolet.com.ec');
return('email where sent to :<br/>'.join($nft,","));

The whole use the Cc: string.
foreach($ul as $usr){
$ntf .= (($ntf=="")?"":", ")."'".$usr['email']."'";
}
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Centinelas <centinelas#serviciochevrolet.com.ec>' . "\r\n";
$headers .= 'From: Centinelas <centinelas#serviciochevrolet.com.ec>' . "\r\n";
$headers .= 'Cc: '.$ntf.'' . "\r\n";
you have to add $ntf "CC:" to the HEADERS (for clarity it is often ok to send the mail to the sender)

Related

HTML Email occasionally sending source code

At my company, we have had several clients claim that their order confirmation emails are coming through just as the HTML source, not formatted at all. However the majority of our clients are receiving their emails through fine.
I think I may have found a link which explains why only some clients receive the unformatted email, however I'm not sure of the cause. It seems that users with a custom domain name for their email address are experiencing the problems and regular email domains such as #gmail.com or #icloud.com are working fine.
The problem is not related to the client used to view the email as I have been able to replicate the problem with my private domain name and the source code is displayed in all clients.
Any idea what would be the cause of this?
Here are my headers:
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1";
Try following headers for HTML Email...
$from = $from . ' <' . $from . '>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'Return-Path: ' . $from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
$headers .= 'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'] . "\r\n";

Using a sender's name and email address in the header of a PHP email form

I can't get the syntax quite right with PHP. How do I change it to display the sender's full name as well as their email address in the 'From:' part this PHP email script?
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: " . $_REQUEST['email'] . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message .= 'Name: ' . $_REQUEST['name'] . "<br>";
$message .= $_REQUEST['message'];
I tried
$headers .= "From: " . $_REQUEST['email'] '<'$_REQUEST['name']'>'. "\r\n";
But I've screwed up the code somewhere.
Thanks guys!
Mail headers are Person Name <email#example.com> so your header needs to be:
$headers .= "From: $_REQUEST[email] <$_REQUEST[name]>\r\n";

php mail() not sending styles

I am trying to send style information along with an email via PHP mail() function. Unfortunately, even though my mail client is set to accept styled emails it still just renders as plain html text.
$to = $email;
$subject = "Subject Details";
$message='<html><body><table width="600" height="840" border="0" cellpadding="5" cellspacing="5">';
$message.='<tr><td height="110"><img src="https://user.co.uk/images/logo.jpg" alt="SignDox" width="300" height="100" /></td></tr>';
$message.='<tr><td height="29" bgcolor="#5C1561"> </td></tr>';
$message.='<tr><td height="537" valign="top">';
$message.="Message Goes Here\n\n";
$message.="Username: ".$new_agent_id."\n";
$message.="Password: ".$pass1."\n\n";
$message.="To sign in to your user panel follow this link: \n";
$message.="Inside your admin section you will be able to change your username and password.\n";
$message.='</td></tr>';
$message.='<tr><td height="140" bgcolor="#5C1561"> </td></tr></table></body></html>';
$from = "no-reply#sender.co.uk";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Note:
1) If intending to send HTML or otherwise Complex mails, it is recommended to use the PEAR package ยป PEAR::Mail_Mime.
2) It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.
Please read more this: PHP Manual
You need to define the correct headers in order to send HTML formatted emails. This can be done very simply using:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
However, I would urge you to use the PHPMailer Class. This class will handle all headers and anything else you may need. You can easily add attachments, embed images, send via SMTP etc...
It is a fantastic class, and not to mention the amount you will learn with objects and classes, expecially if you are a newbie to PHP :-)
See here for the PHPMailer Class
Set the content type, and charset as desired. You should also terminate your from header with \r\n.
$headers = "From:" . $from . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
try something like
<?php
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From:" . $from;
/*
$headers[] = "Bcc: JJ Chong <bcc#domain2.com>";
$headers[] = "Subject: {$subject}"; */ optional
mail($to, $subject, $message, implode("\r\n", $headers));
function send_mail($from,$fromName,$to,$object,$bodyText,$bodyHtml){
$site = "mywebsite.ca";
$from = $fromName." <".$from.">";
$limite = "_----------=_parties_".md5(uniqid (rand()));
$header = "Reply-to: ".$from."\n";
$header .= "From: ".$from."\n";
$header .= "X-Sender: <".$site.">\n";
$header .= "X-Mailer: PHP\n";
$header .= "X-auth-smtp-user: ".$from." \n";
$header .= "X-abuse-contact: ".$from." \n";
$header .= "Date: ".date("D, j M Y G:i:s O")."\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/alternative; boundary=\"".$limite."\"";
$message = "";
$message .= "--".$limite."\n";
$message .= "Content-Type: text/plain\n";
$message .= "charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";
$message .= $bodyText;
$message .= "\n\n--".$limite."\n";
$message .= "Content-Type: text/html; ";
$message .= "charset=\"iso-8859-1\"; ";
$message .= "Content-Transfer-Encoding: 8bit;\n\n";
$message .= $bodyHtml;
$message .= "\n--".$limite."--";
if(mail($to,$object,$message,$header)){
//echo all
}
else{
//echo mssage not submit
}
}
hope this function can help you. try to use this function to as your send_mail, then at the buttom try to
write a condition to see if your message is submitted.You just needed to give inputs in it
//your header should be like this
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

Getting a CC copy of user invite Emails

The code below works great. It allows a user to send a recommendation for my site to a list of friends via email.
For each person that gets the email below, I would like to get an email with that person's email, and the name of the person that sent them the message. If my email address was john#site.com, what code could I use to do this?
Thanks in advance,
John
$msg = "<html><body>Hello, your friend ".htmlspecialchars($_POST['sendername'])." recommends that you use <a href='http://www.site.com/'>Site.com</a>.<a href='http://www.site.com/'>Site.com</a><br><br><img src='http://site.com/images/blacklogo.PNG'></body></html>";
$subject = "Try out Site.com";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $_POST['sendername'] . "\r\n";
foreach($_POST['email'] as $email){
mail($email, $subject,$msg,$headers);
}
$msg = "<html><body>Hello, your friend ".htmlspecialchars($_POST['sendername'])." recommends that you use <a href='http://www.site.com/'>Site.com</a>.<a href='http://www.site.com/'>Site.com</a><br><br><img src='http://site.com/images/blacklogo.PNG'></body></html>";
$subject = "Try out Site.com";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "CC: foo#example.com\r\n";
$headers .= "BCC: foo#example.com\r\n";
$headers .= 'From: ' . $_POST['sendername'] . "\r\n";
foreach($_POST['email'] as $email){
mail($email, $subject,$msg,$headers);
}
Rather than messing about with raw headers, consider using one of the many great APIs available, like Swiftmailer, PHPMailer, or Zend_Mail to name just three. Zend_Mail example:
$mail = new Zend_Mail();
$mail->setBodyHtml('<p>hello</p>');
$mail->setFrom('foo#example.com', 'foo#example.com');
$mail->setSubject('Test Subject');
$mail->addTo('foo#example.com', 'Test');
$mail->addCc('foo#example.com', 'Another Test');
$mail->addBcc('foo#example.com', 'Another Test');
$mail->send();

How to display name in the from address?

I have a site in PHP.In this when I send the invitation to people it takes the from address as mysitename#servername.Instead of this I want o display the Mysitename
.How can I do.I have the code shown below.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To:'. $value . "\r\n";
$headers .= 'From:Mysitename'."\r\n";
How can I show like in users email's from address is Mysitename?
Thanks in advance?
$headers .= 'From: Mysitename <mysitename.com>' . "\r\n";
http://php.net/manual/en/function.mail.php
Maybe something along the lines of:
$headers .= 'From: Mysitename <owner#mysitename.com>' . "\r\n";
You need 'From' to be proper email address.

Categories