Getting a CC copy of user invite Emails - php

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();

Related

How to check if mail was send or not using php?

How to check if mail was send or not using php ?
I use this code for send email using php. It's work good.
But i want to check email send success or not, How can i do that ?
<?PHP
$to = "test#mail.com";
$subject = "test subject";
$message = "test message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: SENDER <noreply#sender.com>' . "\r\n";
$headers .= 'Return-Path: return#sender.com' . "\r\n";
mail($to, $subject, $message, $headers, '-freturn#sender.com');
?>

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 function with customizes from value

Why does this send emails?
$from_admin = $_POST[EMAIL];
$message_admin = 'some html...';
mail($to_admin,"subject",$message_admin,"FROM:$from_admin");
This email looks like it came from my gmail account.
While this doesn't send the email.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To:' . $to . "\r\n";
$headers .= 'From: Registration <register#myserver.org>' . "\r\n";
mail($to, $subject, $message, $headers);
When I contacted my hosting support, they said that I need to do SMTP authentication. But, then why does the first mail function work? I would be ok if its not actually using that server, but people can reply the the html email and still reach the correct account. Is there any way around this?

Sending multiple emails php, but just arriving to first recipient

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)

User Recommendation Email Body

The code below works great. It allows a user to send emails to other people in order to recommend my site.
How can I replace "Michael" below with $_POST['sendername']? I tried and it didn't work.
Thanks in advance,
John
$msg = "<html><body>Hello, your friend Michael recommends that you use <a href='http://www.site.com/'>site.com</a> Please visit the site.<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);
}
Simply:
$msg = "<html><body> … ".htmlspecialchars($_POST['sendername'])." … </body></html>";

Categories