I'm using a pretty standard e-mail creating function which I've used before, but it doesn't work for some reason I can't figure out. It sends a blank e-mail no matter what content I put inside it.
function sendToUser($email,$admin_email,$subject,$content){
$to=$email;
$random_hash = md5(date('r', time()));
$headers = "From: Site Name <$admin_email>";
$headers .= "\r\nReply-To: $admin_email";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
<?php echo $content; ?>
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
mail($to, $subject, $message, $headers);
}
Now, if I call it like this:
sendToUser("myprivatemail#yahoo.com","admin#site.com","Testing","E-mail content");
It sends the e-mail, but it arrives empty. Does anyone see what's wrong here? Or could it be some server setting I'm unfamiliar with?
--PHP-alt-<?php echo $random_hash; cannot have whitespace before it.
Try this:
function sendToUser($email,$admin_email,$subject,$content){
$to=$email;
$random_hash = md5(date('r', time()));
$headers = "From: Site Name <$admin_email>";
$headers .= "\r\nReply-To: $admin_email";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
<?php echo $content; ?>
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
mail($to, $subject, $message, $headers);
}
Related
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";
?>
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
I am trying to email a text file as an attachment from a PHP script using the code from here: http://webcheatsheet.com/php/send_email_text_html_attachment.php#attachment
<?
$subject = 'Requested File';
$random_hash = md5(date('r', time()));
$headers = "From: email#email.com\r\nReply-To: email#email.com";
$headers .= "\r\nContent-Type: myltipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents('path/test.txt')));
ob_start();
?>
--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="test.txt"
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";
?>
I'm not a PHP developer and have very limited experience using it, I am not getting any errors when this script is executed but I am also not receiving the email...any ideas why? Or what I should be looking at specifically?
Thank you for any tips!
EDIT:
As per the comments, I tried using SwiftMailer but I cannot get it to work using this code:
$message = Swift_Message::newInstance();
// Give the message a subject
$message->setSubject('Your subject');
// Set the From address with an associative array
$message->setFrom(array('email#email.com' => 'From Name'));
// Set the To addresses with an associative array
$message->setTo(array('email#email.com', 'email#email.com' => 'Name'));
// Give it a body
$message->setBody('Here is the message itself');
// And optionally an alternative body
$message->addPart('<q>Here is the message itself</q>', 'text/html');
// Optionally add any attachments
$message->attach(Swift_Attachment::fromPath('path/test.csv'));
Again, this code executes without any errors but no email is sent...what am I missing?
That tutorial has errors for its file attachment, I remember it now and I never could modify it to work. (In the past)
Here is a working copy from my own library that you're welcome to use.
Just change all instances of test.txt to the file you wish to attach.
<html>
<head>
<title>Send file attachments using PHP</title>
</head>
<body>
<?php
$to = "email#example.com";
$subject = "This is the subject";
$message = "This is the test message.";
# Open a file
$file = fopen( "test.txt", "r" );
if( $file == false )
{
echo "Error in opening file";
exit();
}
# Read the file into a variable
$size = filesize("test.txt");
$content = fread( $file, $size);
# encode the data for safe transit
# and insert \r\n after every 76 chars.
$encoded_content = chunk_split( base64_encode($content));
# Get a random 32 bit number using time() as seed.
$num = md5( time() );
# Define the main headers.
$header = "From:email#example.com\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num\r\n";
$header .= "--$num\r\n";
# Define the message section
$header .= "Content-Type: text/plain\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$message\r\n";
$header .= "--$num\r\n";
# Define the attachment section
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"test.txt\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"test.txt\"\r\n\n";
$header .= "$encoded_content\r\n";
$header .= "--$num--";
# Send email now
$retval = mail ( $to, $subject, "", $header );
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
?>
</body>
</html>
WITHOUT using PHPMailer, Swiftmailer, PEAR, Zend_mail, or any other libraries at all, I want to send an email with an image attachment inline.
The important part here is attaching it inline: I already am able to do everything else.
Inline meaning that it is able to be used by the HTML in the email in an image tag.
I really don't want to use PHPMailer or anything like that--I am not the only one who has tried to figure out how to do this on stackoverflow, and so far all the questions I've seen get nothing but arguments about why they should be using PEAR or Zend_mail or something. I don't want to do that, and I don't want to argue about it.
is this what you are looking for??
<?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";
?>
I don't know if this is what you want, but is easy to send an email with mail() function in PHP, in HTML format:
http://php.net/manual/en/function.mail.php
In the examples, there's one with HTML
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';
};