I'm using the PHP Swift Mailer library (http://swiftmailer.org/) to send email to users. Following their documentation about sending embedded images (http://swiftmailer.org/docs/embedding-dynamic-content/) I have the following code the send images. The problem is that the images aren't sending so I'm guessing that I haven't actually created them or stored them in a way that is usable. I'd like to be able to send them without saving them to disk which, I believe, should be possible.

function resetpassword($username, $password) {
	global $loginDatabase;
	$font = 4;
	$width  = ImageFontWidth($font) * strlen($password);
  	$height = ImageFontHeight($font);
  	$im = @imagecreatetruecolor ($width,$height);
  	imagecolorallocate ($im, 255, 255, 255);
  	$text_color = imagecolorallocate ($im, 0, 0, 0);
  	imagestring ($im, $font, 0, 0,  $password, $text_color);
	$subject = "Password Reset";
	$userInfo = $loginDatabase->getUserInfo($username);
	$mailer = Swift_Mailer::newInstance ( $this->transport );
	$message = Swift_Message::newInstance($subject);
	$message->setContentType('text/html');
  	$message->setFrom ( array (SERVICE_ADDR => SITE_NAME ) );
 	$message->setTo ( array ($userInfo['email'] =>$userInfo['firstname']." ".$userInfo['lastname'] ) );
  	$message->setBody ( '<html>' .
	' <head></head>' .
	' <body>' .
	$userInfo['firstname'] .', your password has been reset as you requested.<br />'.
	'Your new password is:<br />'.
	'<img src="' .
	$message->embed(Swift_Image::newInstance($im, 'image.jpg', 'image/jpeg')).
	'" alt="Image" /><br />' .
	'  Please login and change this as soon as possible.<br />'.
	'  Thank you.'.
	' </body>' .
	'</html>',
  	'text/html' //Mark the content-type as HTML 
	);		
	return $mailer->send ( $message );
}

The issue you have is that you are using the IMG HTML tag, this requires you to have the image saved in order for the IMG tag to retreive the image and display it.

The knowledge I have of PHP's image functions is that they will generate images and save them to directories or generate them in the current document (so you can use image.php as an image).

Currently what you are doing is making an image and then giving it a name then adding it to an email using the IMG tag. In this proccess you aren't actually saving the image to a directory for the email to reference.

What you are trying to do is possable. You will need to attach the image to the email then reference it in the body of the message.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.