Php Email

Corrupted Attachments ???
I spent many hours with corrupted attachments (of all types of files) - The answer: a blank line is needed after $msg.=$file rn rn [incredible but true].
Heres some useful code for sending an attachment, and display html OR text depending on the users email-reader.
i work with many different systems, so..
<?php # Is the OS Windows or Mac or Linux
if (strtoupper(substr(PHP_OS,0,3)'WIN')) {
$eol='rn';
} elseif (
strtoupper(substr(PHP_OS,0,3)'MAC')) {
$eol='r';
} else {
$eol='n';
}
?>

<?php
# File for Attachment
$f_name='././letters/'.$letter; // use relative path OR ELSE big headaches. $letter is my file for attaching.
$handle=fopen($f_name, 'rb');
$f_contents=fread($handle, filesize($f_name));
$f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode();
$f_type=filetype($f_name);
fclose($handle);
# To Email Address
$emailaddress='[email protected]';
# Message Subject
$emailsubject='Heres An Email with a PDF'.date('Y/m/d H:i:s');
# Message Body
ob_start();
require(
'emailbody.php'); // i made a simple & pretty page for showing in the email
$body=ob_get_contents(); ob_end_clean();
# Common Headers
$headers .= 'From: Jonny <[email protected]>'.$eol;
$headers .= 'Reply-To: Jonny <[email protected]>'.$eol;
$headers .= 'Return-Path: Jonny <[email protected]>'.$eol; // these two to set reply address
$headers .= 'Message-ID:<'.$now.' [email protected]'.$_SERVER['SERVER_NAME'].'>'.$eol;
$headers .= 'X-Mailer: PHP v'.phpversion().$eol; // These two to help avoid spam-filters
# Boundry for marking the split & Multitype Headers
$mime_boundary=md5(time());
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= 'Content-Type: multipart/related; boundary='.$mime_boundary.''.$eol;
$msg = ';
# Attachment
$msg .= '--'.$mime_boundary.$eol;
$msg .= 'Content-Type: application/pdf; name='.$letter.''.$eol; // sometimes i have to send MS Word, use 'msword' instead of 'pdf'
$msg .= 'Content-Transfer-Encoding: base64'.$eol;
$msg .= 'Content-Disposition: attachment; filename='.$letter.''.$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= $f_contents.$eol.$eol;
# Setup for text OR html
$msg .= 'Content-Type: multipart/alternative'.$eol;
# Text Version
$msg .= '--'.$mime_boundary.$eol;
$msg .= 'Content-Type: text/plain; charset=iso-8859-1'.$eol;
$msg .= 'Content-Transfer-Encoding: 8bit'.$eol;
$msg .= 'This is a multi-part message in MIME format.'.$eol;
$msg .= 'If you are reading this, please update your email-reading-software.'.$eol;
$msg .= '+ + Text Only Email from Genius Jon + +'.$eol.$eol;
# HTML Version
$msg .= '--'.$mime_boundary.$eol;
$msg .= 'Content-Type: text/html; charset=iso-8859-1'.$eol;
$msg .= 'Content-Transfer-Encoding: 8bit'.$eol;
$msg .= $body.$eol.$eol;
# Finished
$msg .= '--'.$mime_boundary.'--'.$eol.$eol; // finish with two eol's for better security. see Injection.
# SEND THE EMAIL
ini_set(sendmail_from,'[email protected]'); // the INI lines are to force the From Address to be used !
mail($emailaddress, $emailsubject, $msg, $headers);
ini_restore(sendmail_from);
?>

I hope this helps.
Jon Webb [Madrid&London]
[EDITOR thiago NOTE: Code has a fix from o.straesser AT gmx DOT de]

Open your mail client to see if the message was delivered to the specified email address. (also check your spam folder!) If the message was delivered: Everything is fine - php mail is working. If the message was not delivered: Some provider don't allow external recipients when using php mail. Email; Newsletter Subscription; Offices; Request a Meeting; Issues. Animal Welfare; Campaign Finance Reform and Good Governance; Cannabis Reform; Civil and Constitutional Rights; Defense, Homeland Security, and Foreign Affairs; Education; Environment and Energy; Food and Farm; Gun Violence; Health Care and Social Security; Housing; Immigration.

-->

This guide demonstrates how to perform common programming tasks with the SendGrid email service on Azure. The samples are written in PHP.The scenarios covered include constructing email, sending email, and adding attachments. For more information on SendGrid and sending email, see the Next Steps section.

What is the SendGrid Email Service?

SendGrid is a cloud-based email service that provides reliabletransactional email delivery, scalability, and real-time analytics along with flexible APIsthat make custom integration easy. Common SendGrid usage scenariosinclude:

