Email attachment works fine in gmail but not on my mailserver - email-attachments

Using PHP i am sending email attachment on both my gmail and mailserver id(company email id). I am getting email in both of my email account but on my company email id my email attachment shows error(it doesn't open properly) whereas in gmail it is fine.
Any ideas or suggestion??
I am pasting my php code here. any help would be highly appreciated.
HTML CODE:
<form action="contact.php" method="post" name="form1" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="fileAttach" id="file"><br><br>
<input type="submit" name="submit" value="Submit" align="right">
</form>
contact.php:
<?php
if($_POST['submit'])
{
$strTo = "mymail#company.com, mymail#gmail.com";
$strSubject = "Attachment file";
$strMessage = "Attachment";
$txtFormEmail = "mymail#gmail.com";
$strSid = md5(uniqid(time()));
$strHeader = "";
$strHeader .= "From: "."<".$txtFormEmail.">\nReply-To: ".$txtFormEmail."";
$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";
//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
$strFilesName = $_FILES["fileAttach"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$strHeader .= $strContent."\n\n";
}
$flgSend = mail($strTo,$strSubject,$strMessage,$strHeader);
}
?>

You can use PHP Mailer Class to send mails with attachments.

Related

PHP mail opens in Gmail but not in Outlook

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

PHP Form fields will not send to email

I have created a send to email PHP script.
The form seems to work correctly when sending only one field (the message field) but as soon as other fields are added, the form ceases to be emailed on to the inbox (yet the form still fires correctly and reroutes to the thankyou just fine.)
Here is the code I currently have, this does not work
<?php
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$services = $_REQUEST['services'] ;
$message = $_REQUEST['message'] ;
if (!isset($_REQUEST['email'])) {
header( "Location: feedback.html" );
}
elseif (empty($email) || empty($message)) {
header( "Location: error.html" );
}
else {
mail( "info#website.co.uk", "Message via your website!",
$name, $services, $message, "From: $email" );
header( "Location: thankyou.html" );
}
?>
This is the previous code, this does work, but only displays the message
<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
if (!isset($_REQUEST['email'])) {
header( "Location: feedback.html" );
}
elseif (empty($email) || empty($message)) {
header( "Location: error.html" );
}
else {
mail( "info#website.co.uk", "Message via your website!",
$message, "From: $email" );
header( "Location: thankyou.html" );
}
?>
This is the HTML for the form:
<form method="post" action="sendmail.php">
<label>Name:</label> <input name="name" type="text" /><br />
<label>Email:</label> <input name="email" type="text" /><br />
<label>What service do you require?:</label> <input name="services" type="text" /><br />
<label>Message:</label><br />
<textarea name="message" rows="15" cols="40">
</textarea><br />
<input type="submit" />
</form>
As per the mail() docs the message body should be passed as a single parameter. So it should look something more like:
mail( "info#website.co.uk", "Message via your website!", "$name\r\n $services\r\n $message", "From: $email");
According to php.net the correct use of the mail() function is:
<?
$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";
// Mail it
mail($to, $subject, $message, $headers);
?>
You are trying to send the additional data ($services) as a header.
Try to merge your $message and $service >
$message = $service . "\r\n\r\n" . $message;
mail( "info#website.co.uk", "Message via your website!",
$message, "From: $email" );
You can't just add more and more variables to mail(). There is only one parameter that specifies the content of the email, which would be the third one, as you can see in the official documentation:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
If you want to have more than just $message in your mail, you need to put everything into one variable to be given to the function as content.
$content = $message;
$content .= "\n" . $services;
// etc...
mail($to, $subject, $content);
Try this
<?php
$email = $_POST['email'] ;
$name = $_POST['name'] ;
$services = $_POST['services'] ;
$message = $_POST['message'] ;
if (!isset($_POST['email'])) {
header( "Location: feedback.html" );
}
elseif (empty($email) || empty($message)) {
header( "Location: error.html" );
}
else {
mail( "info#website.co.uk", "Message via your website!",
$name . $services. $message . "From: $email" );
header( "Location: thankyou.html" );
}
?>

Mail Sending in TCPDF

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";
}

php mail with PDF attachment header causes to go into just mail

I have a webform that generates a PDF which is emailed to me upon submission but the email keeps getting filtered to the spam for folder. I suspect that the something is wrong in the email header but not sure what it could be.
Any help is appreciated.
The following is a snippet...
// send by email
$fileatt = '../pdfs/'.$filename;
$fileatt_type = mime_content_type_manual($fileatt); // File Type "application/pdf"
$fileatt_name = $filename; // Filename that will be used for the file as the attachment
$email_from = "auth#mysite.com"; // Who the email is from
$email_subject = "Auth Report"; // The Subject of the email
//$email_to = "info#mysite.com";
$email_to = "info#mysite.com";
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
If you add the sender of the email, which is auth#mysite.com for your case, as a new contact to the address book of your email client, then the email won't probably be marked as spam as it comes from a member of your contact address book.

PHP Sending Emails with File Attachments - Email Not Sending At All

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