I'm trying to send multiple e-mails with different messages.this is my code, it already works and could be sent on 'blazriku#gmail.com' and 'henrikus.antony#gmail.com' but the problem is "send message" and "welcome" always sent to both, not to each other.
then, how to make it "send message" to 'blazriku#gmail.com' and "welcome" to 'henrikus.antony#gmail.com?'
$email = 'blazriku#gmail.com , henrikus.antony#gmail.com';
$subject = array("send message", "Welcome");
$message = array("send message", "Welcome");
$name = array('Admin','Admin');
$to=$email;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: kebunbibit.id <noreply#yourwebsite.com>'."\r\n" . 'Reply-To: '.$name.' <'.$email.'>'."\r\n";
$headers .= 'Cc: admin#yourdomain.com' . "\r\n"; //untuk cc lebih dari satu tinggal kasih koma
foreach (array_combine($subject, $message) as $subjects => $messages){
#mail($to, $subjects, $messages, $headers);
}
if(#mail)
{
print "<script>window.alert('E-Mail Terkirim!')</script>";
print "<script>window.location='home.php?page=surat_jalan'</script>";
}
else{
print "<script>window.alert('E-Mail Gagal Terkirim!')</script>";
print "<script>window.location='home.php?page=surat_jalan'</script>";
}
Rather than looping an email, you may create separate array of emails which are intended for sending different type of subject and emails.and afterwards loop those email array with specified subject and email content.see below:
$email = 'blazriku#gmail.com , henrikus.antony#gmail.com';
// $subject = array("send message", "Welcome");
// $message = array("send message", "Welcome");
$name = array('Admin','Admin');
$to=$email;
$welcome_msg_user_group = array('henrikus.antony#gmail.com');
$send_msg_user_group = array('blazriku#gmail.com');
$welcome_email = array('subject' => 'Welcome', 'msg_body' => 'blah blah');
$send_email = array('subject' => 'Send message', 'msg_body' => 'blah blah send msg body');
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: kebunbibit.id <noreply#yourwebsite.com>'."\r\n" . 'Reply-To: '.$name.' <'.$email.'>'."\r\n";
$headers .= 'Cc: admin#yourdomain.com' . "\r\n"; //untuk cc lebih dari satu tinggal kasih koma
foreach($welcome_msg_user_group as $welcome_user) {
#mail($welcome_user, $welcome_email['subject'], $welcome_email['msg_body'], $headers);
}
foreach($send_msg_user_group as $send_user) {
#mail($send_user, $send_email['subject'], $send_email['msg_body'], $headers);
}
if(#mail)
{
print "<script>window.alert('E-Mail Terkirim!')</script>";
print "<script>window.location='home.php?page=surat_jalan'</script>";
}
else {
print "<script>window.alert('E-Mail Gagal Terkirim!')</script>";
print "<script>window.location='home.php?page=surat_jalan'</script>";
}
Hope it helps you!
Related
I want to send a email from php mail function. This is my code
<?php
$to = "modiv2301#gmail.com";
$subject = "HTML email";
$message = "
<h1>this is msg</h1>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <modiv2301#gmail.com>' . "\r\n";
$success=mail($to,$subject,$message,$headers);
if (!$success) {
$errorMessage = error_get_last()['message'];
print_r($errorMessage);
}else{
echo "Success";
$errorMessage = error_get_last()['message'];
print_r($errorMessage);
}
?>
when i run this code its showing me Success but i am not Receiving any email
My Php mail code is like this i want to send html content in mail but i can not send html template with this code
$to = $email;;
$subject = 'New Order Detail';
$message = 'Hi Jane';
$from = 'info#abc.co.in';
// Sending email
if(mail($to, $subject, $message))
{
echo 'Your mail has been sent successfully.';
}
else
{
echo 'Unable to send email. Please try again.';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message = '<table border="1" align="center" width="50%">';
$message .= '<tr><td>Test Mail';
$message .= '</td></tr>';
//$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</table>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers))
{
echo 'Your mail has been sent successfully.';
} else
{
echo 'Unable to send email. Please try again.';
}
}
This kind of output i get in mail i do not know where is my mistake in this code..
Output
<html><body>
<table border="1" align="center" width="50%">
<tr><td>Test Mail</td></tr>
</table>
</body></html>
There are some errors in your code and also wrong if statement I would like to recommend following script to you for sending email.
$to = $email;
$subject = 'New Order Detail';
$message_text = 'Hi Jane';
$from = 'info#abc.co.in';
$from_name = 'John';
$to_name = 'Smith';
// Sending email
if(!empty($to) && !empty($message_text ))
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' . ucwords($to_name) .' <'.$to.'>' . "\r\n";
$headers .= 'From: ' . ucwords($from_name) . ' <'. $from. '>' ."\r\n";
// Compose a simple HTML email message
$message = '<html><body>';
$message = '<table border="1" align="center" width="50%">';
$message .= '<tr><td>Test Mail';
$message .= '</td></tr>';
$message .= '<p style="color:#080;font-size:18px;">'.$message_text .'</p>';
$message .= '</table>';
$message .= '</body></html>';
// Sending email
mail($to, $subject, $message, $headers);
echo 'Your mail has been sent successfully.';
}else{
echo 'Unable to send email. Please try again.';
}
add proper header contents.
// Compose a simple HTML email message
$message = '<html><body>';
$message = '<table border="1" align="center" width="50%">';
$message .= '<tr><td>Test Mail';
$message .= '</td></tr>';
//$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</table>';
$message .= '</body></html>';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: abc <abc#example.com>, xyz <xyz#example.com>';
$headers[] = 'From: ABC <Abc#example.com>';
$headers[] = 'Cc: abc#example.com';
$headers[] = 'Bcc: xyz#example.com';
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
Set your character set to UTF-8:
$headers .= "Content-type:text/ html;charset=UTF-8" . "\r\n";
That should fix it.
A variation on a theme perhaps and redundant as you have an answer. You should be able to add styles within the style tag region of the below code - or you could potentially add a link to an external css file but that could cause issues.
$to = $email;
$subject = 'New Order Detail';
$message = 'Hi Jane';
$from = 'info#abc.co.in';
$obj=new StdClass;
$obj->success='Your mail has been sent successfully.';
$obj->fail='Unable to send email. Please try again.';
$obj->nl="\r\n";
$status = #mail( $to, $subject, $message );
if( $status ) {
exit( $obj->success );
} else {
$headers = array();
$html = array();
$headers[]="MIME-Version: 1.0";
$headers[]="From: {$from}";
$headers[]="Reply-To: {$from}";
$headers[]="X-Mailer: PHP/". phpversion();
$headers[]="MIME-Version: 1.0";
$headers[]="Content-type: text/html; charset=iso-8859-1";
$html[]="";
$html[]="<html>";
$html[]="<head>";
$html[]="<title>{$subject}</title>";
$html[]="<style>";
$html[]="p{color:red;font-size:18px;}";
$html[]="table{border:1px solid black;width:50%;display:table;}";
$html[]="td{text-align:center;background:yellow;color:blue;}";
$html[]="</style>";
$html[]="</head>";
$html[]="<body>";
$html[]="<table>";
$html[]="<tr><td>Test Mail</td></tr>";
$html[]="<tr><td><p>Will you marry me?</p></td></tr>";
$html[]="</table>";
$html[]="</body>";
$html[]="</html>";
$status = #mail( $to, $subject, implode( $obj->nl, $html ), implode( $obj->nl, $headers ) );
exit( $status ? $obj->success : $obj->fail );
}
I am trying to send mail to all users that have no created reports in one month, also trying to send mail to this users with foreach loop and with mail function, when i refresh one by one then it sends mail one by one, then it works. i want to send mail to all this users in one time.
$from = "xxx#xxx.com";
$subject = "";
$headers = "";
$inc = 0;
foreach($query->result_array() as $row)
{
$inc++;
$to = $row['us_email'];
$to_date = $row['report_date'];
if($to_date != "0000-00-00")
{
$subject = "Hello! (Reports Are Not Created).";
//begin of HTML message
$message = "1 month above you should create reports.";
}
else if($to_date == "0000-00-00")
{
$subject = "Hello! (Generate Reports).";
//begin of HTML message
$message ="generate reports to get more.";
}
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "To: User <".$to.">, Admin <".$from.">" . "\r\n";
$headers .= "From: Complete Online Marketing <".$from.">" . "\r\n";
$headers .= "Reply-To: Recipient Name <".$from.">";
$headers .= "Cc: [email]".$from."[/email]" . "\r\n";
$headers .= "Bcc: [email]".$from."[/email]" . "\r\n";
// now lets send the email.
if(mail($to, $subject, $message, $headers))
{
echo $inc.") ".$row['us_firstname']." ".$row['us_lastname']." - Sending Success...\n";
$ins = array('report_mail'=>'0');
$this->db->update('se_user', $ins, array('us_id' => $row['us_id']));
}
else
{
echo $inc.") ".$row['us_firstname']." ".$row['us_lastname']." - Sending Fail...\n";
}
}
I have created reset password page, where used enters hes email, and PHP sends him back a the reset key. Mail works, but its going as plain text in my gmail account. I wanted it to go in HTML.
$subject = "Your password reset for {$config['site_name']}";
$message = "<html><body>";
$message .= "<p>Someone on" . $config['site_domain'] . "tried to reset your password.</p>";
$message .= "<p>Please click below link, if you want to reset your password.</p>";
$message .= "<p><a href='" . $config['site_url'] . "/forgot_password.php?key=" . $key . "'>" . $config['site_url'] . "/forgot_password.php?key=" . $key . "</a></p>";
$message .= "<p>Thank you,<br>The Admin - " . $config['site_url'] . " </p>";
$message .= "</body></html>";
// Create email 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";
// Additional headers
//$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= "From: " . $config['site_name'] . " <noreply#" . $config['site_domain'] . "> \r\n";
$headers .= "X-Sender: <noreply#" . $config['site_domain'] . "> \r\n";
$headers .= "Reply-To: <noreply#" . $config['site_domain'] . "> \r\n";
mail($input['email'],$subject,$message,$headers);
//update pw_reset field into DATABASE
$stmt = $mysqli->prepare("UPDATE members SET pw_reset = ? WHERE email = ?");
$stmt->bind_param("ss", $key, $input['email']);
$stmt->execute();
$stmt->close();
You should structure your headers like this:
$headers = 'From: You <you#example.com>' . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Notice that the From is before the MIME and Content and only Content ends with "\r\n", the other are just "\n".
Source (saganwebdesign)
Try this function. returns true on success
function sendMail($email, $subject, $message)
{
$supportEmail = 'support#abc.com';
$from = 'Test Application';
$msg = $message;
$from = str_replace(' ', '-', $from);
$frm = $from.' <'.$supportEmail.'>';
preg_match("<(.*)#(.*\..*)>", $frm, $match);
///////////////////Headers/////////////////
$hdr='';
$hdr.='MIME-Version: 1.0'."\n";
$hdr.='content-type: text/html; charset=iso-8859-1'."\n";
$hdr.="From: {$frm}\n";
$hdr.="Reply-To: {$frm}\n";
$hdr.="Message-ID: <".time()."#{$match[2]}>\n";
$hdr.='X-Mailer: PHP v'.phpversion();
$x=#mail($email, $subject, $msg, $hdr);
if($x==0)
{
$email=str_replace('#','\#', $email);
$hdr=str_replace('#','\#',$hdr);
$x=#mail($email, $subject, $msg, $hdr);
}
return $x;
}
I have an html form the links to a PHP email. The form works well, but I am having trouble with the Cc and Bcc not coming through.
Here is the entire code. Please review and help me understand what I am getting wrong on the Cc and Bcc parts in the headers.
Thanks:
<?php
$emailFromName = $_POST['name'];
$emailFrom = $_POST['email'];
$emailFromPhone = $_POST['phone'];
$email9_11 = $_POST['9-10'];
$email10_11 = $_POST['10-11'];
$email11_12 = $_POST['11-12'];
$email12_1 = $_POST['12-1'];
if (empty($emailFromName)) {
echo 'Please enter your name.';
} elseif (!preg_match('/^([A-Z0-9\.\-_]+)#([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
echo 'The email address entered is invalid.';
} else {
$emailTo = "main#gmail.com" ;
$subject = "Family History Conference Registration";
if (!empty($emailFrom)) {
$headers = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
} else {
$headers = 'From: Family History Conference <noreply#domain.org>' . "\r\n";
$headers .= 'Cc: $emailFrom' . "\r\n";
$headers .= 'Bcc: myemail#domain.com' . "\r\n";
}
$body = "From: ".$emailFromName."\n";
$body .= "Email: ".$emailFrom."\n";
$body .= "Phone: ".$emailFromPhone."\n\n";
$body .= "I would like to attend the following classes.\n";
$body .= "9:10 to 10:00: ".$email9_11."\n";
$body .= "10:10 to 11:00: ".$email10_11."\n";
$body .= "11:10 to 12:00: ".$email11_12."\n";
$body .= "12:10 to 1:00: ".$email12_1."\n";
/* Send Email */
if (mail($emailTo, $subject, $body, $headers)) {
echo "<h2>Thank you for Registering</h2>
<h3>You have registered for the following classes</h3>
<p>9:10 to 10:00am: \"$email9_11\" <br />
10:10 to 11:00am: \"$email10_11\"<br />
11:10 to 12:00: \"$email11_12\"<br />
12:10 to 1:00: \"$email12_1\"</p>
<p>We look forward to seeing you October 31, 2010</p>";
} else {
echo 'There was an internal error while sending your email.<br>';
echo 'Please try again later.';
}
}
?>
You're using single quotes
$headers .= 'Cc: $emailFrom' . "\r\n";
PHP won't interpret variables inside single quotes, you must use double quotes
$headers .= "Cc: $emailFrom\r\n";