strange text in gmail inbox - php issue? - php

I have a mailer code which worked fine until a couple days when the received email instead of normal email came as a code to Gmail... like that:
--PHP-mixed-a3bf01bb0d28e41880e43720dfc8860d
Content-Type: multipart/alternative; boundary="PHP-alt-a3bf01bb0d28e41880e43720dfc8860d"
--PHP-alt-a3bf01bb0d28e41880e43720dfc8860d
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Type here...
--PHP-alt-a3bf01bb0d28e41880e43720dfc8860d
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<div style="width:900px"><br /><p>
Type here...</p>
--PHP-alt-a3bf01bb0d28e41880e43720dfc8860d--
--PHP-mixed-a3bf01bb0d28e41880e43720dfc8860d
Content-Type: ; name=""
Content-Transfer-Encoding: base64
Content-Disposition: attachment
--PHP-mixed-a3bf01bb0d28e41880e43720dfc8860d--
any idea where to start to track it back. As far as I know I haven't chaned anything on the code at all.
Dummy question but I have no idea where to start... any comment would be welcome and I am willing to learn. Thank you for your time...
update-
Here is the code which produces the code above:
mail.php
<?php
function csatlakozas($szerver, $user, $pw)
{ // evvel meg csatlakozom az adatbázishoz, hogy ki tudjam olvasni a címzetteket
if (!$con = mysql_connect($szerver, $user, $pw))
{
die('Could not connect to SQL DB: ' .mysql_error().'<br/>Try it later AND please report to your system administrator!');
}
};
//belepes a db-be
csatlakozas("localhost","use","pass");
mysql_select_db("db");
//atveszem a csoport neveket es elszaladok mailozni csoportonkent...
$result=$_POST['to-k'];
$datum=date("Y-m-d");
$subject = $_POST["subject"];
$head="<div style=\"width:900px\"><br />";
$messagetext = $_POST["message"];
$foot="</div>";
$message = $head.$messagetext.$foot;
echo "<p>Original message</p>";
echo $message;
echo "<p>Mass mail has been sending to:</p>";
//kezdodik a csoportok kiolvasasa...
foreach($result as $value)
{
echo "<h3>".$value."</h3>";
// kiolvasom a cimzetteket az adott kategoriaban
$sql="SELECT * FROM emails WHERE sendesstat=1 AND deleted=0 AND classhoz=\"".$value."\"";
//echo $sql;
$query=mysql_query($sql);
// kuldjuk az emailokat
while($data=mysql_fetch_array($query))
{
//define the receiver of the email
$to = $data["firstname"]." ".$data["surname"]." <".$data["email"].">"; // ******* itt a form adatai
$from = "my ltd <admin#myweb.co.uk>"; // ******* itt a form adatai
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: ".$from."\r\nReply-To: ".$from; // ******* itt a form adatai
//add boundary string and mime type specification
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
// ************************************
// itt kellett belenyúlni
// ************************************
$fname=$_FILES["fileatt"]["name"];
$filename=$_FILES["fileatt"]["tmp_name"];
$filetype=$_FILES["fileatt"]["type"];
$attachment=chunk_split(base64_encode(file_get_contents($filename)));
// ************************************
// eddig
// ************************************
//define the body of the message.
include 'sendmail.php';
}
}
?>
sendmail.php:
<?php
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<?php echo strip_tags($message); ?>
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<?php echo $message; ?>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: <?php echo $filetype;?>; name="<?php echo $fname;?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$msg = ob_get_clean();
//send the email
//$mail_sent = 0;
$mail_sent = #mail($to, $subject, $msg, $headers, " -f admin#mydomai.co.uk" );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent to ".$to."<br />" : "Mail failed to ".$to."<br />";
?>
I hope there is a good solution as haven't found anything yet on google which would make life easy about this:(

Related

Send mail in php script with attachment on Ubuntu server [duplicate]

This question already has answers here:
How can I send an email using PHP?
(20 answers)
Closed 6 years ago.
I want to send a mail with attachments using php script on Ubuntu. I have tried mutt, mailx but they all work at local terminal when I execute them using shell_exec() they arr not working. Plz help me
Ps :- I am beginner dnt have much knowledge about owner / permission
I am sure, you didn't google it. There are a number of APIs that do what you need.
<?php
//define the receiver of the email
$to = 'youraddress#example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

Emailing attachments with SMTP Relay

I currently have one server relaying emails to one main mail server, but when I try to mail something with an attachment, it comes up as:
Content-type:text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Email message
Content-Type: application/octet-stream; name="test.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.pdf"
Is there anyway to fix this?
try this
<?php
//define the receiver of the email
$to = 'youraddress#example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Rolling your own MIME encoding in PHP, as lemirage suggests, will work. But it may be simpler to use phpmailer. phpmailer is easy to use for sending messages with attachments, and simple to setup - just a few PHP files to copy to your server. See https://github.com/PHPMailer/PHPMailer

