When I send a mail with PHP it displays correct in Gmail but not in Outlook. It's a mail with a PDF attachment and some text. The PDF is created with fpdf and sent as an attachment, this works fine in Gmail and Outlook.
The only problem there is, in Gmail the text of the emails is displayed correctly and in Outlook it's just a blank page and the text is an extra attachment.
Here is my code:
// encode data
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// email stuff (change data below)
$to = $contactEmail;
$from = $myEmail;
$subject = "MySubjectHere";
$message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head></head><body>
<p>Hi '$contactName.',</p><br>
<p>Please find your pdf attached per your request.</p>
<p>Feel free to call or email if you have any questions.</p>
<p>Kind regards,</p><br>
<p>MyNameHere</p>
<p><strong>Manager</strong></p>
<img alt="image" src="http://LinkToImage.png"/>
<p style="color:#76923c;">
<strong>P:</strong> 01 2345 6789|
<strong>W:</strong> www.example.com</p>
<p style="color:#76923c;">
<strong>A:</strong> 94 Test Street.
</p></body></html>';
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = $id."_".str_replace(" ","_",$Name).'.pdf';
// main header
$headers = "From: ".$from.$eol;
$headers .= "Bcc: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
mail($to, $subject, $body, $headers);
I found where it went wrong:
$body .= $message.$eol;
Should be
$body .= $message.$eol.$eol;
Finally found it after 3 days!
Thanks for the responses anyway!
$to = $contactEmail;
$from = $myEmail;
$boundary = md5(date('r', time()));
$msg = "This is a multi-part message in MIME format.\n\n";
$msg .= "--$boundary\n";
$msg .= "Content-Type: text/html; charset=utf-8; format=flowed; delsp=yes\n";
$msg .= "Content-Transfer-Encoding: 7bit\n\n";
$msg .= "<h1>Website Enquiry</h1>
<p><strong>Name:</strong> $name<br />
<strong>Email:</strong> $email<br />
<strong>Phone:</strong> $phone<br />
<strong>Subject Name:</strong> $subject<br />
<strong>Question:</strong> $question</p>";
$e_headers = "From: " . $from . "\n";
$e_headers .= "MIME-Version: 1.0\n";
$e_headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
mail($to, $subject, $msg, $e_headers);
This is the code I would use for a HTML contact form.
You shouldn't need to pass the content through the PHP_EOL, just the attachment
$separator = md5(time());
$eol = "\r\n";
$message=""; // Always empty
$mailmessage="<h1>Yes got it</h1>"; //html content
$headers = "From: ".$email.$eol;
$headers .= "Bcc:". $Bcc. "\n";
$headers .= "Cc:". $cc. "\n";
$headers .= "Return-Path: <".$email.">\n";
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$body .= "--" . $separator . "\r\n";
$body .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n";
$body .= $message . "\r\n";
$body = "--" . $separator . $eol;
$body .= "Content-type: text/html; charset=\"utf-8\"\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
$body .= $mailmessage . $eol.$eol;
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $file . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol.$eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
mail($to, $subject, $body, $headers);
Related
I have been looking for a mailform script and came up with his. Works fine, but just this matter: when I reply to the received email, I am replying to myself, whilst I would like to reply automatically to the mailadres of the sender.
<?php
$to = "MY#MAILADRES.COM";
$subject = "mail via website";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$onderwerp = $_POST['onderwerp'];
$comments = $_POST['message'];
$message = "
name: $firstname $lastname
email: $email
subject: $onderwerp
message: $comments
";
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$headers = "From: MY#MAILADRES.COM\r\n".
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
foreach($_FILES as $userfile)
{
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
if (file_exists($tmp_name))
{
if(is_uploaded_file($tmp_name))
{
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
$message.="--{$mime_boundary}--\n";
if (mail($to, $subject, $message, $headers))
echo "Thanks for getting in touch.<br>Your message wil get my full attention.<br>I will get back to you soon.";
else
echo "Error in mail.<br>Please try again.";
?>
What should I change in this code, please?
I added these (seperately):
$headers .= "From: Contact Form (Domain.com) <no-reply#domain.com>\r\n";
$headers .= "Reply-To: ".$_REQUEST['email']."<". $_REQUEST['email'].">\r\n"
But none seem to work. I got this code to work with and all good, except for the reply-to.
I have created a PDF using tcpdf function, then i wants to send it to a mail as attachment. i have used following coding at end of the creation of PDF. But it working in some server, but not working in some other servers
$from = "manikandan#niral.us";
$subject = "test";
$message = $_REQUEST['msg'];
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// $eol = "\r\n";
// attachment name
$filename = "Yourinvices.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output('Patient'.$invnumserial.'.pdf', 'S');
$attachment = chunk_split(base64_encode($pdfdoc));
// encode data (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Enconding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charsrt=\"iso-8859-1\"".$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
//$headers .= "Content-Type: application/zip; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
// send message
if(#mail($to, $subject, $message, $headers))
{
Redirect("../../patient_invoicelist.php?msg=1");
}
else Redirect("../../patient_invoicelist.php?msg=2");
Use PHPMailer to send Emails with Attachments. It will be very easy to send emails with attachments using this library.
Download PHPMailer library from this link. You will also find examples to use this class.
http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/PHPMailer%20v5.1/
$mail->SetFrom('noreply#test.com', 'test');
$mail->Subject = $subject;
$mail->AddAttachment($Pdf_path_here);
$mail->MsgHTML($body);
$mail->AddAddress($to, "");
if(!$mail->Send())
{
echo "sent";
}
else
{
echo "error";
}
Instead of adding the following Header information it is showing HTML tag in PHP Mail.
$headers = "From: ".$name."<".$email.">\r\n";
$headers .= "Reply To: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\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";
Please help me.
Try this,
$subject = 'Mail Subject';
$message = 'Test';
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "X-Mailer: PHP \r\n";
$headers .= 'From: ".$name."<".$email.">' . "\r\n";
$headers .= 'Reply-To: ".$email."' . "\r\n";
mail($to, $subject, $message, $headers);
Example from my mail()
$header = "From: Backapp.ru <noreply#backapp.ru>\r\nContent-type: text/html; charset=utf-8";
The basic usage of mail() function with headers is:
<?php
$to = "you#local.com";
$subject = "My email test.";
$message = "Hello";
$headers = "From: myplace#local.com\r\n";
$headers .= "Reply-To: myplace2#local.com\r\n";
$headers .= "Return-Path: myplace#local.com\r\n";
$headers .= "CC: sombodyelse#local.com\r\n";
$headers .= "BCC: hidden#special.com\r\n";
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>
You don't send us your output so we don't know wheres the problem.
I'm trying to write a page where users can send an email along with an attachment. I think I'm almost there. Right now I get the email, but the attachment is either empty in filesize, or when I try to open it, I get a message that it cannot be opened. Any help is appreciated.
function valid_email($Email)
{
//new regex, didn't give me any errors...might be a bit more exact
if (ereg('^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$', $Email) != false)
return true;
else
return false;
}
function sendEmail($email){
return mail ($email['to'], $email['subject'], $email['message'], $email['headers']);
}
if ( strlen($_FILES['Resume_File']['name']) > 0 )
{ // If a file was uploaded, do some processing
$filename = preg_replace('/[^a-zA-Z0-9._-]/', '', $_FILES['Resume_File']['name']);
$filetype = $_FILES["Resume_File"]["type"];
$filesize = $_FILES["Resume_File"]["size"];
$filetemp = $_FILES["Resume_File"]["tmp_name"];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
if ( !preg_match('/\.(doc|docx|odt|pdf|rtf|txt)/i', $ext) )
{ $errors++;
$errorLog .= "Upload filetype not supported.";
}
else if ( $filesize > 2000000 )
{ $errors++;
$errorLog .= "File size too high, up to 2MB is allowed.";
}
else
{ // Looks like the file is good, send it with the email
//$fp = fopen($filetemp, "rb");
//$file = fread($fp, $filesize);
//$file = chunk_split(base64_encode($file));
//$email['headers'] .= "\n--{$num}\n";
//$email['headers'] .= "Content-Type:{$filetype}";
//$email['headers'] .= "name={$filename}r\n";
//$email['headers'] .= "Content-Disposition: attachment; ";
//$email['headers'] .= "filename={$filename}\n";
//$email['headers'] .= "{$file}";
}
}
// get posted data into local variables
$fname = trim(stripslashes($_POST['fname']));
$lname = trim(stripslashes($_POST['lname']));
$emailAddress = trim(stripslashes($_POST['email']));
$company = trim(stripslashes($_POST['company']));
$information = trim(stripslashes($_POST['information']));
$subject = trim(stripslashes($_POST['subject']));
$title = trim(stripslashes($_POST['title']));
//setup email
$to = 'me#me.com';
$subject = "Resume Submission";
$headers = "From: {$fname} {$lname} <{$emailAddress}>\r\n";
// prepare email body text
$message = "First Name: {$fname} <br>";
$message .= "Last Name: {$lname} <br>";
$message .= "Email: {$emailAddress} <br>";
$message .= "Title: {$title} <br><br>";
$message .= "Comments: {$information}";
if ( $errors == 0 ) {
// Attachment headers
//$to = "myemail#mydomain.com";
//$from = "Website <website#mydomain.com>";
//$subject = "Test Attachment Email";
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
//$filename = "document.pdf";
//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers .= "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
}
//sendEmail($email);
// validation
if ( $errors == 0 ) {
if (valid_email($emailAddress) && $fname != "" && $lname != "") { //if return is true...
mail($to, $subject, $body, $headers);
echo 0; //Success
}else { //otherwise
echo 1; //Error
}
} else {
echo 1; //Error
}
I am using portions of code which I found here
PHP mail() attachment problems
Thank you!
PHP's mail() function is really poor, particularly when trying to do advanced stuff like adding attachments. It's really only worth using for the most basic "send a notification email to the site admin" type emails, and even then it's not always the best solution.
I would suggest ditching any attempt to work with the mail() function itself, and instead switch to using a decent PHP mailer class such as the appropriately named phpMailer.
phpMailer makes creating emails from PHP dead easy. I has all the features you could want, including making it very easy to add attachments, write HTML emails, and plenty more.
But the best thing about phpMailer is that it removes all the need to waste dozens of lines of code formatting the email headers. All that stuff with the separators and mime types becomes reduced to a few simple lines of code. Easier to read, easier to maintain, and less likely to have bugs. You win all round.
//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));
$pdfdoc is not mentioned any where in the page. I presume that is the issue:|
After trying to read various articles on sending emails with attachments in PHP (I am use to ASP with VBScript), I wrote the code below. Unfortunately, it does not work at all. Not only does it not send the email with the attachment, the email doesn't seem to send at all, even though my script says that it did send. Where have I gone wrong? I'm not using a form to upload a file. This is a static script.
<?php
$EmailTo = "Me#here.com";
$EmailFrom = "You#There.com";
$EmailSubject = "The Email Subject";
$MailBoundary = md5(uniqid(time()));
$Headers = "To: ". $EmailTo . "\r\n";
$Headers .= "From: ". $EmailFrom . "\r\n";
$Headers = "MIME-Version: 1.0\r\n";
$Headers .= "Content-type: multipart/mixed;boundary=\"$MailBoundary \"";
$Headers .= "\r\n\r\n";
$Headers .= "This is a multi-part message in MIME format.";
$Headers .= "\r\n\r\n";
$FileAttachment = "AttachedFile.pdf";
$File = fopen($FileAttachment, "r");
$FileData = fread($File, filesize($FileAttachment));
$FileData = chunk_split(base64_encode($FileData));
$FileName = basename($FileAttachment);
$EmailBody = "--$MailBoundary\r\n";
$EmailBody .= "Content-type: text/html; charset=iso-8859-1\r\n";
$EmailBody .= "Content-transfer-encoding: 8bit\r\n\r\n";
$EmailBody .= "<html>" . chr(13) .
"<head>" . chr(13) .
"<style>" . chr(13) .
".breg {font-family:arial;font-size:10pt;color:#000000;padding:5px;}" . chr(13) .
"</style>" . chr(13) .
"</head>" . chr(13) .
"<body>" . chr(13) .
"<div class=" . chr(34) . "breg" . chr(34) . ">" . chr(13) .
"The message text body goes here" . chr(13) .
"</div>" . chr(13) .
"</body>" . chr(13) .
"</html>";
$EmailBody .= "--$MailBoundary\r\n";
$EmailBody .= "Content-type: " . mime_content_type($File) . "; name=$FileName\r\n";
$EmailBody .= "Content-transfer-encoding:base64\r\n\r\n";
$EmailBody .= $FileData. "\r\n\r\n";
$EmailBody .= " --$MailBoundary--";
if (mail($EmailTo, $EmailSubject, $EmailBody, $Headers))
{
echo "Email to " . $EmailTo . " has been sent" . chr(13) . "<br />" . chr(13);
}
else
{
echo "<b>Email to " . $EmailTo . " was not sent</b>" . chr(13) . "<br />" . chr(13);
}
?>
For sending mail with attachment using php mail().Try this code:
<?php
//If there is no error, send the email
if(isset($_POST['ur_submit_button_name'])) {
$EmailTo = "Me#here.com";
$EmailFrom = "You#There.com";
$EmailSubject = "The Email Subject";
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "ip.zip";//store that zip file in ur root directory
$attachment = chunk_split(base64_encode(file_get_contents('ip.zip')));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// send message
if (mail($to, $subject, $body, $headers)) {
$mail_sent=true;
echo "mail sent";
} else {
$mail_sent=false;
echo "Error,Mail not sent";
}
}
?>
i think you have an error in your email content,
generate your email content with imap_mail_compose function:
mail('', 'message subject', '', imap_mail_compose(array header, array body));