Corona SDK and other Game Development Tutorials
@gamebuilding

Sending Emails with Attachments using PHP

Recently, I’ve had to set up a cron job send out emails, but the catch was the email needed to have an attachment. There are several ways to send out an email with an attachment using php, but the easiest way I found (so far) was using the file class.phpmailer.php. The class can be found at https://github.com/PHPMailer/PHPMailer and once your incorporate this class, send emails with attachments becomes a breeze.

Below is the sample code provided by the developer of PHPMailer, with some few modifications. Remember, you have to have the file class.phpmailer.php in order for this to work properly.

IsSendmail();

	//Set who the message is to be sent from
	$mail->SetFrom('myname@email.com', 'Daniel');

	//Set who the message is to be sent to
	$mail->AddAddress('recipient@email.com', 'Daniel');	

	//Set the subject line
	$mail->Subject = 'Subject Line';

	//Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
	$mail->MsgHTML('

Some HTML Tags

Body of the email

', dirname(__FILE__)); //Replace the plain text body with one created manually $mail->AltBody = 'This is the plain text portion of the email.'; //Attach an image file $mail->AddAttachment('file-attachment-name.zip'); //Send the message, check for errors if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } ?>
2 Comments
  1. Thanks?

  2. Thanks

Leave a Reply