sending completed PDF as email attachment with field values

I have an online PDF form which a user completes and then clicks a submit button which sends the form to an email address as an attachment and also stores the data in a database. However the completed individual field values do not appear when sent to an email address. How can I achieve this? many thanks.
//define the receiver of the email
$to = 'test#*****.com';
//define the subject of the email
$subject = 'Completed PDF form';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: noreply#test.com\r\nReply-To: noreply#test.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('purchase_order_form.pdf')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="purchase_order_form.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
Here is your example using Swiftmailer, you will see that it's really easier !
require_once '/path/to/swift-mailer/lib/swift_required.php';
// Create the Transport
$transport = Swift_MailTransport::newInstance();
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$message = Swift_Message::newInstance()
// Give the message a subject
->setSubject($subject)
// Set the From address with an associative array
->setFrom(array('noreply#test.com'))
// Set the To addresses with an associative array
->setTo(array($to))
// Give it a body
->setBody($message)
// Optionally add any attachments
->attach(Swift_Attachment::fromPath('purchase_order_form.pdf'))
;
// Send the message
$result = $mailer->send($message);
echo $mail_sent ? "Mail sent" : "Mail failed";

Emailing a dynamically created image

I need to create an image dynamically then email it to the person. Here is the image creation script:
//Get the blank coupon
$rImg = ImageCreateFromJPEG('coupon.jpg');
$color = imagecolorallocate($rImg, 0, 0, 0);
imagestring($rImg,5,135,140,'$first_name', 'black');
imagestring($rImg,5,135,160,'$last_name', 'black');
imagestring($rImg,5,135,180,'$email_address', 'black');
From there I want to take that image and email it to the person. Can I just include it in attachment of the email or do I actually have to write it to the server?
I have tried doing this:
function email_coupon($first_name, $last_name, $email_address){
//Get the blank coupon
$rImg = ImageCreateFromJPEG('coupon.jpg');
$color = imagecolorallocate($rImg, 0, 0, 0);
imagestring($rImg,5,135,140,'$first_name', 'black');
imagestring($rImg,5,135,160,'$last_name', 'black');
imagestring($rImg,5,135,180,'$email_address', 'black');
//define the receiver of the email
$to = '$email_address';
//define the subject of the email
$subject = 'Your coupon';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: joe#joesomebody.com\r\nReply-To: joe#joesomeboy.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
ob_start();
imagejpeg($rImg);
$i = ob_get_clean();
$attachment = chunk_split(base64_encode($i));
//define the body of the message.
ob_clean();
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: image/jpeg; name="coupon.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $email_address, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
if($mail_sent == true) {
echo 'sent';
} else {
echo 'failed';
};
};
Anyone have any ideas?
Thanks in advance.
You don't need to write anything to your server/a file, but imagejpeg with one argument will print out the image to the browser automatically. To get it in a variable, use ob_start and ob_get_contents:
ob_start();
imagejpeg($rImg);
$i = ob_get_contents();
$attachment = chunk_split(base64_encode($i));
// clear the buffer, but don't destroy it
ob_clean();
// echo out mail like usual
echo("Mail body here");
$message = ob_get_clean();
http://www.php.net/manual/en/ref.outcontrol.php
For your code:
function email_coupon($first_name, $last_name, $email_address){
//Get the blank coupon
$rImg = ImageCreateFromJPEG('coupon.jpg');
$color = imagecolorallocate($rImg, 0, 0, 0);
imagestring($rImg,5,135,140,'$first_name', 'black');
imagestring($rImg,5,135,160,'$last_name', 'black');
imagestring($rImg,5,135,180,'$email_address', 'black');
//define the receiver of the email
$to = '$email_address';
//define the subject of the email
$subject = 'Your coupon';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: joe#joesomebody.com\r\nReply-To: joe#joesomeboy.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
ob_start();
imagejpeg($rImg);
$i = ob_get_contents();
$attachment = chunk_split(base64_encode($i));
//define the body of the message.
ob_clean();
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: image/jpeg; name="coupon.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $email_address, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
if($mail_sent == true) {
echo 'sent';
} else {
echo 'failed';
};

Can you send a file from a webpage via e-mail

Ironically, for an IT contractor, I find myself building a website for a recruitment consultancy.
They would like a form which allows users to send them a CV but, from a security point of view would like the CV's sent to an e-mail address rather than stored on a web server. Does anyone know if such a thing is possible with jquery forms plugin or some other type of script?
No luck so far with google etc.
Thanks in Advance.
This page explain everything from file upload to sending email with attachment
<?php
//define the receiver of the email
$to = 'youraddress#example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Let the upload proceed as normal into a temp folder on the web server, then email that file where it needs to go and then delete it.
Otherwise, it's just tell the user to email in their CV. I can't think of any way of redirecting the file stream into a mail message directly.

Categories