Php Email Marketing

  • Automatically sending receipts to customers
  • Administering distribution lists for sending customers monthly e-fliers and special offers
  • Collecting real-time metrics for things like blocked e-mail, and customer responsiveness
  • Generating reports to help identify trends
  • Forwarding customer inquiries
  • Email notifications from your application

For more information, see https://sendgrid.com.

Create a SendGrid Account

To sign up for a SendGrid account

  1. Sign in to the Azure portal.

  2. In the Azure portal menu or the home page, select Create a resource.

  3. Search for and select SendGrid.

  4. Complete the signup form and select Create.

  5. Enter a Name to identify your SendGrid service in your Azure settings. Names must be between 1 and 100 characters in length and contain only alphanumeric characters, dashes, dots, and underscores. The name must be unique in your list of subscribed Azure Store Items.

  6. Enter and confirm your Password.

  7. Choose your Subscription.

  8. Create a new Resource group or use an existing one.

  9. In the Pricing tier section select the SendGrid plan you want to sign up for.

  10. Enter a Promotion Code if you have one.

  11. Enter your Contact Information.

  12. Review and accept the Legal terms.

  13. After confirming your purchase you will see a Deployment Succeeded pop-up and you will see your account listed.

    After you have completed your purchase and clicked the Manage button to initiate the email verification process, you will receive an email from SendGrid asking you to verify your account. If you do not receive this email, or have problems verifying your account, please see our FAQ.

    You can only send up to 100 emails/day until you have verified your account.

    To modify your subscription plan or see the SendGrid contact settings, click the name of your SendGrid service to open the SendGrid Marketplace dashboard.

    To send an email using SendGrid, you must supply your API Key.

To find your SendGrid API Key

  1. Click Manage.

  2. In your SendGrid dashboard, select Settings and then API Keys in the menu on the left.

  3. Click the Create API Key.

  4. At a minimum, provide the Name of this key and provide full access to Mail Send and select Save.

  5. Your API will be displayed at this point one time. Please be sure to store it safely.

To find your SendGrid credentials

  1. Click the key icon to find your Username.

  2. The password is the one you chose at setup. You can select Change password or Reset password to make any changes.

Php Email

Php Mail With Html Examples

Signal messaging api. To manage your email deliverability settings, click the Manage button. This will redirect to your SendGrid dashboard.

For more information on sending email through SendGrid, visit the Email API Overview.

Using SendGrid from your PHP Application

Using SendGrid in an Azure PHP application requires no specialconfiguration or coding. Because SendGrid is a service, it can beaccessed in exactly the same way from a cloud application as it can froman on-premises application.

How to: Send an Email

You can send email using either SMTP or the Web API provided bySendGrid.

Php Email Attachment

SMTP API

PhpPhp

To send email using the SendGrid SMTP API, use Swift Mailer, acomponent-based library for sending emails from PHP applications. Youcan download the Swift Mailer library v5.3.0 (use Composer to install Swift Mailer). Sending email with the libraryinvolves creating instances of the Swift_SmtpTransport, Swift_Mailer, and Swift_Message classes, setting the appropriate properties, and calling the Swift_Mailer::send method.

Web API

Use PHP's curl function to send email using the SendGrid Web API.

Sending

SendGrid's Web API is very similar to a REST API, though it isnot truly a RESTful API since, in most calls, both GET and POST verbscan be used interchangeably.

How to: Add an Attachment

SMTP API

Sending an attachment using the SMTP API involves one additional line ofcode to the example script for sending an email with Swift Mailer.

The additional line of code is as follows:

This line of code calls the attach method on the Swift_Message object and uses static method fromPath on the Swift_Attachment class to get and attach a file to a message.

Web API

Sending an attachment using the Web API is very similar to sending an email using the Web API. However, note that in the example that follows, the parameter array must contain this element:

Example

How to: Use Filters to Enable Footers, Tracking, and Analytics

SendGrid provides additional email functionality through the use of filters. These are settings that can be added to an email message to enable specific functionality such as enabling click tracking, Google analytics, subscription tracking, and so on.

Filters can be applied to a message by using the filters property. Each filter is specified by a hash containing filter-specific settings. The following example enables the footer filter and specifies a text message that will be appended to the bottom of the email message. For this example we will use sendgrid-php library.

Use Composer to install library:

Example

Php

Next Steps

Php Email Template

Now that you've learned the basics of the SendGrid Email service, followthese links to learn more.

  • SendGrid documentation: https://sendgrid.com/docs
  • SendGrid PHP library: https://github.com/sendgrid/sendgrid-php
  • SendGrid special offer for Azure customers: https://sendgrid.com/windowsazure.html

For more information, see also the PHP Developer Center.