**Update: Updated the code and added the email contents as seen in Gmail. **
I have a problem which I've been trying to solve a couple of days. But since I have been unable to make any progress, I'd like the help of someone else. I'm trying to send a zip file from my server to my email. However, the email is being sent empty. This is the code:
`$headers = "From: $from";
/* Generate boundary string */
$random = md5(time());
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random."\"";
/* Read the file */
$attachment = chunk_split(base64_encode(file_get_contents("backup-$date-$time-9.zip")));
/* Define body */
$message = "--PHP-mixed-$random
Content-Type: text/plain; charset=\"iso-8859-1\"
Attached, please find your backup file.
--PHP-mixed-$random
Content-Type: application/zip; name=backup-$date-$time-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random--"; // no intendation
$mail = mail($email, $subject, $message, $headers);
echo $mail ? "Email sent<br>" : "Email sending failed<br>";`
Thanks to all reading and wanting to help.
The resulting email as seen in Gmail is
Attached, please find your backup file.
--PHP-mixed-e44b531b0e0538185289abc521eda78d
Content-Type: application/zip; name=backup-29-11-2014-1417275285-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment
UEsDBBQAAAAIAFZ8fUUs...sUEsFBgAAAAACAAIAeAAAAD4B
AAAAAA==
--PHP-mixed-e44b531b0e0538185289abc521eda78d--
I got it working by doing the following changes:
Removed space character in From: headers, it's an invalid header otherwise
Changed boundary: to boundary=
Removed intendation in $message because that seems to break the email. The boundary string --PHP-mixed-random separates the different parts of the email, like plain text, html text, attachments from each other. When there are spaces in front of the boundary separator, it is not interpreted as the separator anymore, but as normal text.
Here's the code:
<?php
$from = "xxx#myserver";
$email = "xxx#gmail.com";
$subject = "backup file";
$date = "29-11-2014";
$time = "1417275285"; // test file "backup-29-11-2014-1417275285-9.zip" in the same folder
$headers = "From: $from"; // remove space
/* Generate boundary string */
$random = md5(time());
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random."\""; // : to =
/* Read the file */
$attachment = chunk_split(base64_encode(file_get_contents("backup-$date-$time-9.zip")));
/* Define body */
$message = "--PHP-mixed-$random
Content-Type: text/plain; charset=\"iso-8859-1\"
Attached, please find your backup file.
--PHP-mixed-$random
Content-Type: application/zip; name=backup-$date-$time-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random--"; // no intendation
$mail = mail($email, $subject, $message, $headers);
echo $mail ? "Email sent<br>" : "Email sending failed<br>";
Related
When I run this I get the following error: Parse error: syntax error, unexpected end of file in W:\xampp\htdocs___\phpmail.php on line 85
<?php
// Setting a timezone, mail() uses this.
date_default_timezone_set('America/New_York');
// recipients
$to = "you#phpeveryday.com" . ", " ; // note the comma
$to .= "we#phpeveryday.com";
// subject
$subject = "Test for Embedded Image & Attachement";
// Create a boundary string. It needs to be unique
$sep = sha1(date('r', time()));
// Add in our content boundary, and mime type specification:
$headers .=
"\r\nContent-Type: multipart/mixed;
boundary=\"PHP-mixed-{$sep}\"";
// Read in our file attachment
$attachment = file_get_contents('attachment.zip');
$encoded = base64_encode($attachment);
$attached = chunk_split($encoded);
// additional headers
$headers .= "To: You <you#phpeveryday.com>,
We <we#phpeveryday.com>\r\n";
$headers .= "From: Me <me#phpeveryday.com>\r\n";
$headers .= "Cc: he#phpeveryday.com\r\n";
$headers .= "Bcc: she#phpeveryday.com\r\n";
$inline = chunk_split(base64_encode(
file_get_contents('mypicture.gif')));
// Your message here:
$body =<<<EOBODY
--PHP-mixed-{$sep}
Content-Type: multipart/alternative;
boundary="PHP-alt-{$sep}"
--PHP-alt-{$sep}
Content-Type: text/plain
Hai, It's me!
--PHP-alt-{$sep}
Content-Type: multipart/related; boundary="PHP-related-{$sep}"
,
--PHP-alt-{$sep}
Content-Type: text/html
<html>
<head>
<title>Test HTML Mail</title>
</head>
<body>
<font color='red'>Hai, it is me!</font>
Here is my picture:
<img src="cid:PHP-CID-{$sep}" />
</body>
</html>
--PHP-related-{$sep}
Content-Type: image/gif
Content-Transfer-Encoding: base64
Content-ID: <PHP-CID-{$sep}>
{$inline}
--PHP-related-{$sep}--
--PHP-alt-{$sep}--
--PHP-mixed-{$sep}
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
{$attached}
--PHP-mixed-{$sep}--
EOBODY;
// Finally, send the email
mail($to, $subject, $body, $headers);
?>
Why Is it that i get this error? I got this code from a tutorial here: www.phpeveryday.com/articles/PHP-Email-Using-Embedded-Images-in-HTML-Email-P113.html
Because your mail function is still in EOBODY.
Also please try to find another solution, or create your own mailer, because i've just read some comments in this page:
I think this must be one of the worst tutorials ever, doesn't work
this is a stupid shit.
u idiot
etc... and i agree.
There are much more good tutorial like this. Use PHPMailer.
Am facing a problem to sent a PHP email with an excel attachment. I can see the mail sent message, Mail is coming, but there is no attachment. I dint know the real problem. this is my code. I have put the User.xlsx file in my root directory. Please help me to solve this issue. Thanks
$to = "dibeesh#amt.in";
$subject="attachement";
$mail_msg="message with attach";
$filename="User.xlsx"; // Attachement file in root directory
$contentType="application/zip"; // Not sure about what to put here
$pathToFilename="./";
$random_hash = md5(date('r', time()));
$headers = "From: webmaster#mysite.com\r\nReply-To: ".$to;
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($pathToFilename)));
ob_start();
echo "
--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"
--PHP-alt-$random_hash
Content-Type: text/plain; charset=\"utf-8\"
Content-Transfer-Encoding: 7bit
$mail_msg
--PHP-alt-$random_hash--
--PHP-mixed-$random_hash
Content-Type: $contentType; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--
";
$message = ob_get_clean();
// $fh=fopen('log.txt','w');
// fwrite($fh,$message);
$mail_sent = #mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
I have managed to debug my code, sorry for duplicate posting. It may help someone thanks
// Email to Client with attachement
//define the receiver of the email
$to = 'dibeesh#amt.in';
//define the subject of the email
$subject = 'Bayern3 Notification';
$filename = "User.xlsx";
//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: lal#amt.in\r\nReply-To: dibeesh#amt.in";
//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($filename)));
//define the body of the message.
ob_start(); //Turn on output buffering
echo "--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"
--PHP-alt-$random_hash
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
--PHP-alt-$random_hash
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
<h4>Hi,</h4>
<p><b>$userfullname</b> and his 10 friends qualified for <b>$venuename</b>. Please find the Excel sheet attached. You can download the detailed report form Bayern3 Admin Panel</p>
<h4>Regards,</h4>
<h4>Bayern3 Team</h4>
--PHP-alt-$random_hash--
--PHP-mixed-$random_hash
Content-Type: application/zip; name=$filename
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--";
//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";
if($mail_sent === True)
{
//echo "Mail Sent";
}
else{
//echo "Mail Failed";
}
I have a PHP script which sends an HTML email with an attached image. It works beauifully, however, I can't get the attachment to display in an <img> tag in the email body. The attached file is called postcard.png and the original filename on the server is 4e60348f83f2f.png. I've tried giving the image URL as various things: cid:postcard.png, cid:4e60348f83f2f.png, postcard.png, and 4e60348f83f2f.png. Nothing works.
I think the key part that I'm doing wrong is here, because this makes it a separated attachment instead of an inline attachment that I can use:
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="$fname" // i.e.: "postcard.png"
I've tried changing it to use a CID but I don't really know how to do that, and this didnt' work at all:
Content-Transfer-Encoding: base64
Content-ID: <$fname> // i.e.: postcard.png
Here's the full code: (It's based on this code from a comment in the php mail() page.)
<?php
$to = "recipient#email.com";
$email = "sender#email.com";
$name = "Namename";
$subject = "An inline image!";
$comment = "Llookout <b>Llary</b> it's <br> the <b>Ll</b>andllord!<br><img src='cid:postcard.png'><br><img src='cid:4e60348f83f2f.png'><img src='postcard.png'><br><img src='4e60348f83f2f.png'>";
$To = strip_tags($to);
$TextMessage =strip_tags(nl2br($comment),"<br>");
$HTMLMessage =nl2br($comment);
$FromName =strip_tags($name);
$FromEmail =strip_tags($email);
$Subject =strip_tags($subject);
$boundary1 =rand(0,9)."-"
.rand(10000000000,9999999999)."-"
.rand(10000000000,9999999999)."=:"
.rand(10000,99999);
$boundary2 =rand(0,9)."-".rand(10000000000,9999999999)."-"
.rand(10000000000,9999999999)."=:"
.rand(10000,99999);
$filename1 = "4e60348f83f2f.png"; //name of file on server with script
$handle =fopen($filename1, 'rb');
$f_contents =fread($handle, filesize($filename1));
$attachment=chunk_split(base64_encode($f_contents));
fclose($handle);
$ftype ="image/png";
$fname ="postcard.png"; //what the file will be named
$attachments='';
$Headers =<<<AKAM
From: $FromName <$FromEmail>
Reply-To: $FromEmail
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="$boundary1"
AKAM;
$attachments.=<<<ATTA
--$boundary1
Content-Type: $ftype;
name="$fname"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="$fname"
$attachment
ATTA;
$Body =<<<AKAM
This is a multi-part message in MIME format.
--$boundary1
Content-Type: multipart/alternative;
boundary="$boundary2"
--$boundary2
Content-Type: text/plain;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$TextMessage
--$boundary2
Content-Type: text/html;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$HTMLMessage
--$boundary2--
$attachments
--$boundary1--
AKAM;
// Send email
$ok=mail($To, $Subject, $Body, $Headers);
echo $ok?"<h1> Mail sent!</h1>":"<h1> Mail not sent!</h1>";
?>
I finally found the answer, which turns out to be remarkably simple. This page is what helped me figure it out, but I'll demonstrate the parts that I needed to get it done below.
First off, there's the creation of the boundary string, and the image, correctly encoded and chunked:
// Create a boundary string. It needs to be unique (not in the text) so ...
// We are going to use the sha1 algorithm to generate a 40 character string:
$sep = sha1(date('r', time()));
// Also now prepare our inline image - Also read, encode, split:
$inline = chunk_split(base64_encode(file_get_contents('figure.gif')));
In the HTML part of the email, the image is referenced like this (using the boundary string):
<img src="cid:PHP-CID-{$sep}">
Then you create another part of the email below the HTML part for the inline attachment, like this:
--PHP-related-{$sep}
Content-Type: image/gif
Content-Transfer-Encoding: base64
Content-ID: <PHP-CID-{$sep}>
{$inline}
...and that is that! Easier than implementing PHPmailer or any of the other libraries, if this is all you're doing. No doubt for more complicated task, you'll want to get one of those libraries.
I would recommend using PHPMailer but since you say you don't want to I suggest this quick fix.
Put the absolute URL inside an img tag, if you do send an HTML email, it will work.
<img src="http://example.com/image.jpg"/>
I have a Problem, I would like to send a File (Photoshop, .gif etc.) File via Email with PHP.
I POST the Data from the HTML Document to the following PHP File.
The Problem: I receive the Email but the File is corrupt.
Any Idea why this doesn´t work??
$to = 'admin#example.com';
$subject = 'Order';
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$plz = strip_tags($_POST['plz']);
$city = strip_tags($_POST['city']);
$street = strip_tags($_POST['street']);
$nr = strip_tags($_POST['nr']);
$plzdelivery = strip_tags($_POST['plz-delivery']);
$citydelivery = strip_tags($_POST['city-delivery']);
$streetdelivery = strip_tags($_POST['street-delivery']);
$nrdelivery = strip_tags($_POST['nr-delivery']);
$buttonsize = strip_tags($_POST['button-size']);
$count = strip_tags($_POST['count']);
$message = "Name: ".$name." Email: ".$email." Plz: ".$plz." Stadt: ".$city." Strasse: ".$street." Hnr: ".$nr." Button: ".$buttonsize." Anzahl: ".$count;
$type = $_FILES['file']['type'];
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];
$boundary =md5(date('r', time()));
$headers = "From: webmaster#example.com\r\nReply-To: webmaster#example.com";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$message
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to, $subject, $message, $headers);
Short answer: Don't even try.
PHP's mail() function is very very short on features. If you're even considering trying to send attachments using it, you are in for a lot of frustration and wasted time.
The best answer I can give you is to use a third party library. There are several good ones available. Why waste weeks of your time writing something that isn't going to be good enough when others have already spent years doing it for you and getting it right.
My suggestion would be phpMailer, but several others exist as well.
You should install the pear Mail_Mime package, it will allow you to easily attach a file.
http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php
I have a PHP script which sends an HTML email with an attached image. It works beauifully, however, I can't get the attachment to display in an <img> tag in the email body. The attached file is called postcard.png and the original filename on the server is 4e60348f83f2f.png. I've tried giving the image URL as various things: cid:postcard.png, cid:4e60348f83f2f.png, postcard.png, and 4e60348f83f2f.png. Nothing works.
I think the key part that I'm doing wrong is here, because this makes it a separated attachment instead of an inline attachment that I can use:
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="$fname" // i.e.: "postcard.png"
I've tried changing it to use a CID but I don't really know how to do that, and this didnt' work at all:
Content-Transfer-Encoding: base64
Content-ID: <$fname> // i.e.: postcard.png
Here's the full code: (It's based on this code from a comment in the php mail() page.)
<?php
$to = "recipient#email.com";
$email = "sender#email.com";
$name = "Namename";
$subject = "An inline image!";
$comment = "Llookout <b>Llary</b> it's <br> the <b>Ll</b>andllord!<br><img src='cid:postcard.png'><br><img src='cid:4e60348f83f2f.png'><img src='postcard.png'><br><img src='4e60348f83f2f.png'>";
$To = strip_tags($to);
$TextMessage =strip_tags(nl2br($comment),"<br>");
$HTMLMessage =nl2br($comment);
$FromName =strip_tags($name);
$FromEmail =strip_tags($email);
$Subject =strip_tags($subject);
$boundary1 =rand(0,9)."-"
.rand(10000000000,9999999999)."-"
.rand(10000000000,9999999999)."=:"
.rand(10000,99999);
$boundary2 =rand(0,9)."-".rand(10000000000,9999999999)."-"
.rand(10000000000,9999999999)."=:"
.rand(10000,99999);
$filename1 = "4e60348f83f2f.png"; //name of file on server with script
$handle =fopen($filename1, 'rb');
$f_contents =fread($handle, filesize($filename1));
$attachment=chunk_split(base64_encode($f_contents));
fclose($handle);
$ftype ="image/png";
$fname ="postcard.png"; //what the file will be named
$attachments='';
$Headers =<<<AKAM
From: $FromName <$FromEmail>
Reply-To: $FromEmail
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="$boundary1"
AKAM;
$attachments.=<<<ATTA
--$boundary1
Content-Type: $ftype;
name="$fname"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="$fname"
$attachment
ATTA;
$Body =<<<AKAM
This is a multi-part message in MIME format.
--$boundary1
Content-Type: multipart/alternative;
boundary="$boundary2"
--$boundary2
Content-Type: text/plain;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$TextMessage
--$boundary2
Content-Type: text/html;
charset="windows-1256"
Content-Transfer-Encoding: quoted-printable
$HTMLMessage
--$boundary2--
$attachments
--$boundary1--
AKAM;
// Send email
$ok=mail($To, $Subject, $Body, $Headers);
echo $ok?"<h1> Mail sent!</h1>":"<h1> Mail not sent!</h1>";
?>
I finally found the answer, which turns out to be remarkably simple. This page is what helped me figure it out, but I'll demonstrate the parts that I needed to get it done below.
First off, there's the creation of the boundary string, and the image, correctly encoded and chunked:
// Create a boundary string. It needs to be unique (not in the text) so ...
// We are going to use the sha1 algorithm to generate a 40 character string:
$sep = sha1(date('r', time()));
// Also now prepare our inline image - Also read, encode, split:
$inline = chunk_split(base64_encode(file_get_contents('figure.gif')));
In the HTML part of the email, the image is referenced like this (using the boundary string):
<img src="cid:PHP-CID-{$sep}">
Then you create another part of the email below the HTML part for the inline attachment, like this:
--PHP-related-{$sep}
Content-Type: image/gif
Content-Transfer-Encoding: base64
Content-ID: <PHP-CID-{$sep}>
{$inline}
...and that is that! Easier than implementing PHPmailer or any of the other libraries, if this is all you're doing. No doubt for more complicated task, you'll want to get one of those libraries.
I would recommend using PHPMailer but since you say you don't want to I suggest this quick fix.
Put the absolute URL inside an img tag, if you do send an HTML email, it will work.
<img src="http://example.com/image.jpg"/>