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...";
}
}
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:
https://stackoverflow.com/a/18382062/1415724
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.
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';
}
Related
I am not receiving emails from this contact form, but the message seems to sending okay and it also redirects me to the sent page.
I don't have access to the server only via FTP.
PHP
<?php
$to = 'test#gmail.com';
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$body = <<<EMAIL
<html>
<p><h3>Email Submited From Website.</h3></p>
<p><strong>Name:</strong> $name</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Subject:</strong> $subject</p>
<p><strong>Message:</strong> $comment</p>
</html>
EMAIL;
// 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: test email' . "\r\n";
$headers .= 'From: <website#mt.co.uk>' . "\r\n";
//$headers .= 'Cc: noreply#example.com' . "\r\n";
//$headers .= 'Bcc: noreply#example.com' . "\r\n";
if ($_POST['submit']){
mail($to, $subject, $body, $headers);
header ("Location: message-sent.php");
die();
} else {
header ("Location: message-failed.php");
die();
}
?>
Check if mail is actually being sent:
if (mail($to, $subject, $body, $headers)===false) {
echo "Not sent!";
} else {
echo "Sent!";
}
Change this:
$headers .= 'To: test email' . "\r\n";
$headers .= 'From: <website#mt.co.uk>' . "\r\n";
To this:
$headers .= "To: $to <test email>\r\n";
$headers .= "From: website#mt.co.uk <website#mt.co.uk>\r\n";
Also, you need to sanitize the subject and body of the email so that the email arrives, but this will usually be reflected in the results after email() reports a success, in that case the email will bounce, go to the spambox, or simply be refused.
If your hosting provider doesn't have an email server, you could try to use a free email server and phpMailer. https://github.com/PHPMailer/PHPMailer
I am using the below PHP code that takes values from an HTML form and sends an email in HTML. This works fine for me.
Now I need to add an ATTACHMENT field that will send the attachment along with the email (with or without storing the file on the server). Can someone please suggest how to do it with or without PHPmailer?
Thanks!
<?php
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['address'])) {
die('Error: Missing variables');
}
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$position=$_POST['position'];
$email=$_POST['email'];
$message=$_POST['message'];
$ip=$_SERVER['REMOTE_ADDR'];
$to="email#server.com";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=utf-8\r\n";
$headers = 'From: '.$_POST['name'].' <'.$_POST['email'].'>';
'X-Mailer: PHP/' . phpversion();
$subject='Job Application from '.$name."\n\n\n";
$body.='Name: <b>'.$name."</b><br>\n";
$body.='Mobile No: <b>'.$mobile."</b><br>\n";
$body.='Position: <b>'.$position."</b><br>\n";
$body.='Email: <b>'.$email."</b><br>\n";
$body.='Message: <b>'.$message."</b><br>\n";
$body.='IP address of the submitter: '."\n".$ip."\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email."\r\n";
if(mail($to, $subject, $body, $headers)) {
header("Location: thank-you.html");
} else {
echo "Something has gone wrong! Please try again!";
}
?>
It's easier to use PHPMailer..
You can get the documentation and tutorial here.
This is the code for a basic mail and attaching files:
<?php
require_once('../class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Hope this helps.
$filePath = 'foldername/attachment.zip';
$file = fopen($filePath,'rb');
$data = fread($file,filesize($filePath));
fclose($file);
$data = chunk_split(base64_encode($data));
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$data
Add this code before this line "if(mail($to, $subject, $body, $headers)) {".
I am pulling email addresses from my mailing list in a txt file. with the following:
clearstatcache();
$file = file("test.txt");
for ($i = 0; $i < 20; $i++) {
$emails .= $file[$i];
}
As you can see I've stored them in $emails. and if I echo $emails, I get the emails listed:
info#example.com, test#mydomain.com, admin#domain.com, etc.
now to sending the BCC:
// recipient
$to = '';
// subject
$subject = 'The subject is here';
// message
$message = 'The body of the email is here';
// 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 .= 'From: John Doe <info#example.com>' . "\r\n";
$headers .= 'Bcc: '.$emails . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
The mail doesn't get sent to the list, only to info#example.com - So this doesn't work and something unexpected happens - for some strange reason the 20 emails from the for loop are listed at the top of the email body when received by info#example.com
When I try manual input it works perfectly. so the code below works, but manual input is counter productive to what I am trying to achieve.
$test = "info#example.com, test#mydomain.com, admin#domain.com,";
// recipient
$to = '';
// subject
$subject = 'The subject is here';
// message
$message = 'The body of the email is here';
// 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 .= 'From: John Doe <info#example.com>' . "\r\n";
$headers .= 'Bcc: '.$test . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
So it appears that the issue is in the variable, but I can't figure out why it doesn't work because $emails echoes out all the email addresses fine.
White space is automatically added to the end of line. This change in the for loop fixes the issue.
$emails .= trim($file[$i]);
You must close the loop after:
mail($to, $subject, $message, $headers);
}
and
$headers .= 'Bcc: '.$emails . "\r\n";
is
$headers .= 'Bcc: '.$file[$i] . "\r\n";
So the loop will run the entire program 20 times. Do not place recipient in $ to because otherwise will send 20 times as well.
Tested and works great.
Here's my code:
<?php
$to = 'test#hotmail.com';
$subject = 'reservation hotel n';
$msg ='ok';
// Make sure to escape quotes
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: hôtel <reservation#hotel.com' . "\r\n";
mail($to, $subject, $msg, $headers);
?>
It worked for Gmail, Yahoo, GMX ...but it didn't work for Hotmail/Live/MSN.
Because it worked for Gmail, I can assume that it has nothing to do with my server, right?
I also tried it with just:
http://www.microsoft.com/mscorp/safety/content/technologies/senderid/wizard/Default.aspx
System Maintenance in progress. Please try again later.
think's for help
Check it
<?php
$to = "$email";
$subject = "Welcome to";
$message = " Hi $username,<br /><br />
Thank you for signing up with us.<br />
Thanks <br />";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <test#gmail.com>' . "\r\n";
$mail=mail($to,$subject,$message,$headers);
?>
problem solved i change with php mailer
require_once "class.phpmailer.php";
require_once "class.smtp.php";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "**#gmail.com";
$mail->Password = "**";
$mail->From = "***";
$mail->FromName = "Hôtel **";
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.
//Caractéristiques du message
$mail->CharSet = 'utf-8';
$mail->ContentType = 'text/plain';
$mail->Encoding = '8bit';
$mail->Subject = "**";
$mail->Body = "okkk";
$mail->WordWrap = 0;
$mail->AddAddress("**#hotmail.com", "nom");
$mail->Send();
I think that you need one of these for it to work:
Sender ID Framework SPF Record Wizard
It should solve your problem as Hotmail wants this for safety.
i have use this code in php mail send with attachment. I am using the Apache http server with wamp.
<?php $to = 'santosh_091020#rediffmail.com';
// Declaring the necessary variables for mail sending :
$subject = 'Testing sendmail.exe';
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$mob = $_REQUEST['mob'] ;
$msg = $_REQUEST['msg'] ;
$message = ' santosh enquire \n\nName: '.$name.' \n Email:'. $email.' \n Mobile:'. $mob.' \n Message: '.$msg.' \n ';
// Declaration of the attributes for attachment.
$upload_name=$_FILES["upload"]["name"];
$upload_type=$_FILES["upload"]["type"];
$upload_size=$_FILES["upload"]["size"];
$upload_temp=$_FILES["upload"]["tmp_name"];
$fp = fopen($upload_temp, "rb");
$file = fread($fp, $upload_size);
fclose($fp);
$file = chunk_split(base64_encode($file));
$num = md5(time());
// Defining the contents of the header.
$headers = "From: ".$email. "\r\n" . "CC: santosh#creativecrows.com" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
//$headers .= "Message-ID: <".gettimeofday()." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n";
$headers .= "Content-Type:".$upload_type." ";
$headers .= "name=\"".$upload_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$upload_name."\"\r\n\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."--";
//$data = chunk_split(base64_encode($data));
// sending the mail.
if(mail($to, $subject, $message, $headers))
{
echo "Email sent";
send();
}
else
{
echo "Email sending failed";
}
//send mail in client
function send()
{
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$to= trim($email);
$subject = 'thanx';
$message = '<html><head><title>HTML email</title></head>
<body style="background-color:#000099;"><p style="color:#000099;">This email contains HTML Tags!</p><table><tr><th>Firstname</th><th>Lastname</th></tr><tr><td>John</td><td>Doe</td></tr></table></body></html>';$headers1 = 'MIME-Version: 1.0' . "\r\n";
$headers1.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers1.= "From: ".$email;
if(mail($to, $subject, $message,$headers1))
{
echo "Email sent";
}
else
{
echo "Email sending failed";
echo $to;
echo $subject;
echo $message;
}
}
?>
but i got the following error;
Delivery to the following recipient failed permanently:
1.0#localhost
Technical details of permanent failure:
DNS Error: Domain name not found.
please help me. Thnx in advance.
Check this URL,
seems your header info is not absolute
http://webcheatsheet.com/php/send_email_text_html_attachment.php
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup server
$mail->Port = '';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = '';
$mail->FromName = '';
$mail->AddAddress($row['client_email'], ''); // Add a recipient
$mail->AddReplyTo('', 'Information');
$mail->AddCC('cc#example.com');
$mail->AddBCC('bcc#example.com');
$mail->WordWrap = 50;
// Set word wrap to 50 characters
$mail->AddAttachment('kurtacompany/techreporting/upload/"'.$row['file1'].'"'); // Add attachments
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = '';
$mail->Body = '<p>type whatever you want</p>';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';