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

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.

Related

reply to sender mail php script

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.

Sent email with iCal to outlook with valarm reminder

I am in a situation where I want to send email with iCal attachment to Outlook Calendar and I want to set reminder to 120 minutes before start event.
I send RAW message with iCal(see below).
If recipient opens the message, event is automatically added to outlook calendar, but reminder is set to 15 minutes(default in outlook), even if I set different value in VALARM.
If I use ics file with iCal to import to Outlook Calendar then reminder is set to my value.
I would really appreciate help on this. How to set reminder for event?
$from_name = "<FROM_NAME>";
$from_address = "<FROM_ADDRESS>";
$subject = "Meeting Booking";
$meeting_description = "Here is a brief description of my meeting\n\n";
$meeting_location = "My Office";
$domain = 'domain.com';
$to_name = "<TO_NAME>";
$to_address = "<TO_ADDRESS>";
$meeting_date = "2015-10-21 15:40:00";
$meeting_duration = 3600;
$meeting_stamp = STRTOTIME($meeting_date . " UTC");
$dtstart= GMDATE("Ymd\THis\Z",$meeting_stamp);
$dtend= GMDATE("Ymd\THis\Z",$meeting_stamp+$meeting_duration);
$mime_boundary = "----Meeting Booking----".MD5(TIME());
//Create ICAL Content
$ical = 'BEGIN:VCALENDAR' . "\r\n" .
'PRODID:-//Patient Portal//MyEyeDr.//EN' . "\r\n" .
'VERSION:2.0' . "\r\n" .
'METHOD:REQUEST' . "\r\n" .
'BEGIN:VEVENT' . "\r\n" .
'ORGANIZER;CN="'.$from_name.'":MAILTO:'.$from_address. "\r\n" .
'ATTENDEE;CN="'.$to_name.'";ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:'.$to_address. "\r\n" .
'LAST-MODIFIED:' . date("Ymd\TGis") . "\r\n" .
'UID:'.date("Ymd\TGis", strtotime($meeting_date)).rand()."#".$domain."\r\n" .
'DTSTAMP:'.date("Ymd\TGis"). "\r\n" .
'DTSTART;TZID="America/New_York":'.$dtstart. "\r\n" .
'DTEND;TZID="EAmerica/New_York":'.$dtend. "\r\n" .
'SEQUENCE:1'. "\r\n" .
'SUMMARY:' . $subject . "\r\n" .
'LOCATION:' . $meeting_location . "\r\n" .
'CLASS:PUBLIC'. "\r\n" .
'PRIORITY:5'. "\r\n" .
'BEGIN:VALARM'. "\r\n" .
'ACTION:Display'. "\r\n" .
'DESCRIPTION:'.$meeting_description. "\r\n" .
'SUMMARY:Event Alarm'. "\r\n" .
'TRIGGER:-PT120M'. "\r\n" .
'END:VALARM'. "\r\n" .
'END:VEVENT'. "\r\n" .
'END:VCALENDAR'. "\r\n";
$message= "To: ".$to_address."\n";
$message.= "From: ".$from_address."\n";
$message.= "Subject: Example SES mail (raw)\n";
$message.= "MIME-Version: 1.0\n";
$message .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
$message .= "Content-class: urn:content-classes:calendarmessage\n";
$message.= "\n\n";
$message .= "--$mime_boundary\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";
$message .= "<html>\n";
$message .= "<body>\n";
$message .= '<p>Dear ...,</p>';
$message .= '<p>This is an email message.</p>';
$message .= "</body>\n";
$message .= "</html>\n";
$message .= "--$mime_boundary\r\n";
$message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST'."\n";
$message .= "Content-Transfer-Encoding: 8bit\n\n";
$message .= "X-Mailer: Microsoft Office Outlook 15.0\r\n";
$message .= $ical;
I think there is nothing wrong with your code. Most calendar clients will ignore any alarm that is sent along with an invitation. When you think about it, this makes sense: if you invite me, I may want to accept or decline, but you should not dictate when I want to be notified.
On the other hand, when importing, you are making those events your own.
Change METHOD:REQUEST to METHOD:PUBLISH. Also change your other method code at the bottom from method=REQUEST to method=PUBLISH.

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 sending attachment the can't be opened

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:|

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